Overlaying two plots??

69 views
Skip to first unread message

Gray, Wayne

unread,
Jan 6, 2016, 12:51:35 AM1/6/16
to ggplot2
This is driving me nuts. What I want to do is to overlay the two attached plots (referred to as the “dots plot" and the “jags plot"for visually apparent reasons). Discovery learning is not working, neither are endless variations on web searches.

Here are the basics: I have a large number of dependent variables that fall on the same x-axis. The x-axis runs from 1-248 in intervals of 1 (call this “games”). Both plots show different aspects of the same 10 variables. The “dots" plot is categorical and the “jags” plot is continuous.

The continuous variables rises and falls across the 248 games, but mostly rises as expertise increases. There is one “measure” for each variable for each game.

The second categorical variable, “changepoints”, indicates games at which the game-to-game rise (or fall) for these two variables differed than what would have been expected. Our “changepoint” measure yields 1-7 changepoints for each of the 10 variables shown in the dot plot. (There are, of course, more players and more variables — but those issues are easy to handle.)

For ease of inspection, I would to overlay the dots with the jags for each of the measures.

This seems like a simple problem to solve but I am bewildered by it. My failed code won’t help anyone. However, I have included the much simpler code for each plot. — but I think the plots are more useful.

Thanks!

Wayne
############################

Dot plot code:

for(sid in unique(scd.ss$SID)){
plotCPTs <- subset(scd.ss, SID == sid)

plotprint = ggplot(plotCPTs, aes(x = ChangePoint, y=score_type, color = score_type)) +
ggtitle(sid) + theme(plot.title = element_text(lineheight=1, face="bold")) +
geom_line() + geom_point(aes(size=12)) + theme(legend.position="none") +
scale_y_discrete(name="Measures and Factors") +
scale_x_continuous(name="Games", limits=c(1,248), breaks= seq(from = 0, to = 248, by = 31)) +
theme(axis.title.x = element_text(face="bold", colour="#990000", size=20)) +
theme(axis.title.y = element_text(face="bold", colour="#990000", size=20)) +
theme(axis.text.y = element_text(angle = 60, size=12)) +
## vertical lines are "visual aids" only
geom_rect(aes(xmin=24, xmax=24, ymin=-Inf, ymax=Inf), fill="gray90", alpha=0.1, linetype="solid") +
geom_rect(aes(xmin=74, xmax=74, ymin=-Inf, ymax=Inf), fill="gray90", alpha=0.1, linetype="solid") +
geom_rect(aes(xmin=124, xmax=124, ymin=-Inf, ymax=Inf), fill="gray90", alpha=0.1, linetype="solid") +
geom_rect(aes(xmin=174, xmax=174, ymin=-Inf, ymax=Inf), fill="gray90", alpha=0.1, linetype="solid") +
geom_rect(aes(xmin=224, xmax=224, ymin=-Inf, ymax=Inf), fill="gray90", alpha=0.1, linetype="solid")

ggsave(paste0("plots_160105/DotPointPlots/", sid, ".pdf"), width=8.5, height=14, plotprint)
}

Jaggy plot code:
for(sid in unique(run.me$SID)){
plotSCORE <- subset(run.me, SID == sid)

plotprint = ggplot(plotSCORE, aes(x=Game, y = score_value, group = score_type)) +
theme_bw() +
geom_line(color="darkgreen") +
geom_point(color="black") +
facet_grid(score_type ~. , scales="free_y") + theme_bw() +
ggtitle("Changepoints determined by running cpt.meanvar on Total score")

ggsave(paste0(plot.me, sid, ".pdf"), width=14, height=14, plotprint)
}



sid1133_dots.pdf
sid1133_scores.pdf

Crump, Ron

unread,
Jan 6, 2016, 4:09:26 AM1/6/16
to Gray, Wayne, ggplot2
Wayne,

Does this help to get you started?

# A little bit of simulated data...
# Observations on all games for two variables
x<-1:250
y1 <- 0.25*x+rnorm(250,0,5)
y2 <- 3*x+rnorm(250,0,10)
DF<-data.frame(x=c(x,x),y=c(y1,y2),v=rep(c("y1","y2"),each=250))
# Randomly choose some games to be highlighted (change points)
DF2<-data.frame(x=c(sample(1:250,4),sample(1:250,6)),v=c(rep("y1",4),rep("y
2",6)))

# merge to add the y value for the randomly chosen x value
DF2<-merge(DF,DF2,all=FALSE)

ggplot()+geom_line(data=DF,aes(x=x,y=y))+geom_point(data=DF,aes(x=x,y=y),si
ze=2)+geom_point(data=DF2,aes(x=x,y=y),colour="red",size=3)+facet_wrap(~v,n
col=1,scales="free_y")

Or maybe excluding the dot for each data point, which I think looks a bit
better:

ggplot()+geom_line(data=DF,aes(x=x,y=y))+geom_point(data=DF2,aes(x=x,y=y),c
olour="red",size=3)+facet_wrap(~v,ncol=1,scales="free_y")


If you want the dots to be on the same horizontal, you will need to choose
an appropriate value within the y-range of each variable. I think what I
have done is easier. I did think marking the change points with a vertical
line might be good, but I see you are using vertical lines for something
else.

The vertical lines you are doing with geom_rect, you might do with
geom_vline, or set the x-axis grid points to be at those values.

Ron.
>--
>--
>You received this message because you are subscribed to the ggplot2
>mailing list.
>Please provide a reproducible example:
>https://github.com/hadley/devtools/wiki/Reproducibility
>
>To post: email ggp...@googlegroups.com
>To unsubscribe: email ggplot2+u...@googlegroups.com
>More options: http://groups.google.com/group/ggplot2
>
>---
>You received this message because you are subscribed to the Google Groups
>"ggplot2" group.
>To unsubscribe from this group and stop receiving emails from it, send an
>email to ggplot2+u...@googlegroups.com.
>For more options, visit https://groups.google.com/d/optout.
>


Gray, Wayne

unread,
Jan 6, 2016, 1:38:32 PM1/6/16
to Crump, Ron, ggplot2
Ron,

Fantastic! And like all good solutions — very clear . . . once you know how! Bah!!

Many thanks,

Wayne

sid1133_Scores_&_CPTs.pdf
Reply all
Reply to author
Forward
0 new messages