Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R ggplot2中的条形图,宽度为变量,条间距为偶数_R_Graph_Plot_Ggplot2 - Fatal编程技术网

R ggplot2中的条形图,宽度为变量,条间距为偶数

R ggplot2中的条形图,宽度为变量,条间距为偶数,r,graph,plot,ggplot2,R,Graph,Plot,Ggplot2,所以我试图制作一个堆叠条形图,条形图的宽度映射到一个变量;但我希望我的酒吧之间的间距是恒定的 有人知道如何使杆之间的间距保持恒定吗 现在我有了这个: 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"

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

有人知道如何使杆之间的间距保持恒定吗

现在我有了这个:

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
对于分类或“离散”比例-您可以调整宽度,但它需要介于0和1之间。你的值.x等于1,因此重叠。您可以使用“缩放”软件包中的“重缩放”来快速调整,以便条形图的“类别内宽度”代表其他变量(在本例中为值.x)

玩重新缩放的最佳“视图”更改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")
尝试#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,您可能需要考虑使用+FACETHYGRADE(~DATE)。感谢你的回答,但我实际上是在试图阻止重叠,而不是通过重新调整条的宽度(我需要较大的宽度差,以便视觉效果),我希望条之间的空白量保持不变。这可以做到吗?我也知道在某些情况下,刻面网格是一个更好的选择,但在这种情况下,年份的值最终会达到一个最终值,因此就我而言,原始格式是好的。对于分类变量,我不相信有一种方法可以唯一地调整刻度的每个元素的宽度。虽然在0.91中对scale_x_discrete()进行了一些调整,所以也许在ggplot2邮件列表中更好地回答您的问题。关于最佳视图,如果您想将它们放在一起,但仍然希望在类别之间进行比较,那么在每个栏上放置一个标签可能会有所帮助。

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