0
votes

I am trying to plot the following dataframe with ggplot2, but I have several issues. Also, the visual aspect doest not seem very nice

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- The X axis is discrete, this is why i define it as character, while my data is organized as following (300,600,900,7200), when ploting the X axis is reordered as following (300,600,7200, 900), How to maintain the previous order ?

2- Error bars are not correctly aligned on value, sometimes going more to the left or the right, how can i fix it ?

3- How can I combine correctly geom_line and geom_point to get one colour and one single point shape for each drawn line ?

Thanks!

enter image description here

1
You are using aes incorrectly. Map values to column names, do not call values directly. It's a great mystery to me where people learn this style with ggplot2, where all examples I see on the internet use the proper way. You are using dodge, which shifts points causing misalignment. Change characters to factors and specify the correct order.Roman Luštrik
You mean aes in geom_barerror ? concerning dodge, must i delete it ? finally i will check the last part ;) many thanksHanniBaL90
If you provide a reproducible example, perhaps we can have a look.Roman Luštrik
i put all needed data frame in the example above, don't pay attention to further line on the legend (i intentionally exclude some lines because putting here all data will make the post very long)HanniBaL90

1 Answers

2
votes

Hopefully this is what you're looking for:

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

EDIT:

Adding a line between the points:

 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

Input data:

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))