【问题标题】:Bar graph in ggplot2 with width as a variable and even spacing between barsggplot2中的条形图,宽度为变量,条形之间的间距均匀
【发布时间】:2012-07-05 22:51:57
【问题描述】:

所以我正在尝试制作一个堆积条形图,其条形宽度映射到一个变量;但我希望条形之间的间距保持不变。

有谁知道如何使条之间的间距保持不变?

现在我有这个:

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

提前致谢。

顺便说一下,这是我的数据(对于冗长而庞大的数据感到抱歉):

> 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")

【问题讨论】:

  • 您可能需要考虑使用 + facet_grid(~Date)。它使图表更具可读性。

标签: r graph plot ggplot2


【解决方案1】:

对于分类或“离散”比例 - 您可以调整宽度,但它需要介于 0 和 1 之间。您的 value.x 将其置于 1 之上,因此重叠。您可以使用 scales 包中的 rescale 来快速调整它,以便条形的类别内宽度代表一些其他变量(在本例中为 value.x)

install.packages("scales")
library(scales) 
ggplot(dd,aes(x=variable,y=value.y,fill=Date)) +
geom_bar(aes(width=rescale(value.x,c(0.5,1))),stat="identity",position="stack")' +
coord_flip()

玩重新缩放以获得最佳“视图”更改 0.5 到 0.25...等等。

就个人而言,我认为这样的内容更丰富:

ggplot(dd,aes(x=variable,y=value.y,fill=Date)) +
geom_bar(aes(width=rescale(value.x,c(0.2,1))),stat="identity") +
coord_flip() + facet_grid(~Date) + opts(legend.position="none")

【讨论】:

  • 感谢您的回复,但我实际上是在尝试停止重叠,而不是通过重新调整条形的宽度(我需要较大的宽度差异才能使其具有视觉效果),我' m 希望强制条形之间的空白空间量保持不变。这个可以吗?
  • 我也看到在某些情况下分面网格会是一个更好的选择,但在这种情况下,年份的值最终会产生最终值,因此对于我的目的来说,原始格式是好的。跨度>
  • 对于分类变量,我不相信有办法为刻度的每个元素唯一地调整宽度。虽然,在 0.91 中对 scale_x_discrete() 进行了一些调整,所以也许您的问题会在 ggplot2 邮件列表中得到更好的回答。关于最佳视图,如果您要将它们放在一起但仍想在类别之间进行比较,那么在每个条上放置一个标签可能会有所帮助。
【解决方案2】:

尝试#2。

我在欺骗 ggplot2 将连续尺度写成分类。

# 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 &lt;- 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)) 完成此操作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多