如何使用GGPLOT2在Y轴的两侧绘制Barplot?
我正在尝试在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当
sustained_cr
为“ no”时,您需要使norm_exp
值为负面值:You need to make the
Norm_exp
values negative whenSustained_CR
is "NO":