5

I'm trying to plot a scatter graph with error bars in R using categorical data on the x axis, using the following code:

Nesk <- read.table("E:\\R stuff\\Chapter 2\\Boxplots of nb\\NEnbNOINF.txt", header=TRUE, fill=TRUE) 

pd <- position_dodge(0.2) 

ggplot(Nesk, aes(x = TYPE, y = NB, color = TYPE)) + 
  geom_jitter() + 
  geom_point(position = pd) +
  geom_errorbar(aes(ymin = LC, ymax = UC), position = pd) + 
  theme_bw() + 
  theme(axis.title = element_text(face = "bold")) + 
  ylab("Nb")

However, I can't get the error bars on the jittered points. I end up with this https://i.stack.imgur.com/q3ayt.jpg. Sorry all, don't have the reputation to directly insert images

I've tried using position dodge however I'm aware that it just separates the points by category (COL, LIN, NOM) as opposed to within each category. Is there any way I can jitter the points and attach error bars to these? I've seen some posts with fixes for this, but I think somewhere along the line an update invalidated those.

Thanks in advance!

Whilux
  • 63
  • 6
  • Perhaps you could add manual jitter? – Roman Luštrik Nov 22 '17 at 11:33
  • Hi Roman, do you mean by supplying the different arguments to geom_jitter? – Whilux Nov 22 '17 at 11:38
  • @Whilux have you seen this [post](https://stackoverflow.com/questions/25789434/dodging-points-and-error-bars-with-ggplot)? – mnm Nov 22 '17 at 13:16
  • @Ashish Just solved the issue by copying the x-axis on that post, giving my named categories numbers to provide a scale, and each point a unique identifier so that position_dodge would apply to spread them out. Cheers! – Whilux Nov 22 '17 at 13:29
  • This cannot work as written, because the jitter is generated inside `geom_jitter` and `geom_errorbar` doesn't have access to it. You need to jitter your data before handing it to `ggplot`. Please provide a fully reproducible example, with data, so we can edit it and improve it. – Claus Wilke Nov 22 '17 at 21:21
  • You will have to use `geom_point` and applying jitter before that. – Roman Luštrik Nov 22 '17 at 21:48

0 Answers0