r - ggplot2: Drop unused factors in a faceted bar plot but not have differing bar widths between facets -
df <- structure(list(id = structure(c(1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 3l, 3l, 3l, 3l, 3l, 3l, 4l, 4l, 4l, 4l, 4l, 4l, 5l, 5l, 5l, 5l, 5l, 5l, 6l, 6l, 6l, 6l, 6l, 6l, 7l, 7l, 7l), .label = c("1", "2", "3", "4", "5", "6", "7"), class = "factor"), type = structure(c(1l, 2l, 3l, 4l, 5l, 1l, 2l, 3l, 4l, 5l, 6l, 1l, 2l, 3l, 4l, 5l, 6l, 1l, 2l, 3l, 4l, 5l, 6l, 1l, 2l, 3l, 4l, 5l, 6l, 1l, 2l, 3l, 4l, 5l, 6l, 1l, 2l, 3l), .label = c("1", "2", "3", "4", "5", "6", "7", "8"), class = "factor"), time = structure(c(2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l, 1l, 1l, 1l, 2l, 2l, 2l, 2l, 2l, 2l, 1l, 1l, 1l), .label = c("1", "5", "15"), class = "factor"), val = c(0.937377670081332, 0.522220720537007, 0.278690102742985, 0.967633064137772, 0.116124767344445, 0.0544306698720902, 0.470229141646996, 0.62017166428268, 0.195459847105667, 0.732876230962574, 0.996336271753535, 0.983087373664603, 0.666449476964772, 0.291554537601769, 0.167933790013194, 0.860138458199799, 0.172361251665279, 0.833266809117049, 0.620465772924945, 0.786503327777609, 0.761877260869369, 0.425386636285111, 0.612077651312575, 0.178726130630821, 0.528709076810628, 0.492527724476531, 0.472576208412647, 0.0702785139437765, 0.696220921119675, 0.230852259788662, 0.359884874196723, 0.518227979075164, 0.259466265095398, 0.149970305617899, 0.00682218233123422, 0.463400925742462, 0.924704828299582, 0.229068386601284)), .names = c("id", "type", "time", "val"), row.names = c(na, -38l), class = "data.frame")
if create following plot:
ggplot(df, aes(x=id, y=val, fill=type)) + facet_wrap(~ time, ncol=1) + geom_bar(position="stack") + coord_flip()
i decide ideally supress factors being shown in facet don't have data. have referenced various questions , answers scale="free"
method way go (as opposed drop=true
drop empty facets corresponding unused values in time
), next:
ggplot(df, aes(x=id, y=val, fill=type)) + facet_wrap(~time, ncol=1, scale="free") + geom_bar(position="stack") + coord_flip()
my question how prevent rescaling of bars occurs facet has 4 bars vs. facet 3 bars. effect subtle in contrived example, worse actual data. ideal output have bottom facet id factors 1,4, , 6 on vertical axis bars having same width top facet, , overall vertical dimension of facet reduced.
bonus points if can me why counts stacked instead of numeric values (fixed now)
bounty update:
as mentioned in followup question looks better solution involve use of ggplot_build
, ggplot_table
, modifying gtable object. i'm pretty sure figure out given time, i'm hoping bounty might motivate else me out. koshke has posted examples of this.
how this:
df$w <- 0.9 df$w[df$time == 5] <- 0.9 * 3/4 ggplot(df, aes(x=id, y=val, fill=type)) + facet_wrap(~time, ncol=1, scale="free") + geom_bar(position="stack",aes(width = w),stat = "identity") + coord_flip()
not sure if got arithmetic right there, idea.
Comments
Post a Comment