首页 文章

ggplot2存在多个问题(离散X_axis,错误栏没有正确对齐)

提问于
浏览
0

我试图用ggplot2绘制以下数据帧,但我有几个问题 . 此外,视觉方面看起来不太好

selectedResDF <- data.frame(protocol=character(), run=character(), x=character(), y=double(), sd=double())
selectedResDF
    protocol run    x          y           sd
1  DDelivery   B  300 0.05063383 2.009576e-04
2  DDelivery   C  600 0.05064577 8.512595e-05
3  DDelivery   D  900 0.05065898 1.027849e-04
4  DDelivery   A 7200 0.05066435 1.505408e-04
21  Epidemic   B  300 0.73445680 8.737406e-03
22  Epidemic   C  600 0.80729300 3.713654e-03
23  Epidemic   D  900 0.80729514 6.705972e-03
24  Epidemic   A 7200 0.80680767 5.182245e-03

pd <- position_dodge(0.05)
oneCfgPlot <- ggplot(selectedResDF, aes(x=selectedResDF$x, y=selectedResDF$y, group=selectedResDF$protocol, colour=selectedResDF$protocol)) + geom_errorbar(aes(ymin=selectedResDF$y-selectedResDF$sd, ymax=selectedResDF$y+selectedResDF$sd), color="black", width=.1, position=pd) + geom_line(position=pd) + geom_point(position=pd, size=3, shape=21, fill="white")

print(oneCfgPlot)

Issues

1- X轴是离散的,这就是为什么我将它定义为字符,而我的数据组织如下(300,600,900,7200),当绘制X轴时重新排序如下(300,600,7200,900), How to maintain the previous order ?

2-错误栏未正确对齐值,有时会向左或向右移动, how can i fix it ?

3-如何正确组合geom_line和geom_point以获得每条绘制线的一种颜色和一种单点形状?

谢谢!

enter image description here

1 回答

  • 2

    希望这是你正在寻找的:

    ggplot(df, aes(x, y, group=protocol, colour=protocol)) + 
      geom_errorbar(aes(ymin=y-sd, ymax=y+sd), width=.1) +
      geom_point(size=1, shape=16) +
      theme_bw()
    

    enter image description here

    编辑:

    在点之间添加一条线:

    ggplot(df, aes(x, y, group=protocol, colour=protocol)) + 
      geom_errorbar(aes(ymin=y-sd, ymax=y+sd), width=.1) +
      geom_line() + 
      geom_point(size=1, shape=16) + 
      theme_bw()
    

    enter image description here

    输入数据:

    df <- structure(list(protocol = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L), .Label = c("DDelivery", "Epidemic"), class = "factor"), 
        run = structure(c(2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L), .Label = c("A", 
        "B", "C", "D"), class = "factor"), x = structure(c(1L, 2L, 
        3L, 4L, 1L, 2L, 3L, 4L), .Label = c("300", "600", "900", 
        "7200"), class = "factor"), y = c(0.05063383, 0.05064577, 
        0.05065898, 0.05066435, 0.7344568, 0.807293, 0.80729514, 
        0.80680767), sd = c(0.0002009576, 8.512595e-05, 0.0001027849, 
        0.0001505408, 0.008737406, 0.003713654, 0.006705972, 0.005182245
        )), class = "data.frame", .Names = c("protocol", "run", "x", 
    "y", "sd"), row.names = c(NA, -8L))
    

相关问题