ggplot2中的条形图,宽度作为变量,条间间距甚至相等 [英] Bar graph in ggplot2 with width as a variable and even spacing between bars

查看:3107
本文介绍了ggplot2中的条形图,宽度作为变量,条间间距甚至相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图制作一个条形宽度映射到一个变量的堆积条形图;但我想我的酒吧之间的间距是恒定的。



有谁知道如何使棒之间的间距不变?



现在我得到了这个:

  p <-ggplot(dd,aes(variable,value。 )+ coord_flip()+ opts(ylab =)

p1 <-p + geom_bar(stat =identity)+ scale_fill_brewer( palette =Dark2)+ scale_fill_hue(l = 55,c = 55)

p2 <-p1 + opts(axis.title.x = theme_blank(),axis.title.y = theme_blank ))

p2



预先致谢。

这是我的数据顺便说一下(对于长而笨重的dput):

 >输出(dd)
结构(列表(变量=结构(c(1L,1L,1L,1L,1L,3L,
3L,3L,3L,3L,4L,4L,4L,4L, 4L,7L,7L,7L,7L,7L,2L,2L,
2L,2L,2L,6L,6L,6L,6L,6L,5L,5L,5L,5L,5L,9L, 9L,
9L,9L,8L,8L,8L,8L,8L)。标签= c(酒精和烟草,
健康和个人护理,服装,能源 ,娱乐和教育,
家庭,食物,运输,住房),class =factor,scores =结构(c(2.91,
5.31,10.08, 15.99,4.95,11.55,11.2,27.49,20.6),.Dim = 9L,.Dimnames = list(
c(酒精和烟草,服装,能源,食品,护理,
家庭,娱乐和教育,住房,交通
)))),价值x = c(2.91,2.91,2.91,2.91,2.91,5.31 ,5.31,
5.31,5.31,5.31,10.08,10.08,10.08,10.08,10.08,15.99,15.99,
15.99,15.99,15.99,4.95,4.95,4.95,4.95,4.95,11.55,11.55 ,
11.55,11.55,11.55,11.2,11.2,11.2,11.2,11.2, 27.49,27.49,
27.49,27.49,27.49,20.6,20.6,20.6,20.6,20.6),日期=结构(c(5L,
4L,3L,2L,1L,5L,4L,3L) ,2L,1L,5L,4L,3L,2L,1L,5L,4L,
3L,2L,1L,5L,4L,3L,2L,1L,5L,4L,3L,2L,1L,5L ,4L,3L,
2L,1L,5L,4L,3L,2L,1L,5L,4L,3L,2L,1L),.Label = c(1993-2001,
2001 - 2006\" 年, 2007 - 2010年, 2010-2011, 2012 1至5月),类= 因子),
value.y = C(2.1,2.5,7.6,21.7, 2.8,1.5,0.3,-4.1,-4.2,
4.7,3,16.9,1.9,32.8,23.9,3.2,4.6,11.3,8.9,12.9,
1.7,2,7.8,5.9, 10,1.9,2.1,5.6,2.2,9.9,1.4,1.3,
2.2,0.6,17.3,1.1,2.3,6.4,13.1,10,4.3,7.6,0.9,15.2,
20.5) ),.Names = c(variable,value.x,Date,value.y
),row.names = c(NA,-45L),class =data.frame )


解决方案

尝试#2。



我试图将ggplot2写成一个连续的范围作为分类。

 #手工计算的tmp数字。不知道如何编程
#这部分,但数学是
#last + half(previous_width)+ half(current_width)
#更改cumsum中的第一个数字以调整类别宽度

tmp < - c(2.91,7.02,14.715,27.75,38.22, 46.47,57.845,77.19,101.235)+ cumsum(rep(5,9))

  dd $ x.pos1<  -  rep(tmp,each = 5)
ggplot(dd,aes(x = x.pos1,y = value.y,fill = Date))+
geom_bar(aes (width = value.x),stat =identity,position =stack)+
scale_x_continuous(breaks = tmp,labels = levels(dd $ variable))+
coord_flip()



为了达到最佳效果,您可能需要调整文字大小。这是用 ... + opts(axis.text.y = theme_text(size = 12))


完成的

So I am trying to make a stacked bar graph with bar width mapped to a variable; but I want the spacing between my bars to be constant.

Does anyone know how to make the spacing constant between the bars?

Right now I've got this:

p<-ggplot(dd, aes(variable, value.y, fill=Date, width=value.x / 15))+ coord_flip() + opts(ylab="") 

p1<-p+ geom_bar(stat="identity") + scale_fill_brewer(palette="Dark2") + scale_fill_hue(l=55,c=55)

p2<-p1 + opts(axis.title.x = theme_blank(), axis.title.y = theme_blank())

p2

Thanks in advance.

Here's my data by the way (sorry for the long, bulky dput):

> dput(dd)
structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 3L, 
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 7L, 7L, 7L, 7L, 7L, 2L, 2L, 
2L, 2L, 2L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 9L, 9L, 9L, 
9L, 9L, 8L, 8L, 8L, 8L, 8L), .Label = c("Alcohol and Tobacco", 
"Health and Personal Care", "Clothing", "Energy", "Recreation and Education", 
"Household", "Food", "Transportation", "Shelter"), class = "factor", scores = structure(c(2.91, 
5.31, 10.08, 15.99, 4.95, 11.55, 11.2, 27.49, 20.6), .Dim = 9L, .Dimnames = list(
    c("Alcohol and Tobacco", "Clothing", "Energy", "Food", "Health and Personal Care", 
    "Household", "Recreation and Education", "Shelter", "Transportation"
    )))), value.x = c(2.91, 2.91, 2.91, 2.91, 2.91, 5.31, 5.31, 
5.31, 5.31, 5.31, 10.08, 10.08, 10.08, 10.08, 10.08, 15.99, 15.99, 
15.99, 15.99, 15.99, 4.95, 4.95, 4.95, 4.95, 4.95, 11.55, 11.55, 
11.55, 11.55, 11.55, 11.2, 11.2, 11.2, 11.2, 11.2, 27.49, 27.49, 
27.49, 27.49, 27.49, 20.6, 20.6, 20.6, 20.6, 20.6), Date = structure(c(5L, 
4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 
3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L, 
2L, 1L, 5L, 4L, 3L, 2L, 1L, 5L, 4L, 3L, 2L, 1L), .Label = c("1993-2001", 
"2001-2006", "2007-2010", "2010-2011", "2012 Jan - May"), class = "factor"), 
    value.y = c(2.1, 2.5, 7.6, 21.7, 2.8, 1.5, 0.3, -4.1, -4.2, 
    4.7, 3, 16.9, 1.9, 32.8, 23.9, 3.2, 4.6, 11.3, 8.9, 12.9, 
    1.7, 2, 7.8, 5.9, 10, 1.9, 2.1, 5.6, 2.2, 9.9, 1.4, 1.3, 
    2.2, 0.6, 17.3, 1.1, 2.3, 6.4, 13.1, 10, 4.3, 7.6, 0.9, 15.2, 
    20.5)), .Names = c("variable", "value.x", "Date", "value.y"
), row.names = c(NA, -45L), class = "data.frame")

解决方案

Attempt # 2.

I'm tricking ggplot2 into writing a continuous scale as categorical.

# The numbers for tmp I calculated by hand. Not sure how to program 
# this part but the math is 
# last + half(previous_width) + half(current_width)
# Change the 1st number in cumsum to adjust the between category width

tmp <- c(2.91,7.02,14.715,27.75,38.22,46.47,57.845,77.19,101.235) + cumsum(rep(5,9))

dd$x.pos1 <- rep(tmp,each=5)
ggplot(dd,aes(x=x.pos1,y=value.y,fill=Date)) +
geom_bar(aes(width=value.x),stat="identity",position="stack") + 
scale_x_continuous(breaks=tmp,labels=levels(dd$variable)) + 
coord_flip()

For good measure you're probably going to want to adjust the text size. That's done with ... + opts(axis.text.y=theme_text(size=12))

这篇关于ggplot2中的条形图,宽度作为变量,条间间距甚至相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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