如何在ggplot2的函数geom_pointrange()中用一条线连接点并避免重叠?

时间:2017-07-27 03:39:28

标签: r plot ggplot2

我想调整此图以避免垂直线的重叠,并使用相同的颜色连接所有点(参见下图)。

的代码:

library(ggplot2)
dat=read.csv("data.csv",header=T)

p<-ggplot(dat, aes(x=population, y=resp, group=population,color=cov)) + 
  geom_pointrange(aes(ymin=resp-sd, ymax=resp+sd))

enter image description here

数据

    resp    cov population  sd  

    42.07   X1      A      0.33 

    41.92   X1      B      0.54 

    42.2    X1      C      0.25 

    41.92   X1      D      1.15 

    41.98   X1      E      1.35

    42.5    X1      F      0.51

    42.85   X1      G      0.35

    38.37   X2      A      0.13

    .....  

1 个答案:

答案 0 :(得分:0)

您可以尝试更改position参数以避免重叠的行:

p<-ggplot(dat, aes(x=population, y=resp, group=population,color=cov)) + 
  geom_pointrange(aes(ymin=resp-sd, ymax=resp+sd), position = position_dodge(width = 0.2)