与ggplot堆积的酒吧阴谋 [英] stacked bar plot with ggplot

查看:108
本文介绍了与ggplot堆积的酒吧阴谋的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  totalleft 
1S 2S 3S我试图制作一个堆积条形图4S 12S 25S测试
A-000 5 0 10 10 0 NA A-000
A-001 10 8 10 NA NA NA A-001
A-002 5 3 10 10 10 NA A -002
A-003 2 0 10 9 0 10 A-003
A-004 5 4 10 10 10 NA A-004
A-005 5 3 10 10 10 NA A-005
A-006 8 7 NA 10 10 NA A-006
A-009 9 10 NA NA 10 10 A-009
A-015 NA 1 NA NA NA NA A-015
A-016 NA 0 10 NA 6 9 A-016
A-017 NA 0 NA NA 4 NA A-017
A-020 NA 1 NA NA NA NA A-020
A-025 NA 0 NA NA 0 NA A-025
A-025a NA 0 NA NA 10 NA A-025a
A-026 NA 9 10 NA 9 9 A-026
A- 027 NA 0 10 NA 2 9 A-027
A-028 NA 0 NA NA 1 NA A-028
A-030 NA 7 NA NA 8 8 A-030
B-000 0 0 7 8 0 0 B-000
B-05 6 4 0 9 NA 0 5 B-056
B-076 9 9 NA NA 10 10 B-076
B-099 6 5 10 NA 5 9 B-099
B-102 7 0 NA NA 0 10 B-102
B-105 NA 6 NA NA 6 B-105
B-119 7 8 10 10 NA NA B-119
然而,大部分文档都涉及到两个因素:一个用于沿X轴分割条,另一个用于分割每个条。我的问题是如何通过因子测试将X轴分开,然后用相应的行(即 1S,2S,3S,4S,12,25S )分隔每个小节。 。

因此,第一个栏将是 A-000 的栏,其中20%是一种颜色(对于 1S ,5 /(5 + 10 + 10)),第二个40%是另一种颜色( 3S < (5 + 10 + 10)),最后的40%是另一种颜色( 4S ,10 /(5 + 10 + 10) ))



我使用这个命令作为参考:

  ggplot(钻石,aes(清晰度,填充=切割))+ geom_bar()


http://docs.ggplot2.org/0.9.3.1/geom_bar .html#

解决方案

所以你需要重塑数据。
你想要一个堆积的barplot,所以你需要告诉ggplot有关变量1S,2S ...
和测试。

 #让我们融化数据
#library(reshape2)
data.plot.m< -melt(data.plot,id.vars =tests)#I stored your data.plot
data.plot.m $ variable< -gsub(X,,data.plot.m $ variable)
#as R不喜欢变量名开始用数字
#it自动添加一个'X',当
#we用read.table加载数据时,所以我们从熔化的数据中删除它

#现在我们绘制数据
ggplot(data.plot.m,aes(y = value,x = variable,fill = tests))+
geom_bar(stat =identity)


您会注意到图的顺序不同。
我们需要对您的变量重新排序:

  data.plot.m $ variable < -  factor(data。 plot.m $ variable,levels = unique(data.plot.m $变量))
#现在再绘制图
ggplot(data.plot.m,aes(y = value,x = variable,fill = test))+
geom_bar(stat =identity)



我刚刚意识到你想要这个

p>

  ggplot(data.plot.m,aes(y = value,x = tests,fill = variable))+ geom_bar(stat = 身份证)

并且x轴刻度标签已旋转

  ggplot(data.plot.m,aes(y = value,x = tests,fill = variable))+ geom_bar(stat =identity)+ theme (axis.text.x = element_text(angle = 90))


注意我如何切换x并填入

I'm trying to make a stacked bar plot with the following dataframe:

 totalleft
        1S 2S 3S 4S 12S 25S      tests
A-000   5  0 10 10   0  NA       A-000
A-001  10  8 10 NA  NA  NA       A-001
A-002   5  3 10 10  10  NA       A-002
A-003   2  0 10  9   0  10       A-003
A-004   5  4 10 10  10  NA       A-004
A-005   5  3 10 10  10  NA       A-005
A-006   8  7 NA 10  10  NA       A-006
A-009   9 10 NA NA  10  10       A-009
A-015  NA  1 NA NA  NA  NA       A-015
A-016  NA  0 10 NA   6   9       A-016
A-017  NA  0 NA NA   4  NA       A-017
A-020  NA  1 NA NA  NA  NA       A-020
A-025  NA  0 NA NA   0  NA       A-025
A-025a NA  0 NA NA  10  NA      A-025a
A-026  NA  9 10 NA   9   9       A-026
A-027  NA  0 10 NA   2   9       A-027
A-028  NA  0 NA NA   1  NA       A-028
A-030  NA  7 NA NA   8   8       A-030
B-000     0  0  7  8   0   0     B-000
B-056     4  0  9 NA   0   5     B-056
B-076     9  9 NA NA  10  10     B-076
B-099     6  5 10 NA   5   9     B-099
B-102     7  0 NA NA   0  10     B-102
B-105    NA  6 NA NA  NA   6     B-105
B-119     7  8 10 10  NA  NA     B-119

However, most of the documentation involves plotting against two factors: one for splitting up the bars along the X axis and the other for dividing up each bar. My questions is how split up the X axis by the factor test and then divide up each bar by the corresponding rows (i.e. 1S,2S,3S,4S,12,25S).

So, the first bar would be a bar for A-000, and 20% of the it would be one color (for the 1S, 5/(5+10+10)) and the second 40% would be another color (3S, 10/(5+10+10)) and the final 40% would be a another color (4S, 10/(5+10+10))

I'm using this command as a reference:

ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()

from this website: http://docs.ggplot2.org/0.9.3.1/geom_bar.html#

解决方案

So you need to reshape the data. You want a stacked barplot, so you will need to tell ggplot about variables 1S, 2S ... and tests.

#let's melt the data
#library(reshape2)
data.plot.m <-melt(data.plot, id.vars = "tests") #I stored your data in data.plot
data.plot.m$variable <-gsub("X","",data.plot.m$variable) 
#as R doesn't like variable names beginning with numbers,
#it adds an 'X' automatically when
#we load the data with read.table so we remove this from melted data

#now we plot the data
ggplot(data.plot.m,aes(y = value,x = variable,fill = tests)) +
geom_bar(stat = "identity")

You will notice the order of the plots are different. We will need to reorder your variable:

data.plot.m$variable <- factor(data.plot.m$variable, levels = unique(data.plot.m$variable))
#now plot again
ggplot(data.plot.m,aes(y = value,x = variable,fill = tests))+
geom_bar(stat = "identity") 

I just realized you wanted this instead

ggplot(data.plot.m,aes(y=value,x=tests,fill=variable))+geom_bar(stat="identity")

and with the x-axis tick labels rotated

ggplot(data.plot.m,aes(y=value,x=tests,fill=variable))+geom_bar(stat="identity") + theme(axis.text.x = element_text(angle=90))

Note how I switched x and fill

这篇关于与ggplot堆积的酒吧阴谋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆