You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm dealing with a figure where I would like to be able to dodge columns based on the group aesthetic while having the columns stay stacked on the fill aesthetic.
My intuition was that for geometries that use the group aesthetic, color/fill is assumed to be used as the group when it is not explicitly mapped to a variable. This seems to be the case with geom_line, for instance.
However, when using position_dodge and an explicitly mapped group, the bars appear to still be dodged by the group + fill variable.
Since the default position is stack, specifying the group as the replicate stacks the bars for each of the replicates of day 2 following the order of species-replicate: A-1, B-1, C-1, A-2, B-2, C-2. Fair enough, this is probably the expected and desired behaviour.
Dodged Behaviour
The issue arises when we attempt to dodge the groups, so that the height of the bars still represents the total counts in a single sample:
The group (replicates) have now been properly dodged/unstacked in day 2, but the previously stacked species all start now in the same x-y coords of the day-group instead of being stacked. Lowering the alpha a tad helps to see it better:
position_dodge2
The results with position_dodge2 are also problematic, since the width of the bars doesn't seem to be properly calculated (day 1 values become half as wide, while values for day 2 stay the same and overlap with values from the other days).
In any case, this completely unstacks the species/fill aesthetic, which is something we don't want even if bars' width was properly calculated.
The hacky solution
I thought that manually changing the x values (works in this case but would be a bit harder if the x variable was a Categorical, for instance) and generating a width variable to pass as an aesthetic manually setting the width of the day 2 values to .25 should allow me to make this work:
I actually managed to solve this while writing the issue with this hacky solution, but still, it feels like there might be some way to include this behaviour in some position variety? I don't know if this situation is common enough to justify generating a specific position_dodgestack or something like that, but what do you think?
The text was updated successfully, but these errors were encountered:
Yes this is a dodgestack or stackdodge situation. Though I do not think it would be common enough to warrant an implementation. Plus the resulting graphic has missing information! There is no guide for the two different split bars at location 2. You need to know about the replication to understand what is shown!
A more straight forward alternative to create new grouping variable in the data, with the appropriate x labels.
In most cases when you find yourself fighting with the plotting grammar, then the data has not been coded well. I think that is the case here.
I'm dealing with a figure where I would like to be able to dodge columns based on the
group
aesthetic while having the columns stay stacked on thefill
aesthetic.My intuition was that for geometries that use the
group
aesthetic, color/fill is assumed to be used as thegroup
when it is not explicitly mapped to a variable. This seems to be the case withgeom_line
, for instance.However, when using
position_dodge
and an explicitly mappedgroup
, the bars appear to still be dodged by thegroup + fill
variable.To show some examples with a toy dataset:
In this case, we have two different samples/replicates in day 2.
Default Behaviour
Since the default position is
stack
, specifying thegroup
as the replicate stacks the bars for each of the replicates of day 2 following the order of species-replicate:A-1, B-1, C-1, A-2, B-2, C-2
. Fair enough, this is probably the expected and desired behaviour.Dodged Behaviour
The issue arises when we attempt to dodge the groups, so that the height of the bars still represents the total counts in a single sample:
position_dodge
The
group
(replicates) have now been properly dodged/unstacked in day 2, but the previously stacked species all start now in the same x-y coords of the day-group instead of being stacked. Lowering thealpha
a tad helps to see it better:position_dodge2
The results with
position_dodge2
are also problematic, since the width of the bars doesn't seem to be properly calculated (day 1 values become half as wide, while values for day 2 stay the same and overlap with values from the other days).In any case, this completely unstacks the species/fill aesthetic, which is something we don't want even if bars' width was properly calculated.
The hacky solution
I thought that manually changing the
x
values (works in this case but would be a bit harder if thex
variable was a Categorical, for instance) and generating awidth
variable to pass as an aesthetic manually setting the width of the day 2 values to .25 should allow me to make this work:I actually managed to solve this while writing the issue with this hacky solution, but still, it feels like there might be some way to include this behaviour in some
position
variety? I don't know if this situation is common enough to justify generating a specificposition_dodgestack
or something like that, but what do you think?The text was updated successfully, but these errors were encountered: