简体   繁体   English

ggplot具有不同数量x轴类别但条形宽度恒定的堆叠条形图

[英]ggplot Stacked bar charts with different number of x-axis categories, but constant bar width

I have generating multiple stacked bar charts for a report. 我已经为报告生成了多个堆积的条形图。 Each chart represents a different region and may have different number of x-axis categories. 每个图表代表一个不同的区域,并且可能具有不同数量的x轴类别。 For a consistent visual between different charts I wish ensure that the width of the bars are constant between separate charts. 为了使不同图表之间具有一致的视觉效果,我希望确保各个图表之间的条形宽度保持恒定。 I currently haven't been able to do this as charts with fewer x-axis categories have wider bars. 我目前无法执行此操作,因为X轴类别较少的图表的条形较宽。 It seems a combination of dodge and width might work for standard bar charts, but this does not work for stacked bars. 似乎dodgewidth的组合可能适用于标准条形图,但不适用于堆叠的条形图。 Note that there are 100s of possible x-axis categories, though only 3 to 6 (max 6) are actually shown on each chart. 请注意,尽管每个图表上实际上只显示3到6(最大6),但可能有100多种x轴类别。 Does anyone know of a means of specifying a constant bar width which could then be applied to all charts? 有谁知道指定恒定条形宽度的方法,然后将其应用于所有图表?

One thought I had for regions with less than 6 categories, was to add additional empty categories to the right end of the chart (bringing total to 6) and then somehow set the plot panel width so as to cut these last empty placeholders off. 对于具有少于6个类别的区域,我的一个想法是在图表的右端添加其他空类别(使总数增加到6),然后以某种方式设置绘图面板的宽度,以减少这些最后的空占位符。 I'm not sure how to implement the cutting off of the placeholders. 我不确定如何实现占位符的切断。

Here is a simple example of the basic problem. 这是基本问题的简单示例。

df1 <- data.frame(variable_name = c(rep('var1', 3),rep('var2',4), rep('var3', 3), rep('var4', 4)),
             var_value = c(1,4,2,4,3,6,2,1,6,3,1,3,2,4),
             value_type = c('A','B','C','A','B','C','D','B','C','D','B','D','E','F'))

df2 <- data.frame( variable_name = c(rep('var6',4), rep('var4', 4)),
             var_value = c(4,3,6,2,1,3,2,4),
             value_type = c('A','B','C','D','B','D','E','F'))

ggplot() + 
    geom_col(data = df1,
    aes(x = variable_name, y =var_value,fill = value_type), 
    width = 0.8)

ggplot() + 
    geom_col(data = df2,
    aes(x = variable_name, y =var_value,fill = value_type), 
    width = 0.8)

Ultimately bar width will be determined by the width at which you save the plot, but if you want to scale within the plot, just multiply width by the number of x values divided by the maximum possible: 最终,条形宽度将由保存绘图的宽度决定,但是,如果要在绘图中缩放,只需将width乘以x值的数量除以最大可能值即可:

library(ggplot2)

df1 <- data.frame(variable_name = c(rep('var1', 3), rep('var2', 4), rep('var3', 3), rep('var4', 4)),
                  var_value = c(1,4,2,4,3,6,2,1,6,3,1,3,2,4),
                  value_type = c('A','B','C','A','B','C','D','B','C','D','B','D','E','F'))

df2 <- data.frame(variable_name = c(rep('var6', 4), rep('var4', 4)),
                  var_value = c(4,3,6,2,1,3,2,4),
                  value_type = c('A','B','C','D','B','D','E','F'))

ggplot(df1, aes(x = variable_name, y = var_value, fill = value_type)) + 
    geom_col(width = 0.8 * length(unique(df1$variable_name)) / 6)

ggplot(df2, aes(x = variable_name, y = var_value, fill = value_type)) + 
    geom_col(width = 0.8 * length(unique(df2$variable_name)) / 6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM