如何使用GGPLOT2在Y轴的两侧绘制Barplot?

发布于 2025-02-04 17:39:56 字数 3235 浏览 2 评论 0原文

我正在尝试在y-axis的任一侧绘制barplot(在“是”或“否”中至少存在),而不是堆叠“是”和“没有。首先,我将data.frame转换为更长的格式>计算平均值> GGPLOT2。

当我运行ggplot2代码时,barplot均用“是”和“否”一起堆叠在一起(请参见下面的屏幕截图)。我提供了用于绘制的示例数据。基本上,我想查看预期的情节。我还有其他步骤吗?请协助。

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

“”

实际上,我不希望两侧堆叠的barplot。该条应仅在“是”或“否”中存在。 “”

谢谢,谢谢,

toufiq

I am trying to draw a barplot on either side of the y-axis (atleast present in "YES" or "NO") instead of stacking both "YES" and "NO" together. Firstly I convert the data.frame to longer format > Calculated mean > ggplot2.

When I ran the ggplot2 code, the barplot is stacked with both "YES" and "NO" together (see screenshot below). I have provided example data that I am using to plot. Basically, I would like to view as expected plot. Is there any additional step that I am missing? Please assist.

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

Actually, I dont want the stacked barplot on either side. The bar should be either present in "YES" or "NO" only.

Thank You,

Toufiq

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2025-02-11 17:39:56

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)

“在此处输入图像描述”

You need to make the Norm_exp values negative when Sustained_CR is "NO":

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)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文