ggplot2;根据原始计数数据创建箱线图

时间:2019-06-07 00:23:11

标签: r ggplot2 data-visualization

我正在尝试从一个数据帧在ggplot2中创建一个箱形图,其中包含有关来自多个样本的计数数据的信息。就我而言,对于6个样本中的每个样本,每个基因都记录有计数。

所以它看起来像这样:

df <- data.frame(sample(c(1:100), 20, replace = T), sample(c(1:100), 20, replace = T),
                 sample(c(1:100), 20, replace = T), sample(c(1:100), 20, replace = T),
                 sample(c(1:100), 20, replace = T), sample(c(1:100), 20, replace = T))
names(df) <- paste0("Sample-", c(1:6))
rownames(df) <- paste0("Gene-", c(1:20))

这是我尝试过的:

bp <- ggplot(df, aes(x = names(df), y = )) + geom_boxplot()

但是我有0个想法为y值输入什么。从字面上看没有任何线索。我敢肯定我什至没有正确指示x轴。对于这个非常基本的问题,我将不胜感激。对于这个简单的问题,我感到抱歉。

2 个答案:

答案 0 :(得分:2)

ggplot在数据采用整齐的“长”格式而不是“宽”格式时效果最好。您可以使用tidyr::gather将样本放入一列,并将其值放入另一列:

library(tidyverse)

set.seed(1001) # for reproducible example data
# generate df here as in your question

df %>% 
  gather(Sample, Count) %>% 
  ggplot(aes(Sample, Count)) + 
  geom_boxplot()

结果:

enter image description here

答案 1 :(得分:1)

喜欢吗?

library(tidyverse)
df2 <- df %>% 
  gather(sample, value)

ggplot(df2, aes(sample, value)) +
  geom_boxplot() + coord_flip()

enter image description here