Home > front end >  change y axis scale in ggplot
change y axis scale in ggplot

Time:05-30

I'm trying to replicate a plot drawn by graphpad in r but I have a problem changing the y axis here is an example data and my plot codes:

library(ggplot2)
data <- data.frame(names = rep(factor(LETTERS[1:3])),
                   values = c(0.001, 0.02 ,0.95),
                   group = rep("A",3))


ggplot(data,
       aes(x = group,
           y = values,
           fill = names))   geom_bar(stat = "identity", position='stack')  
  scale_y_continuous(breaks = c(0.001,0.01,0.1,1), labels=c(0.001,0.01,0.1,1))

the result of my code is on top but I want to plot it like the image on the bottom.

enter image description here

enter image description here

CodePudding user response:

You can convert the values to their logs, then add a constant that makes all the log values greater than 0. Then re-label the axis appropriately:

data2 <- data %>% 
  mutate(logvalues = log10(values) - min(log10(0.0001)))

ggplot(data2[order(-data2$logvalues),], 
         aes(x = group, y = logvalues, fill = names))   
  geom_col(position = 'identity')  
  scale_y_continuous(breaks = 0:4, labels = ~ 10^(.x-4))

enter image description here

  • Related