首页 > 解决方案 > 在ggplot对数上更改X轴

问题描述

我正在尝试显示一个图表,显示一些数据的 log10 转换值。使用以下代码创建小提琴图时:

tt <- EditedDF1 %>% 
ggplot(aes(x=ct_marshallFAC, y=log10(uchl1_d1_pgml), fill = EditedDF1$ct_marshallFAC)) + 
geom_violin() +
geom_boxplot(width=0.1, outlier.shape = NA, fill="white") +
theme_classic() +
theme(axis.text.x = element_text(size= 20)) +
labs(x="", y= "Log UCHL1(pg/ml)") +
ylim(-3.5, 6)  +
theme(legend.position="none")+
scale_x_discrete(labels=c("I", "II", "III-IV", "V-VI")) +
geom_hline(yintercept = 1.568, colour = "red", linetype="dotted" )

我的问题是 y 轴给出的值是 -3、-2、-1、0,而我希望它显示的是 log10 等于的实际值。例如 -1 为 0.1,0 为 1,1 为 10,以此类推。

我选择显示对数转换的数据,因为我有多个数据面板。但是我觉得如果我在 y 轴上给出实际值,读者会更好地理解。

非常感谢。丹W

标签: rggplot2

解决方案


取出你的ylim(-3.5, 6)并使用 ggplotscale_y_log10代替那里的限制:

library(ggplot2)

EditedDF1 <- data.frame(ct_marshallFAC = rep(c("I", "II", "III-IV", "V-VI"),10), uchl1_d1_pgml =runif(40)*100)


tt <- EditedDF1 %>% 
  ggplot(aes(x=ct_marshallFAC, y=log10(uchl1_d1_pgml), 
             fill = EditedDF1$ct_marshallFAC)) + 
  geom_violin() +
  geom_boxplot(width=0.1, outlier.shape = NA, fill="white") +
  theme_classic() +
  theme(axis.text.x = element_text(size= 20)) +
  labs(x="", y= "UCHL1(pg/ml)") +
  theme(legend.position="none")+
  scale_x_discrete(labels=c("I", "II", "III-IV", "V-VI")) +
  geom_hline(yintercept = 1.568, colour = "red", linetype="dotted" ) +
  scale_y_log10(limits = c(exp(-3.5), exp(6)))

图形

编辑:我从来没有人告诉我对数刻度令人困惑......我只是使用你必须开始的东西并保留标签以反映这些值是对数的。


推荐阅读