ggplot2中的轴转换

时间:2019-01-23 22:39:24

标签: r ggplot2 mean boxplot

我正在尝试使用ggplot2创建箱形图,我想同时显示均值和中位数。当我将y轴转换为负对数10时,曲线图上显示的平均值会发生变化(X2和X3被翻转),并且它们是不正确的。

这是我的代码:

x <- read.table("stackoverflow.txt", sep="\t", header=T)

library(tidyr)

y <- x %>% gather(key, value, 2:6)

library(ggplot2)

z <- ggplot(y, aes(x = key, y = -log10(value), color = key)) + 
  geom_boxplot() + 
  theme_bw() + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1), 
        axis.text = element_text(size = 12), 
        axis.title = element_text(size = 14, face = "bold")) + 
  labs(x = "" , y = "-Log10(T)") +
  coord_cartesian(ylim = c(6, 13)) + 
  scale_y_continuous(breaks = seq(6, 13, 2))

z + stat_summary(fun.y = mean, geom = "point", shape = 18, size = 3,
                 show.legend = TRUE, color = "black") +
  geom_point(aes(shape = "mean"), alpha = 0) + 
  guides(shape = guide_legend(title = NULL, override.aes = list(alpha = 1)))

means <- aggregate(value ~ key, y, mean)

means

key       value
1  X1 1.142106e-09
2  X2 1.038333e-09
3  X3 7.831222e-10
4  X4 1.216502e-09
5  X5 1.321300e-09

我正在使用的文件以制表符分隔

Ratio   1           2           3           4           5    
0.01    1.56E-13    1.09E-13    1.22E-11    2.22E-12    1.79E-11   
0.02    3.98E-11    8.89E-12    2.59E-11    2.93E-11    4.88E-11   
0.03    1.96E-10    1.00E-10    1.02E-10    1.05E-10    1.90E-10
0.04    4.98E-10    2.46E-10    3.89E-10    6.68E-10    4.83E-10
0.05    7.35E-10    8.30E-10    6.59E-10    9.14E-10    9.62E-10
0.06    1.18E-09    1.25E-09    1.01E-09    1.51E-09    1.68E-09
0.07    2.05E-09    1.77E-09    1.48E-09    2.13E-09    2.13E-09
0.08    2.48E-09    2.39E-09    1.64E-09    2.62E-09    3.05E-09 
0.09    3.10E-09    2.75E-09    1.73E-09    2.97E-09    3.33E-09

1 个答案:

答案 0 :(得分:0)

我试图将另一列添加到y数据框中,且log10值为负 表示接缝正确并符合情节

y$l10val <- -log10(y$value)

z <- ggplot(data <- y, aes(key,-log10(value),color = key)) + 
  geom_boxplot() + 
  theme_bw() + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1), 
        axis.text = element_text(size = 12), 
        axis.title = element_text(size = 14, face = "bold")) + 
  labs(x = "" , y = "-Log10(Threshold)") +
  coord_cartesian(ylim = c(6, 13)) + 
  scale_y_continuous(breaks = seq(6, 13, 2))

z + stat_summary(fun.y = mean, geom = "point", shape = 18, size = 3,
                 show.legend = TRUE, color = "black") +
  geom_point(aes(shape = "mean"), alpha = 0) + 
  guides(shape = guide_legend(title = NULL, override.aes = list(alpha = 1)))

means <- aggregate(l10val ~ key, y, mean)
means