R 使用不同的线型连接ggplot2中的点
“a”是一个数据帧R 使用不同的线型连接ggplot2中的点,r,ggplot2,R,Ggplot2,“a”是一个数据帧 set.seed(2) a<-data.frame(group= rep(c("A","B","C"),each=4),factor=rep(c(1,1,2,2),3), model=rep(c("old","new"),6),mean=runif(12),sd=runif(12)/10) >a group factor model mean sd 1 A
set.seed(2)
a<-data.frame(group= rep(c("A","B","C"),each=4),factor=rep(c(1,1,2,2),3),
model=rep(c("old","new"),6),mean=runif(12),sd=runif(12)/10)
>a
group factor model mean sd
1 A 1 old 0.1848823 0.076051331
2 A 1 new 0.7023740 0.018082010
3 A 2 old 0.5733263 0.040528218
4 A 2 new 0.1680519 0.085354845
5 B 1 old 0.9438393 0.097639849
6 B 1 new 0.9434750 0.022582546
7 B 2 old 0.1291590 0.044480923
8 B 2 new 0.8334488 0.007497942
9 C 1 old 0.4680185 0.066189876
10 C 1 new 0.5499837 0.038754954
11 C 2 old 0.5526741 0.083688918
12 C 2 new 0.2388948 0.015050144
set.seed(2)
aa
组因子模型平均标准差
1 A 1旧的0.1848823 0.076051331
2 A 1新的0.7023740 0.018082010
3 A 2旧的0.5733263 0.040528218
4 A 2新的0.1680519 0.085354845
5 B 1旧的0.9438393 0.097639849
6 B 1新的0.9434750 0.02582546
7 B 2旧的0.1291590 0.044480923
8 B 2新的0.8334488 0.007497942
9 C 1旧的0.4680185 0.066189876
10 C 1新的0.5499837 0.038754954
11 C 2旧的0.5526741 0.083688918
12 C 2新的0.2388948 0.015050144
我想用ggplot2画一个“平均值±标准偏差”的线图。
我的目标是画一幅画(x轴是“组”,y轴是“平均值±标准差”,不同的“因子”应该有不同的颜色,不同的“模型”应该有不同的连接线类型(旧模型是虚线,新模型是实线))
我使用以下代码:
library("ggplot2")
pd <- position_dodge(0.1) #The errorbars overlapped, so use position_dodge to move
#them horizontally
plot<-ggplot(a, aes(x=group, y=mean, colour=as.factor(factor), linetype=model)) +
geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), width=.1, position=pd) +
geom_point(position=pd, size=3, shape=21, fill="white") + # 21 is filled circle
xlab("Groups") +
ylab("Power") +
geom_line(position=pd) +
scale_linetype_manual(values = c(new = "solid", old = "dashed"))
库(“ggplot2”)
pd我建议添加一个新的分组变量:
a$group2 <- paste(a$factor, a$model, sep="_")
非常感谢您!看起来不错!但是有一个问题:同一颜色的新型号和旧型号重叠,你能帮我处理一下吗,谢谢@例如,在B组中,两个红色的“平均值±标准偏差”重叠,最好不要将它们排列在同一个x轴上,一个小间隙更好。如果我设置“pd OK”,我现在明白你的意思,但遗憾的是,我现在想不出解决方案,对不起。
ggplot(a, aes(x = group, y = mean, colour = as.factor(factor))) +
geom_errorbar(aes(ymin = mean-sd, ymax = mean+sd),
width = .1, position = pd) +
geom_point(position = pd, size = 3, shape = 21, fill = "white") +
xlab("Groups") +
ylab("Power") +
geom_line(aes(x = group, y = mean, colour = as.factor(factor),
group = group2, linetype = model)) +
scale_linetype_manual(values = c(new = "solid", old = "dashed"))