开发者

R: draw a line on the same boxplot graph

I try to display a line on top of a boxplot graph with the x made from factor.

This code work well:

x <- c(91,92,93,125,123开发者_开发百科,140)
y <- c(200,260,220,300,350,360)
d1 <- data.frame(x=x,y=y)
d1$f1 = factor(round(d1$x/10))
qplot(f1,y,data=d1,geom="boxplot")
d2<-data.frame(x2=c(90,140),y2=c(210,320))
qplot(x2,y2,data=d2,geom="line")

But when i try to add the line to the graph...

qplot(f1,y,data=d1,geom="boxplot") + geom_line(data = d2, aes(x = x2, y=y2))

To see my results: http://jeb-files.s3.amazonaws.com/Clipboard01.jpg

How do I manage to have my line align with my boxplot?

Thanks!


A boxplot requires the x-values to be factors, whereas a geom_line requires the x-values to be numeric. You can get what you want by modifying the geom_line call so that the x value is defined as the numeric version of the ordered factor obtained from round(x2/10):

qplot( f1,y,data=d1,geom="boxplot") +
 geom_line(data = d2, aes(x = as.numeric(ordered(round(x2/10))), y=y2))

R: draw a line on the same boxplot graph

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜