0

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!

alistaire
  • 42,459
  • 4
  • 77
  • 117
Hamid Z
  • 13
  • 2
  • 2
    Welcome to SO! You'll need to post enough data and code for a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) to get a full answer, but you'll likely need `scale_fill_brewer` or the like. – alistaire Mar 07 '16 at 21:22

1 Answers1

0

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

Bayesric
  • 329
  • 3
  • 13
  • Thanks so much Eric. You guys are right. I should have provided more details. Your output is exactly what I am looking for. But the plot does not show up when I run your code. – Hamid Z Mar 08 '16 at 16:42
  • @HamidZ You are welcome! Is there any error msg? Or maybe you did not tell R to output the plot `c`? Try run `c` in your console? – Bayesric Mar 08 '16 at 19:07
  • I made a mistake somewhere else. Now it works. Thanks! – Hamid Z Mar 08 '16 at 19:17
  • Awesome -- feel free to "accept" (click on tickmark) and/or "upvote" (click on up-triangle) as is common here on StackOverflow. – Bayesric Mar 08 '16 at 19:28