gpt4 book ai didi

r - geom_bar ggplot2 堆叠、分组的带正值和负值的条形图 - 金字塔图

转载 作者:行者123 更新时间:2023-12-04 23:00:03 34 4
gpt4 key购买 nike

我什至不知道如何描述我试图正确生成的情节,这不是一个好的开始。我将首先向您展示我的数据,然后尝试解释/展示包含数据元素的图像。

我的数据:

   strain condition count.up count.down
1 phbA balanced 120 -102
2 phbA limited 114 -319
3 phbB balanced 122 -148
4 phbB limited 97 -201
5 phbAB balanced 268 -243
6 phbAB limited 140 -189
7 phbC balanced 55 -65
8 phbC limited 104 -187
9 phaZ balanced 99 -28
10 phaZ limited 147 -205
11 bdhA balanced 246 -159
12 bdhA limited 143 -383
13 acsA2 balanced 491 -389
14 acsA2 limited 131 -295

我有七个样本,每个样本有两种情况。对于这些样本中的每一个,我都有下调的基因数量和上调的基因数量(count.down 和 count.up)。

我想绘制此图,以便对每个样本进行分组;所以 phbA 平衡被避开了 phbA 限制。每个条形图的正侧都有一部分(代表 count.up #),图的负侧有一部分(代表 count.down #)。

我希望“平衡”条件下的条形为一种颜色,而“限制”条件下的条形为另一种颜色。理想情况下,每种颜色会有两个渐变(一个用于 count.up 和一个用于 count.down),只是为了在条的两个部分之间产生视觉差异。

一些包含我试图整合的元素的图像:

我还尝试应用此 stackoverflow 示例的一些部分,但我不知道如何使其适用于我的数据集。
I like the pos v. neg bars here; a single bar that covers both, and the colour differentiation of it. This does not have the grouping of conditions for one sample, or the colour coding extra layer that differentiates condition

我已经尝试了很多东西,但我不能做对。我想我真的很挣扎,因为很多 geom_bar 示例都使用计数数据,该图会自行计算,而我正在为其提供直接计数数据。当我转移到 stat= "identity" 时,我似乎无法在我的代码中成功地进行这种区分。然后一切都会变得困惑。任何想法或建议将不胜感激!

使用建议的链接:
所以我一直在用它作为模板,但我被卡住了。
df <- read.csv("countdata.csv", header=T) 
df.m <- melt(df, id.vars = c("strain", "condition"))
ggplot(df.m, aes(condition)) + geom_bar(subset = ,(variable == "count.up"), aes(y = value, fill = strain), stat = "identity") + geom_bar(subset = ,(variable == "count.down"), aes(y = -value, fill = strain), stat = "identity") + xlab("") + scale_y_continuous("Export - Import",formatter = "comma")

当我尝试运行 ggplot 行时,它返回一个错误:找不到函数“.”。我意识到我没有安装/加载 dplyr,所以我这样做了。
然后我玩了很多,最终想出了:
library(ggplot2)
library(reshape2)
library(dplyr)
library(plyr)

df <- read.csv("countdata.csv", header=T)
df.m <- melt(df, id.vars = c("strain", "condition"))

#this is what the df.m looks like now (if you look at my initial input df, I just changed in the numbers in excel to all be positive). Included so you can see what the melt does
df.m =read.table(text = "
strain condition variable value
1 phbA balanced count.up 120
2 phbA limited count.up 114
3 phbB balanced count.up 122
4 phbB limited count.up 97
5 phbAB balanced count.up 268
6 phbAB limited count.up 140
7 phbC balanced count.up 55
8 phbC limited count.up 104
9 phaZ balanced count.up 99
10 phaZ limited count.up 147
11 bdhA balanced count.up 246
12 bdhA limited count.up 143
13 acsA2 balanced count.up 491
14 acsA2 limited count.up 131
15 phbA balanced count.down 102
16 phbA limited count.down 319
17 phbB balanced count.down 148
18 phbB limited count.down 201
19 phbAB balanced count.down 243
20 phbAB limited count.down 189
21 phbC balanced count.down 65
22 phbC limited count.down 187
23 phaZ balanced count.down 28
24 phaZ limited count.down 205
25 bdhA balanced count.down 159
26 bdhA limited count.down 383
27 acsA2 balanced count.down 389
28 acsA2 limited count.down 295", header = TRUE)

这按应变绘制,两种条件下的count.up 和count.down 值
ggplot(df.m, aes(strain)) + geom_bar(subset = .(variable == "count.up"), aes(y = value, fill = condition), stat = "identity") + geom_bar(subset = .(variable == "count.down"), aes(y = -value, fill = condition), stat = "identity") + xlab("") 

#this adds a line break at zero
labels <- gsub("20([0-9]{2})M([0-9]{2})", "\\2\n\\1",
df.m$strain)


#this adds a line break at zero to improve readability
last_plot() + geom_hline(yintercept = 0,colour = "grey90")

我无法开始工作的一件事(不幸的是)是如何在每个条形框中显示代表“值”的数字。我已经得到了要显示的数字,但我无法将它们放在正确的位置。我要疯了!

我的数据和上面一样;这是我的代码所在的位置

我已经查看了大量使用 geom_text 在躲避图上显示标签的示例。我一直无法成功实现。我得到的最接近的如下 - 任何建议将不胜感激!
library(ggplot2)
library(reshape2)
library(plyr)
library(dplyr)
df <- read.csv("countdata.csv", header=T)
df.m <- melt(df, id.vars = c("strain", "condition"))
ggplot(df.m, aes(strain), ylim(-500:500)) +
geom_bar(subset = .(variable == "count.up"),
aes(y = value, fill = condition), stat = "identity", position = "dodge") +
geom_bar(subset = .(variable == "count.down"),
aes(y = -value, fill = condition), stat = "identity", position = "dodge") +
geom_hline(yintercept = 0,colour = "grey90")

last_plot() + geom_text(aes(strain, value, group=condition, label=label, ymax = 500, ymin= -500), position = position_dodge(width=0.9),size=4)

这给出了:

enter image description here

你为什么不对齐!

我怀疑我的问题与我实际绘制的方式有关,或者我没有正确地告诉 geom_text 命令如何定位自己的事实。有什么想法吗?

最佳答案

尝试这个。就像用两个语句(一个表示肯定,一个表示否定)来定位条形一样,以相同的方式定位文本。然后,使用 vjust 微调它们的位置(在栏内或栏外) .此外,数据框中没有“标签”变量;我假设标签是 value .

library(ggplot2)

## Using your df.m data frame
ggplot(df.m, aes(strain), ylim(-500:500)) +
geom_bar(data = subset(df.m, variable == "count.up"),
aes(y = value, fill = condition), stat = "identity", position = "dodge") +
geom_bar(data = subset(df.m, variable == "count.down"),
aes(y = -value, fill = condition), stat = "identity", position = "dodge") +
geom_hline(yintercept = 0,colour = "grey90")


last_plot() +
geom_text(data = subset(df.m, variable == "count.up"),
aes(strain, value, group=condition, label=value),
position = position_dodge(width=0.9), vjust = 1.5, size=4) +
geom_text(data = subset(df.m, variable == "count.down"),
aes(strain, -value, group=condition, label=value),
position = position_dodge(width=0.9), vjust = -.5, size=4) +
coord_cartesian(ylim = c(-500, 500))

enter image description here

关于r - geom_bar ggplot2 堆叠、分组的带正值和负值的条形图 - 金字塔图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38268741/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com