[英]How to plot two lines on one plot with ggplot2 while specifying order of the x-axis labels?
The data are in "stats":数据在“统计”中:
run valence mean sd sem
Nnf1_mINS n 2.1 .5 .01
Nnf2_mINS p 3.2 .2 .01
Nobs_mINS n 2.3 .1 .02
Here's my code:这是我的代码:
ggplot(stats,aes(x=run,y=mean,color=valence,group=1))+
geom_point() +
geom_line()+
geom_errorbar(aes(ymin = mean - sem, ymax = mean + sem),
width = 0.2,
position = position_dodge(0.9))
3 problems: 3个问题:
stats$run <- factor(stats$run, levels = c("Nobs_mINS","Nnf1_mINS","Nnf2_mINS"))
but to plot 2 lines on one plot, there can't be a factor variable.但是要在一个图上绘制 2 条线,不能有因子变量。 This is the error: "geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?"
这是错误:“geom_path:每个组仅包含一个观察值。您需要调整组审美吗?” I fixed this error by adding "group=1" thanks to this post: ggplot2 line chart gives "geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?"
由于这篇文章,我通过添加“group=1”修复了这个错误: ggplot2 折线图给出了“geom_path:每个组只包含一个观察值。你需要调整组美学吗?”
For some reason the line is not connected correctly:由于某种原因,线路未正确连接:
Well, group = valence solves the problem!好吧,group = valence 解决了这个问题! Here's the edited code:
这是编辑后的代码:
ggplot(stats,aes(x=run,y=mean,color=valence,group=valence))+
geom_point() +
geom_line()+
geom_errorbar(aes(ymin = mean - sem, ymax = mean + sem),
width = .2,
position = position_dodge(.09))+
theme(text = element_text(size=15),
axis.text.x = element_text(angle=0, hjust=1))+
scale_y_continuous(limits = c(-0.22, 0.1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.