-
-
Save jsonbecker/0cc702804512fdf29c7f9067adfc17d0 to your computer and use it in GitHub Desktop.
library(ggplot2) | |
df <- data.frame(type = c(rep('Elementary', 2), rep('Middle', 2), rep('High', 2)), | |
included = rep(c('included','excluded'),3), | |
dollars = c(1000, 2500, 4000, 1000, 3000, 2800)) | |
ggplot(data = df, aes(type, dollars, fill = included)) + | |
geom_bar(position = 'fill', stat = 'identity') + | |
geom_text() |
Not sure exactly what your desired output is, but this is a start:
library(ggplot2)
df <- data.frame(type = c(rep('Elementary', 2), rep('Middle', 2), rep('High', 2)),
included = rep(c('included','excluded'),3),
dollars = c(1000, 2500, 4000, 1000, 3000, 2800))
ggplot(data = df, aes(x=type, y=dollars, fill = included, label=type)) +
geom_bar(position = 'fill', stat = 'identity') +
geom_text(position='fill')
...or make both position = "stack"
if you want the bars to not be scaled to proportions. Either way it was the different default position settings bar/text.
@noamross So my issue is I want the percentages to be the labels, not the type. While your code works, it provides the wrong text and I can't set the "label" to the proportions seemingly produced by position = 'fill'
@joranE thanks, but the primary motivation was finding out after 6 years of using ggplot2 that position = 'fill'
existed and I didn't have to use prop.table()
like I used to.
The closest you can get would be:
ggplot(data = df, aes(x=type, y=dollars, fill = included, label=type)) +
geom_bar(position = 'fill', stat = 'identity') +
geom_text(position='fill', aes(label = ..y..))
But you can't display the percentage that way because the filling happens after geom_text
has been given the data.
It's not so hard to do with dplyr though:
library(ggplot2)
library(dplyr)
df <- data.frame(
type = c(rep('Elementary', 2), rep('Middle', 2), rep('High', 2)),
included = rep(c('included','excluded'),3),
dollars = c(1000, 2500, 4000, 1000, 3000, 2800)
)
df %>%
group_by(type) %>%
mutate(prop = dollars / sum(dollars)) %>%
ggplot(aes(type, prop, fill = included)) +
geom_col() +
geom_text(aes(label = scales::percent(prop)), position = position_stack(vjust = 0.5))
Which has the additional advantage that you can easily check the computation.
@hadley thanks I really appreciate the response. The latter way with dplyr
is generally how I've done this, but I was hoping my discovery of position = 'fill'
meant I could remove that step. Oh well!
Another option is to leverage ggplotly()
's ability to return data after statistics have been applied:
https://cpsievert.github.io/plotly_book/extending-ggplotly.html#leveraging-statistical-output
Note: remove
geom_text()
and it makes the right graph. Butgeom_text
wants a label, and doesn't likedollars
ory
which are my best guesses as to what should go there.