0

I have created a for looped code using ggplot2 to create similar bar plots using a large analysed dataset. I have added code to make the origin be at y=0, without the gap that seems to be standard with ggplot2 with:

scale_y_continuous(expand = c(0,0)) 

Which means that the graphs are now automatically cutting off at to top of the largest error bar (see picture below).

enter image description here

I know that I can hard code in the limits of the graph as so:

scale_y_continuous(expand = c(0,0), limits = c(0, 100), breaks = seq(0,100,25))

But I am using a loop and not all graphs will need the same limits. Does anyone know a way to set the y limits to say 10 units above the height that the error bar reaches?

I have tried using (sem is what the error bars are measured by):

scale_y_continuous(expand = c(0,0), limits = c("Mean" + "sem"))

But that doesn't work as they are non-numeric

Error in "Mean" + "sem" : non-numeric argument to binary operator

Recommendations greatly appreciated! Sorry I haven't included reproducible data, but this problem is not specific to my data!

dunnns
  • 25
  • 4
  • 2
    "Mean" and "sem" are character strings. If you have numeric objects saved as 'Mean' and 'sem' then you need to use the names, not strings (e.g. `Mean + sem`). If that doesn't work, we'd need to see a little more of your code to see exactly what you are doing. – Andrew Taylor Nov 23 '16 at 12:49
  • @Henrik - yes, I know. Sorry I didn't have time to make an easy dataset up to use. Mine is pretty complicated so I'm not posting that! – dunnns Nov 23 '16 at 13:04
  • @AndrewTaylor - I've tried it as names without the " either side - it says **Error in continuous_scale(c("y", "ymin", "ymax", "yend", "yintercept", : object 'Mean' not found**. But it is using Mean as the y aesthetic! I will post the code for my plot and maybe try to make some data to go with it... – dunnns Nov 23 '16 at 13:08
  • @Henrik I'm sorry that I have clearly angered you. I was just seeing if anyone had had the same issue with their own data and found a solution to it. Thank you for sending me to the previously answered question. I had not found that on my search of Stack Overflow. I have not got the solution to work for my data yet, but hopefully the answer is in there. – dunnns Nov 23 '16 at 13:29
  • 1
    I have used `coord_cartesian(ylim=c(0, max(df$Mean)+max(df$sem)+0.5), expand = FALSE` and have got the results I wanted. Thank you again to @Henrik for pointing me to the question in which I found the answer. I'm sorry that my question had already been answered, but for some reason I didn't find that answer before! I am still fairly new to R so must not have used the right terms in my search. – dunnns Nov 23 '16 at 13:43

0 Answers0