R 如何绘制新旧数据的比较图

R 如何绘制新旧数据的比较图,r,plot,ggplot2,R,Plot,Ggplot2,我有一个名为mydf的数据帧。我有(count)个体中突变基因的数量。我将其与公布的数据进行比较(old_counts)。我想绘制此数据以与我的数据进行比较(并排显示是合适的)。任何在旧数据中没有值的基因,我想在绘图中将其标记为“新的”(例如,对于TTYRgene,我想在计数条下标记为新的) mydf gene counts old_counts GPT 13 12 TTYR 1 GTT

我有一个名为
mydf
的数据帧。我有(
count
)个体中突变基因的数量。我将其与公布的数据进行比较(
old_counts
)。我想绘制此数据以与我的数据进行比较(并排显示是合适的)。任何在旧数据中没有值的基因,我想在绘图中将其标记为“新的”(例如,对于
TTYR
gene,我想在
计数
条下标记为新的)

mydf

gene       counts       old_counts
GPT          13          12
TTYR         1           
GTT          2           5 
JUN          3           2
我们可以试试

m1 <- `colnames<-`(t(df1[-1]), df1$gene)
b1 <- barplot(m1, beside=TRUE, legend=TRUE, col = c('blue', 'green'))
axis(1, at = b1+0.2, labels = 
 c('', 'new')[c(is.na(m1))+1L], pos= -0.8, lwd.ticks=0, lty=0)

数据
df1ggplot2的替代方案:

其中:


对于要在打印和垂直x轴标签中使用百分比的情况:

# create a percentage value by group
df2$perc <- ave(df2$value, df2$variable, FUN = function(x) x/sum(x, na.rm = TRUE))

# set the break you want to use for the y-axis
brks <- c(0,0.2,0.4,0.6,0.8,1.0)

# load the 'scales' library (needed for the 'percent' function)
library(scales)

# create the plot
ggplot(df2, aes(x = gene, y = perc, fill = variable)) +
  geom_bar(stat="identity", position = position_dodge(width = 0.9), width = 0.7) +
  geom_text(aes(y = 0.02, label = lbl), hjust = 0, angle = 90, size = 4, position = position_dodge(width = 0.7)) +
  scale_y_continuous(breaks = brks, labels = percent(brks), limits = c(0,1)) +
  theme_minimal(base_size = 14) +
  theme(axis.text.x = element_text(angle = 90))
#按组创建百分比值

df2$perc我被你的问题弄糊涂了,这个例子没有帮助。请您进一步详细说明并提供更多的数据代码和您尝试过的内容好吗?您可以使用ggplot的geom_栏和道奇@Amsterll嗯,有一列是我的数据,另一列是旧数据。我想把每个基因的旧数据和新数据放在一起。如果任何基因的旧计数中没有值,我想在
计数中将该基因的条标为新的。谢谢。如果
计数
中没有值,如何在
旧计数
栏下方添加
缺失
(就像
新建
)?是的,这是正确的。我很想知道,如果计数没有值,但存在于旧计数中,我们如何添加
缺失的
。例如,如果我想为
JUN
条添加
missing
df1@MAPK,请检查更新是否有帮助。(刚刚注意到您在评论中提供了一个新的dput。)无论如何,我为“GTT”添加了缺少的一个。它也适用于您在示例中显示的带有“JUN”的示例。谢谢。如何将y轴设置为100?也就是说,如果所有值都以百分比和y轴为单位,则将标记为100%限制。如何垂直定位基因标签?@MAPK将
主题(axis.text.x=element\u text(angle=90))
添加到
ggplot
-code;我也更新了答案
df1 <- structure(list(gene = c("GPT", "TTYR", "GTT", 
 "JUN"), counts = c(13L, 
1L, 2L, 3L), old_counts = c(12L, NA, 5L, 2L)), 
.Names = c("gene", 
"counts", "old_counts"), class = "data.frame", 
row.names = c(NA, -4L))
# load needed libraries
library(reshape2)
library(ggplot2)

# set the order of the 'gene' variable if you don't want it to be plotted
# in alphabetical order, else you can skip this step
df1$gene <- factor(df1$gene, levels = c("GPT", "TTYR", "GTT", "JUN"))

# reshape the data
df2 <- melt(df1, "gene")

# create a variable with the labels
df2$lbl <- c(NA,"new","missing")[((is.na(df2$value) & df2$variable=="old_counts") + 1L) + 
                                   (is.na(df2$value) & df2$variable=="counts")*2]


# create the plot
ggplot(df2, aes(x = gene, y = value, fill = variable)) +
  geom_bar(stat="identity", position = position_dodge(width = 0.9), width = 0.7) +
  geom_text(aes(y = -1, label = lbl), size = 5, position = position_dodge(width = 0.7)) +
  theme_minimal(base_size = 14)
ggplot(df2, aes(x = gene, y = value, fill = variable)) +
  geom_bar(stat="identity", position = position_dodge(width = 0.9), width = 0.7) +
  geom_text(aes(y = 0.2, label = lbl), hjust = 0, angle = 90, size = 4, position = position_dodge(width = 0.7)) +
  theme_minimal(base_size = 14)
# create a percentage value by group
df2$perc <- ave(df2$value, df2$variable, FUN = function(x) x/sum(x, na.rm = TRUE))

# set the break you want to use for the y-axis
brks <- c(0,0.2,0.4,0.6,0.8,1.0)

# load the 'scales' library (needed for the 'percent' function)
library(scales)

# create the plot
ggplot(df2, aes(x = gene, y = perc, fill = variable)) +
  geom_bar(stat="identity", position = position_dodge(width = 0.9), width = 0.7) +
  geom_text(aes(y = 0.02, label = lbl), hjust = 0, angle = 90, size = 4, position = position_dodge(width = 0.7)) +
  scale_y_continuous(breaks = brks, labels = percent(brks), limits = c(0,1)) +
  theme_minimal(base_size = 14) +
  theme(axis.text.x = element_text(angle = 90))
df1 <- structure(list(gene = c("GPT", "TTYR", "GTT", "JUN"), counts = c(13L, 1L, 2L, NA), old_counts = c(12L, NA, 5L, 2L)), .Names = c("gene", "counts", "old_counts"), class = "data.frame", row.names = c(NA, -4L))