繁体   English   中英

如何使用 ggplot2 在 y 轴的任一侧绘制条形图?

[英]How to draw barplot on either side of the y-axis using ggplot2?

我试图在y-axis的任一侧绘制barplot (至少存在于“YES”或“NO”中),而不是将“YES”和“NO”堆叠在一起。 首先,我将data.frame转换为更长的格式 > 计算mean > ggplot2。

当我运行barplot代码时,条形图与“YES”和“NO”堆叠在一起(见下面的截图)。 我提供了用于绘图的示例数据。 基本上,我想按预期查看情节。 我还缺少任何其他步骤吗? 请协助。

dput(Data)
structure(list(SampleID = c("Sample_1", "Sample_2", "Sample_3", 
                            "Sample_4", "Sample_5", "Sample_6"), Sustained_CR = c("NO", "NO", 
                                                                                  "NO", "YES", "YES", "YES"), Gene_A = c(2.245937679, 0, 0, 1.128343065, 
                                                                                                                         0, 0), Gene_CCC = c(83.09969414, 16.10799215, 122.2752332, 57.54549633, 
                                                                                                                                             13.12780895, 62.70159708), Gene_XED = c(6.737813038, 1.006749509, 
                                                                                                                                                                                     12.80978634, 1.128343065, 0, 2.508063883), RNA = c(580.5748901, 
                                                                                                                                                                                                                                        312.0923478, 347.0287571, 292.2408539, 355.7636226, 125.4031942
                                                                                                                                                                                     )), row.names = c("Sample_1", "Sample_2", "Sample_3", "Sample_4", 
                                                                                                                                                                                                       "Sample_5", "Sample_6"), class = "data.frame")

library(tidyverse)
Data_long <- Data[, -c(1)] %>% pivot_longer(cols = -"Sustained_CR",names_to="Gene_Symbols",values_to="Normalized expression values")
sum(is.na(Data_long))
head(Data_long)

## Calculate the mean based on the Gene_Symbols
Data_mean <- Data_long %>% group_by(Gene_Symbols, Sustained_CR) %>% summarize(Norm_exp=mean(`Normalized expression values`, na.rm = T))
print(Data_mean)
Gene_Symbols Sustained_CR Norm_exp
<chr>        <chr>           <dbl>
  1 Gene_A       NO              0.749
2 Gene_A       YES             0.376
3 Gene_CCC     NO             73.8  
4 Gene_CCC     YES            44.5  
5 Gene_XED     NO              6.85 
6 Gene_XED     YES             1.21 
7 RNA          NO            413.   
8 RNA          YES           258.   


ggplot(Data_mean, aes(reorder(Gene_Symbols, -Norm_exp), Norm_exp, fill = Sustained_CR)) +
  geom_bar(stat = "identity") +
  coord_flip() +
  xlab("Gene_Symbols")

实际上,我不希望两边都有堆积的条形图。 该栏应仅出现在“是”或“否”中。

谢谢你,

图菲克

Sustained_CR为“NO”时,您需要使Norm_exp值为负:

Data_mean %>% 
  mutate(Norm_exp = ifelse(Sustained_CR == "YES", Norm_exp, -Norm_exp)) %>%
  ggplot(aes(reorder(Gene_Symbols, -Norm_exp), Norm_exp, fill = Sustained_CR)) +
  geom_col(position = "dodge") +
  coord_flip() +
  xlab("Gene_Symbols") +
  scale_fill_brewer(palette = "Set1") +
  theme_minimal(base_size = 16) +
  scale_y_continuous(labels = abs)

在此处输入图像描述

暂无
暂无

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

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