简体   繁体   English

改变ggplot中的y轴比例

[英]change y axis scale in ggplot

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:我正在尝试在 r 中复制由 graphpad 绘制的图,但我在更改 y 轴时遇到问题,这是一个示例数据和我的绘图代码:

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.我的代码结果在顶部,但我想像底部的图像一样绘制它。

在此处输入图像描述

在此处输入图像描述

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:您可以将这些值转换为它们的对数,然后添加一个使所有对数值大于 0 的常数。然后适当地重新标记轴:

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))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM