Reputation: 1015
I'm using the gss_cat
tibble from forcats
(tidyverse
), and the first thing I decided to plot was race x income. So I did a bar plot, but it turns out the total of surveyed people is not the same for each race:
gss_cat %>%
filter(!rincome %in% c("No answer", "Don't know", "Not applicable")) %>%
ggplot(aes(x = race, fill = rincome))+
geom_bar(position = "stack")+
#coord_polar(theta = "y")+
theme_classic()+
scale_fill_viridis_d(direction = -1)
The same happens with a pie chart (so without that #
at coord_polar
):
So I thought I do
y = ..prop..
, which theoretically would use the proportion instead of the total, but this seems to make everything the same:
gss_cat %>%
filter(!rincome %in% c("No answer", "Don't know", "Not applicable")) %>%
ggplot(aes(x = race, y = ..prop.., fill = rincome))+
geom_bar(position = "stack")+
#coord_polar(theta = "y")+
theme_classic()+
scale_fill_viridis_d(direction = -1)
I then tried doing
group = race
, which in my experience works for geom_count
, but it didn't for this:
gss_cat %>%
filter(!rincome %in% c("No answer", "Don't know", "Not applicable")) %>%
ggplot(aes(x = race, y = ..prop.., fill = rincome, group = race))+
geom_bar(position = "stack")+
#coord_polar(theta = "y")+
theme_classic()+
scale_fill_viridis_d(direction = -1)
It just threw the
fill
out the window entirely. There was no warning.
Why is this happening and how can I change it? I didn't find anything on R Documentation, the ggplot2 homepage, this website – which is one of my favorites... I don't know where to go from here.
Upvotes: 1
Views: 373
Reputation: 39595
Try with position=fill
:
library(forcats)
library(ggplot2)
library(dplyr)
#Code
gss_cat %>%
filter(!rincome %in% c("No answer", "Don't know", "Not applicable")) %>%
ggplot(aes(x = race, fill = rincome))+
geom_bar(position = "fill")+
#coord_polar(theta = "y")+
theme_classic()+
scale_fill_viridis_d(direction = -1)
Output:
Upvotes: 1