0

I am a bit new with making nice plots in R. Now I would like to make stacked bar plots. Here is the code for the stacked bar plots:

longer_data = structure(list(question = 
c("HMI1_Speed_and_distance_control", 
"HMI2_Supercruise_Speed_and_distance_control", 
"HMI3_Speed_and_distance_control", "HMI4_Speed_and_distance_control", 
"HMI5_Speed_and_distance_control", "HMI6_Speed_and_distance_control", 
"HMI1_Speed_and_distance_control", 
"HMI2_Supercruise_Speed_and_distance_control", 
"HMI3_Speed_and_distance_control", 
"HMI4_Speed_and_distance_control"), response = c(3L, 3L, 1L, 1L, 2L,
 1L, 2L, 3L, 1L, 3L)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))
longer_data %>% 
  ggplot(aes(x = response)) +
    geom_bar()+
    facet_wrap(vars(question), ncol = 3) +
    labs(x = "Response (on a 1 to 5 scale)", y = "Number of respondents")

I would like that the three bars per figure are put on top of each other and not next to each other. How do I do that?

Thanks!

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • This is confusing because `position = "stack"` in `geom_bar` is the default. Can you share a little reproducible data? Call your data `pivoted_data` after you pivot it and give us `dput(pivoted_data[1:10, ])` for a copy/pasteable version of the first 10 rows of your data including all class and structure info. (Or choose a different illustrative subset). – Gregor Thomas Dec 07 '21 at 16:12
  • Thanks! However, I did that and nothing changes! Any other idea? – walkytalky Dec 07 '21 at 16:13
  • Call: dput(longer_data[1:10, ]) structure(list(question = c("HMI1_Speed_and_distance_control", "HMI2_Supercruise_Speed_and_distance_control", "HMI3_Speed_and_distance_control", "HMI4_Speed_and_distance_control", "HMI5_Speed_and_distance_control", "HMI6_Speed_and_distance_control", "HMI1_Speed_and_distance_control", "HMI2_Supercruise_Speed_and_distance_control", "HMI3_Speed_and_distance_control", "HMI4_Speed_and_distance_control"), response = c(3L, 3L, 1L, 1L, 2L, 1L, 2L, 3L, 1L, 3L)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame")) – walkytalky Dec 07 '21 at 16:18
  • I followed your advice. Is this what you mean? – walkytalky Dec 07 '21 at 16:23
  • That's good, I put it in your question and formatted the text. Now, to your goal---I see one bar in each facet for each Response, because you've got `x = response`. You say you want the bars stacked - do you want to use a fill color to tell the different response values apart, or something like that? – Gregor Thomas Dec 07 '21 at 16:27
  • And do you still want the facets, with one stacked bar per facet? Or do you maybe want `question` on the x-axis and no facets? – Gregor Thomas Dec 07 '21 at 16:29
  • Hi Thomas, many thanks. I really appreciate your help. To your question. Yes, I would like to set the different responses per category apart from each other. Thus, per question I have three response categories: Yes, No, and I don't know. Yes should be coloured in green, No in red, and I dont know in orange or purple. In this case we do not need any facets. So yes, the question should be put on the x-axis. – walkytalky Dec 07 '21 at 16:41

2 Answers2

0

I created a factor variable and closed the facet wrap

    longer_data$question<- factor(longer_data$question)                     
    
    longer_data %>% 
      ggplot(aes( x=response, fill=question)) +
      geom_bar(stat="count")+
     # facet_wrap(vars(question), ncol = 3) +
      labs(x = "Response (on a 1 to 5 scale)", y = "Number of respondents")

enter image description here

Rfanatic
  • 2,224
  • 1
  • 5
  • 21
0

I made guesses that 1 is Yes, 2 is No, and 3 is don't know, you should correct those if necessary.

longer_data %>% 
  mutate(response = factor(response, levels = 1:3, labels = c("Yes", "No", "Don't Know"))) %>%
  ggplot(aes(x = question, fill = response)) +
  geom_bar()+
  scale_fill_manual(values = c("Yes" = "forestgreen", "No" = "firebrick2", "Don't Know" = "purple")) +
  theme(axis.text.x = element_text(angle = -90, hjust = 0))

enter image description here

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thank you so so much! I am running this code now and dont understand why I have now four response categories per question (an additional one coloured in grey) even though I have three response categories. Any idea? – walkytalky Dec 07 '21 at 22:05
  • Hi again, I have one additional comment about your figure. First, thanks again. Why are there only two response categories in all bars while the legend has three response options (and I know from the data that there is data for all three responses)? Thanks a lot – walkytalky Dec 07 '21 at 23:26
  • Because I only have the first 10 rows of data. – Gregor Thomas Dec 08 '21 at 01:44
  • Hi Gregor, do you now happen to know how to add percentages to the stacked bar plots? – walkytalky Dec 08 '21 at 13:51
  • Search the [ggplot2] tag for "stacked bar percentage labels", https://stackoverflow.com/q/12386005/903061 , https://stackoverflow.com/q/47936123/903061, https://stackoverflow.com/a/55208869/903061, https://stackoverflow.com/a/43488370/903061 – Gregor Thomas Dec 08 '21 at 13:59
  • Thanks, Gregor! However, it does not work when I check these. I already did some research and unfortunately it does not really work. I know this is really basic stuff. Could you support me again for hopefully a final time? – walkytalky Dec 08 '21 at 18:14
  • bb.perc <- ddply(longer_data,.(question),summarise,response.PROP = sum(response) / length(response)) longer_data %>% drop_na(response) %>% mutate(response = factor(response, levels = 1:3, labels = c("Yes", "No", "I don't know"))) %>% ggplot(bb.perc, aes(x = factor(question), y = response.PROP, label=paste(round(response.PROP*100), "%")) + geom_bar(stat= "count", position = "fill") + labs(title =" xy" )+ scale_fill_manual(values = c("Yes" = "Green", "No" = "Red", "don't know" = "Grey")) + labs(x ="HMIs", y = "Percentage") + scale_y_continuous(labels = scales::percent) – walkytalky Dec 08 '21 at 18:15
  • Please ask this as a new question. – Gregor Thomas Dec 08 '21 at 19:11
  • Thanks! I will do that now! – walkytalky Dec 08 '21 at 19:20
  • https://stackoverflow.com/questions/70280626/adding-percentage-labels-to-stacked-barplot – walkytalky Dec 08 '21 at 19:26