0
votes

I have a data frame that summarizes the number of people living in different household sizes for different age groups in both urban and rural areas. I'd like to visualize the population values in stacked plots so that the length of each section corresponds to the population counts living in that household size for an age group in either rural or urban areas. I have created these two plots so far but neither is what I want.

First effort: First Effort

Second effort: Second Effort

They are not what I want because they use the same colors for household sizes (s1:s7) in both urban and rural areas. Instead, I want to have 7 shades of green for rural areas and 7 shades of red for urban areas so that these two settings can be easily distinguished in the plot.

Is there any way in ggplot2 to show the values based on two criteria (urban vs. rural, household sizes)? If so, how could it be reflected in the legend?

Thanks so much in advance for your help!

1
Welcome to SO! You'll need to post enough data and code for a minimal reproducible example to get a full answer, but you'll likely need scale_fill_brewer or the like.alistaire

1 Answers

0
votes

Controlling the transparency (alpha) might give you the desirable plot? I am with @alistaire and next time you should give us the data or a chunk of reproducible data generating code.

Below is my solution

require(ggplot2)
# Simulate some data
n <- 1000
set.seed(1234)
df <- data.frame(urban = as.factor(runif(n)<0.3), 
             hsize = as.factor(sample(1:7,n,replace = TRUE)),
             age = as.factor(sample(1:20,n,replace = TRUE, prob = 1/(sqrt(abs(1:20-10))+1)) ))

c <- ggplot(df, aes(age,fill = urban, alpha = hsize))
c <- c + geom_bar()
c

And the result looks like this:

a possible solution