Created
December 1, 2016 04:01
-
-
Save jsonbecker/0cc702804512fdf29c7f9067adfc17d0 to your computer and use it in GitHub Desktop.
How do I get labels on this plot?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
@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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The closest you can get would be:
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:
Which has the additional advantage that you can easily check the computation.