R: create generic plot in ggplot2 -
trying create line chart representing simple slopes analysis. created plot using builtin plot() command. how rewrite code ggplot2? tried in ggplot2 couldn't achieve figure wrote using plot().
here code used create plot.
yrange = c(4,7) xrange = c(-1.5,1.5) par(bty = 'l') par(family="times") plot(c(-1, 1), c(5.8, 6.2), type='b', lty=1, pch = 15, axes=f, xlab="", ylab="", ylim=yrange, xlim=xrange) par(new = t) plot(c(-1, 1), c(5.3, 5.5), type='b', lty=2, pch = 17, axes=f, xlab="iv1", ylab="dv", ylim=yrange, xlim=xrange) axis(1, at=c(-1,1), labels=c("cond1", "cond2")) axis(2, at=c(4, 5,6,7)) legend("topright", title = "moderator", c("high", "low"), lty=1:2) box() here code used while trying create above figure using ggplot2. couldn't fiture out how create legend moderator, how change x-axis labels, add boxes/triangles/circles end of lines, or otherwise set lines proper publication quality apa figure::
ggplot(df) + geom_segment(aes(x = -1, y = 5.8, xend = +1, yend = 6.2), linetype = 1) + geom_segment(aes(x = -1, y = 5.3, xend = +1, yend = 5.5), linetype = 2) + labs(x = 'iv1', y = 'dv') + coord_cartesian(ylim = c(4, 7)) + scale_x_continuous(breaks=seq(-1, +1, 2)) + theme(legend.position = "bottom") and, sorry, first post here. interested in using ggplot2 more it's got steep learning curve, r.
@pdubbs - updated code make closer situation. noted below, i'm trying plot 2x2 interactions between 2 continuous variables , figure i'm creating visualize interaction using simple slopes regression analysis. here happens , output when run code:
> df<-data.frame(dv = c(5.8,5.3,6.2,5.5), + moderator = c(1,2,3,4), + iv1 = c(6,4,3,2)) > > ggplot(df,aes(x=iv1,y=dv,shape=moderator)) + geom_point() + + geom_segment(aes(x = 1, y = 5.8, xend = 2, yend = 6.2), linetype = 1) + + geom_segment(aes(x = 1, y = 5.3, xend = 2, yend = 5.5), linetype = 2) + + labs(x = 'iv1', y = 'dv') + + coord_cartesian(ylim = c(4, 7)) + + theme(legend.position = "bottom") error: continuous variable can not mapped shape a couple last changes can't figure out. how increase size endpoints of lines? instance, make triangles larger?
a couple other issues legend. first, how put black box around legend? not black background, simple black box outline around legend. second, how add solid or dashed line legend in addition circle or triangle? third, how center legend title text? see generic plot code example of mean 3 of these.
your problem seems you're lacking aesthetic , layer data points. geom_point plot datapoints, giving legend , shapes on lines want. aes inside main ggplot command tells ggplot how data in df should plotted. can plot below:
df<-data.frame(dv = c(5.8,5.3,6.2,5.5), moderator = c("high", "low", "high", "low"), iv1 = c("cond1","cond1","cond2","cond2")) ggplot(df,aes(x=iv1,y=dv,shape=moderator)) + geom_point() + geom_segment(aes(x = 1, y = 5.8, xend = 2, yend = 6.2), linetype = 1) + geom_segment(aes(x = 1, y = 5.3, xend = 2, yend = 5.5), linetype = 2) + labs(x = 'iv1', y = 'dv') + coord_cartesian(ylim = c(4, 7)) + theme(legend.position = "bottom")
Comments
Post a Comment