0

Good Afternoon,

I'm trying to re-create a stacked bar graph found on slide 6 of Sunlight Foundations data visualization guide (https://github.com/amycesal/dataviz-style-guide/blob/master/Sunlight-StyleGuide-DataViz.pdf). I can't seem to get the two different categories of fruit to appear side by side on the same graph. Here is what I have so far.

fruit_data <- tribble(
  ~day, ~honey_crisp, ~granny_smith, ~clementines, ~navel,
  "Monday", 3, 12, 3, 10,
  "Tuesday", 3, 9, 2, 7,
  "Wednesday", 3, 18, 4, 12,
  "Thursday", 3, 15, 3, 10,
  "Friday", 3, 3, 5, 22
)

fruit_data_long <- 
  fruit_data %>%
  pivot_longer(cols = c(honey_crisp:navel), 
               names_to = "fruit", 
               values_to = "unit") %>%
  mutate(type = if_else(fruit == "honey_crisp" | fruit == "granny_smith", "apple", "orange")) %>%
  mutate(day = factor(day, levels = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")))

ggplot(data = fruit_data_long, 
       aes(x = day, y = unit, group = type)) +
  geom_col(aes(fill=fruit), stat="identity", position = "stack")

Any ideas?

Mishalb
  • 153
  • 7
  • you will find plenty of ideas in this discussion – tjebo Feb 12 '21 at 13:52
  • With the changes I got from your suggestion I now have the different fruits sitting side by side for each day (good), but the different types of apples and oranges are still not distinguished from one another. ```ggplot(data = fruit_data_long, aes(x = day, y = unit, fill=type)) + geom_bar(stat="identity", position="dodge")``` – Mishalb Feb 12 '21 at 13:58

1 Answers1

0

You need another variable to specify the additional category, so one way is use the alpha (this returns a warning message about using alpha for discrete):

ggplot(data = fruit_data_long,
aes(x = day, y = unit, fill=type,alpha=fruit)) +   
geom_bar(stat="identity", position="dodge")

enter image description here

Or specify the colors manually and then use an interaction:

COLS = alpha(rep(c("#a1cae2","#cfc5a5"),each=2),rep(c(0.2,0.7),2))

ggplot(data = fruit_data_long,
aes(x = day, y = unit, fill=interaction(fruit,type))) +   geom_bar(stat="identity", position="dodge") +
scale_fill_manual(values=COLS) +
theme_bw()

enter image description here

StupidWolf
  • 45,075
  • 17
  • 40
  • 72