分别绘制趋势线

时间:2019-06-18 21:07:10

标签: r ggplot2 plot

我想绘制一个如图2所示的图,但是我不能绘制每种栖息地类型的趋势线,而只能绘制一条普通线。

有人知道我该写哪个命令吗?

此外,我还要指定y轴的比例(0,0.5,1,1.5 ...)

library(ggplot2)
pd <- position_dodge(0.1)
ggplot(dados, aes(x=sp, y=abund, colour=habitat, group = 1)) + 
  geom_errorbar(aes(ymin=abund-dv, ymax=abund+dv), colour="black", width=.1, position=pd) +
 geom_point(position=pd, size=4)+
  geom_smooth(method = "lm", aes(fill=habitat))+
  scale_fill_brewer(palette="Dark2") + 
  scale_colour_brewer(palette="Dark2")+
  theme_bw()
sp  body_mass   habitat abund   dv
AAOtol_cras 1206.61 FO  -0.021666667    -0.010833333
ABMiop_tal  1248.86 FO  -0.021666667    -0.010833333
ACCLep_cap  1500    FO  -0.041666667    -0.102062073
ADGenet_gen 1756.17 FO  -0.047619048    -0.162590417
BAThry_swin 4000    FO  -0.382978723    -0.28152599
BDPhil_mont 6000    FO  -0.725806452    -0.377581583
CALept_serv 11999.96    FO  -0.433333333    -0.287270381
AAOtol_cras 1206.61 S   -0.021666667    -0.010833333
ABMiop_tal  1248.86 S   -0.021666667    -0.010833333
ACCLep_cap  1500    S   -0.047619048    -0.109108945
ADGenet_gen 1756.17 S   -0.236842105    -0.118421053
BAThry_swin 4000    S   -0.475  -0.29930475
BDPhil_mont 6000    S   -0.944444444    -0.446325385
CALept_serv 11999.96    S   -0.590909091    -0.311003287

当前图:

enter image description here

所需图:

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要将group中的aes()与变量habitat一起使用,以便将其也传递到geom_smooth

library(ggplot2)

ggplot(dados, aes(x=sp, y=abund, colour=habitat, group = habitat)) + 
  geom_errorbar(aes(ymin=abund-dv, ymax=abund+dv), 
                colour="black", width=.1, position=position_dodge(0.1)) +
  geom_point(position=position_dodge(0.1), size=4) +
  geom_smooth(method = "lm", aes(color=habitat)) +
  scale_fill_brewer(palette="Dark2") + 
  scale_colour_brewer(palette="Dark2") +
  theme_bw()

您可以在se=FALSE中使用geom_smooth来消除灰色置信区间,也可以使用fill代替color或与dados <- read.table(text="sp body_mass habitat abund dv AAOtol_cras 1206.61 FO -0.021666667 -0.010833333 ABMiop_tal 1248.86 FO -0.021666667 -0.010833333 ACCLep_cap 1500 FO -0.041666667 -0.102062073 ADGenet_gen 1756.17 FO -0.047619048 -0.162590417 BAThry_swin 4000 FO -0.382978723 -0.28152599 BDPhil_mont 6000 FO -0.725806452 -0.377581583 CALept_serv 11999.96 FO -0.433333333 -0.287270381 AAOtol_cras 1206.61 S -0.021666667 -0.010833333 ABMiop_tal 1248.86 S -0.021666667 -0.010833333 ACCLep_cap 1500 S -0.047619048 -0.109108945 ADGenet_gen 1756.17 S -0.236842105 -0.118421053 BAThry_swin 4000 S -0.475 -0.29930475 BDPhil_mont 6000 S -0.944444444 -0.446325385 CALept_serv 11999.96 S -0.590909091 -0.311003287", header=T) 来获得相同的颜色点数。

数据:

[Manufacturer]
"Company Name" = DDInstallSectionName, architecture

[DDInstallSectionName.architecture]
"Driver Display Name" = Name_Of_Install_Section, HardwareId