-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: geom_text in middle of stacked bar_plot not in the middle (v2.1.0.9001) #1821
Comments
The dataset will need to be arranged in the reverse order of the
|
That's a possibility, of course, but a bad one at that, IMHO. Because reversing the order of the grouping of the bars under the hood without reversing the order of the labels is a breaking change. After all the example from the geom_text help file is working in v2.1 and will break, as it stands, in v2.2, and so will all similar bar charts created with ggplot2 v2.1 and before. So is there a chance to adapt geom_text to the new grouping order? |
One possible resolution would be a new position adjustment that "stacks" text like this, including an optional parameter (0-1) that lets you control where the text appears within the rectangular region. That wouldn't fix existing code the relies on the specific stacking order, but it would provide a simple fix. |
Here's a quick implementation: #' @example
#' df <- data.frame(
#' x = factor(c(1, 1, 2, 2)),
#' y = c(1, 3, 2, 1),
#' grp = c("a", "b", "a", "b")
#' )
#'
#' ggplot(data = df, aes(x, y, fill = grp, label = y)) +
#' geom_col() +
#' geom_text(position = "stack_point")
position_stack_point <- function(vjust = 0.5) {
ggproto(NULL, PositionStackPoint,
vjust = vjust
)
}
PositionStackPoint <- ggproto("PositionStackPoint", Position,
required_aes = c("x", "y"),
vjust = 0.5,
setup_params = function(self, data) {
list(vjust = self$vjust)
},
compute_layer = function(self, data, params, panel) {
data <- data[order(data$x, -data$group), ]
data$y <- ave(data$y, data$x, FUN = function(x) cumsum(x) - ((1 - params$vjust) * x))
data
}
) |
I now think that this should be integrated with |
We could have a |
That's what I'm in the middle of implementing 😄 |
The placement of geom_text in the middle of a stacked bar_plot is off. When I run the example from the help file of
geom_text
I get:I suppose this is due to "
position_stack()
andposition_fill()
now stack values in the reverse order of the grouping, which makes the default stack order match the legend."The text was updated successfully, but these errors were encountered: