【问题标题】:Stacked and beside bar plot with percentage on y-axis在 y 轴上带有百分比的堆叠和旁边的条形图
【发布时间】:2013-05-18 18:41:12
【问题描述】:

我在 R 中有一个大约 7000 行的数据框。显示了我的数据框的 10 行-

TypeA   TypeB   Ct_for_typeA    Ct_for_typeB
code3   code2   m               n
code4   code1   m               p
code3   code7   p               n
code8   code6   n               n
code1   code3   m               p
code5   code8   n               o
code2   code1   o               p
code5   code5   p               m
code7   code4   o               m
code6   code1   m               o

第 1 列 (TypeA) 和第 2 列 (TypeB) 有 8 个不同的代码,从 code1 到 code8。第 3 列和第 4 列有 4 个不同的类别,即 m、n、o 和 p。我想在 x 轴上用 code1 到 code8 绘制条形图,在 y 轴上绘制“百分比”。这意味着 x 轴将有 8 对条形图,y 轴将显示代码的百分比,而且我想根据第 3 列(对于第 1 列)和第 4 列(对于第 2 列)用不同的颜色堆栈划分每个条形图.示例:

仅考虑 x 轴上的第一对代码,即代码 1。从以上 10 行中,我们可以看到“TypeA”中的 code1 为 10%,“TypeB”中为 30%。所以第一对有第一条直到 10% 和第二条直到 30%。现在将根据第 3 列划分第一对的第一条(堆叠颜色)。我们可以看到只有“m”和code1,颜色将是“m”(全部10%)但是对于“TypeB”中的code1,即第一对的第二条将分成20%,颜色为“p”和 10% 的颜色为“o”。

我已尝试使用“beside=F”堆叠颜色,它正在工作。这意味着如果我只有第一列和第三列,我可以轻松完成。但是包括第 2 列和第 4 列的第二条让我感到困惑。 我希望我的解释不会令人困惑。提前致谢。

编辑:在托马斯发表评论之后。

如果“my_frame”是超过 10 行的数据框。对于我使用的具有堆叠颜色的单个变量-

px=ggplot(my_frame,aes(x=TypeA,fill=Ct_for_typeA))+geom_bar()
print(px)

首先,这里我没有得到 y 轴上的百分比,其次,如何将“旁边”栏放在第二列,堆叠颜色在第四列。

【问题讨论】:

  • 正如我在原帖中提到的那样,我很容易用第一列和第三列制作条形图,但我没有在互联网上找到任何能让我走得更远的东西。
  • 所以向我们展示您迄今为止使用的代码,这样人们就不必再做所有的工作来帮助您了。

标签: r ggplot2


【解决方案1】:

目前,您展示了一种宽格式的数据。这意味着每个变量都是一列。 ggplot 比较喜欢长格式。

要在数据框中计数,您可以使用data.table 包。由于您的名称也称为代码,因此您无法轻松使用 reshape2 包中的 melt 函数。因此,通过data.table 绕道而行。

library(data.table)
test.df <- read.table("your.data", header=T, sep='\t')

# create a data table
test.dt <- as.data.table(test.df)

# here it would be possible to use melt, if your "codes" wouldn't be named identical

# count TypeA
 test.a.count.dt <- test.dt[, length(Ct_for_typeA), by="TypeA" ]
 test.a.count.dt
    TypeA V1
1: code1  1
2: code2  1
3: code3  2
4: code4  1
5: code5  2
6: code6  1
7: code7  1
8: code8  1

# do the same for TypeB
test.b.count.dt <- test.dt[, length(Ct_for_typeB), by="TypeB" ]

colnames(test.a.count.dt) <- c("code","count")
colnames(test.b.count.dt) <- c("code","count")

test.a.count.dt$type <- "TypeA"
test.b.count.dt$type <- "TypeB"


# fuse the two data sets
# this is a long data format that suits ggplot better
test.all.count.dt <- rbind(test.a.count.dt, test.b.count.dt)

colnames(test.all.count.dt) <- c("code","count","type")

# this can be plotted already, but it isn't relative
ggplot(data=test.all.count.dt, aes(code, count, fill=type)) + geom_bar(stat="identity", position="dodge")

# the detour to get relative counts
test.all.count.dt$relative <- apply(test.all.count.dt, 1, function(x){
 count<-x[2];
 type<-x[3];
 return(as.numeric(count)/sum(test.all.count.dt$type==type))
})

# finally plot your relative counts
ggplot(data=test.all.count.dt, aes(code, relative, fill=type)) +
  geom_bar(stat="identity", position="dodge")

ggplot 的geom_bar 已经具有stat=count 方法,但这仅绘制绝对数据。我找不到直接让geom_bar 返回相对值的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 2017-08-27
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2020-04-18
    相关资源
    最近更新 更多