我什至不知道如何描述我试图正确生成的情节,这不是一个好的开始。我将首先向您显示我的数据,然后尝试解释/显示包含该数据元素的图像。
我的资料:
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示例的一些内容,但是我不知道如何使它适用于我的数据集。我喜欢这里的pos v。neg酒吧;单个条形覆盖了两者及其颜色差异。这没有针对一个样本的条件分组,也没有区分条件的颜色编码额外层
我已经尝试了很多事情,但我做对了。我想我真的很挣扎,因为许多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)
这给出了:
你为什么不结盟!
我怀疑我的问题与我实际绘制的方式有关,或者我没有正确告诉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))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句