Mike
Mike

Reputation: 1141

ggplot log scale y axis messes up stat_summary bar plot

I've made a barplot with error bars in ggplot. I want to make the y-axis log scale, but it messes up the bars, and I can't work out why.

Here's a small sample of the data:

data <- structure(list(label = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L), .Label = c("CSF_raw", "CSF_supernatant", 
"CSF_pellet", "PC_raw", "PC_supernatant", "PC_pellet", "NC_raw", 
"NC_supernatant", "NC_pellet", "GPC", "GNC", "standard", "NC"
), class = "factor"), conc = c(`49` = 0, `50` = 22.137978797117, 
`51` = 0, `52` = 0.558357028761278, `53` = 35.0832980064114, 
`54` = 106.064331352197, `55` = 4.14765656994934, `56` = 1.16840050032707, 
`57` = 8.33529714053568, `58` = 0, `59` = 0, `60` = 0, `73` = 0, 
`74` = 0, `131` = 0.00015364395215365, `132` = 22.3641755644066, 
`133` = 0, `134` = 0.409025645808659, `135` = 40.8376939323448, 
`136` = 81.1807837353875, `137` = 6.38488684568018, `138` = 1.68909814271646, 
`139` = 7.61828609738757, `140` = 0, `141` = 0, `142` = 0, `155` = 0, 
`156` = 0.000140010408919781)), row.names = c(NA, -28L), class = c("tbl_df", 
"tbl", "data.frame"))

It works nicely if I plot with normal axes:

ggplot(data, aes(x=label, y=conc)) +
  stat_summary(fun = mean, geom = "bar") + 
  stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5) +
  geom_point() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

But when I introduce a log scale it messes up the plot, with bars floating rather than starting at 0!

ggplot(data, aes(x=label, y=conc)) +
  stat_summary(fun = mean, geom = "bar") + 
  stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5) +
  geom_point() +
  scale_y_continuous(trans='log2',
                     labels = scales::number_format(accuracy=0.01)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

Upvotes: 0

Views: 410

Answers (1)

TarJae
TarJae

Reputation: 78937

As stated in the comments this would be the correct output if you transform with log2. You could use pseudo_log

ggplot(data, aes(x=label, y=conc)) +
  stat_summary(fun = mean, geom = "bar") + 
  stat_summary(fun.data = mean_se, geom = "errorbar", colour="black", width=0.5) +
  geom_point() +
  scale_y_continuous(trans='pseudo_log',
                     labels = scales::number_format(accuracy=0.01)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

enter image description here

Upvotes: 2

Related Questions