R:在同一个boxplot图上绘制一条线 [英] R: draw a line on the same boxplot graph

查看:275
本文介绍了R:在同一个boxplot图上绘制一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这段代码很好用:

  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 =因子(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)

但是当我尝试将该行添加到图表中时...



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

查看我的结果: http://jeb-files.s3.amazonaws.com/Clipboard01.jpg



我如何设法让我的在线对齐我的boxplot?



谢谢!

解决方案

code> boxplot 要求x值是因素,whe重新设置 geom_line 要求x值为数字。你可以通过修改 geom_line 调用来获得你想要的结果,这样 x 值被定义为 round(x2 / 10)获得的 因子:

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


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:在同一个boxplot图上绘制一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆