2

When I set the ylim within the scale_y_continousus, the bar plots disappear, I have tried changing the minimum and maximum but there is always some bars missing. Why is this? I would like to adjust it to 10^4 to 10^7.

A = c(1,2,3,4,5,6,7)
B= c(33045, 970250, 1870125, 259625, 3838750, 962333, 2272916.667)
sd = c(8538, 319023, 1538958, 27753, 1602186, 561207, 322322)
df = data.frame(A,B,sd)

ggplot(df, aes(x=A, y=B)) +
    geom_bar(colour="black", stat="identity", fill="grey")  +
  geom_errorbar( aes(x=A, ymin=B-sd, ymax=B+sd), width=0.2, colour="black", alpha=0.9, size=0.5) +
  scale_x_continuous(breaks = 1:7)+
  theme_classic()+
  theme(strip.placement = "outside", 
        axis.text.x = element_text( size = 12 ))+
  scale_y_continuous(trans='log10', limits = c(10000,10000000))

enter image description here

Ecg
  • 908
  • 1
  • 10
  • 28
  • 2
    Replace the final line with: `scale_y_continuous(trans='log10') + coord_cartesian(ylim = c(1e4, 1e7))`. The `coord_cartesian` will "clip" elements of the plot to the limits after drawing them. In contrast setting the limits inside the y scale will set the limits and then draw the elements of the plot. That means elements that are all or partly outside the limits (like the bars) won't draw. – qdread Jan 26 '21 at 15:11
  • 2
    Does this answer your question? [How to set limits for axes in ggplot2 R plots?](https://stackoverflow.com/questions/3606697/how-to-set-limits-for-axes-in-ggplot2-r-plots) – qdread Jan 26 '21 at 15:12
  • Yes, thanks, therefore, the 'ylim=' within the "scale_y_continuous" is only worth when you have a natural/normal ylim? – Ecg Jan 26 '21 at 15:21
  • 1
    There are use cases for both. However I would also advise you not to use `geom_bar()` with a log y axis. See https://ggplot2.tidyverse.org/reference/geom_bar.html where it states "Proceed with caution when using transformed scales with a bar chart. It's important to always use a meaningful reference point for the base of the bar. For example, for log transformations the reference point is 1. In fact, when using a log scale, geom_bar() automatically places the base of the bar at 1." I think it can be misleading so I'd use geom_point() instead if it were me. – qdread Jan 26 '21 at 15:24
  • Thanks for the explanation, however I am inclined to use a bar_plot in this case as it is a comparison with other data. But I understand the misleading behind. – Ecg Jan 26 '21 at 15:36

0 Answers0