0

I only have basic skills with R and concerning the subject line I have tried the following approach: I have a huge data-set (Data_IlA) from which I want to analyze one variable (LMXnofive) through a bar chart. However, I want to analyze all observations based on their group-belonging (G1_ID) and also want to give the percentage of particular answers given (the scale is from 0 to 7). Hence, I created a df:

Data_IlA %>%
select(LMXnofive, G1_ID) %>% 
na.omit -> data_items

data_items %>%  
gather(key = items, value = answer) %>% 
mutate(answer = factor(answer)) %>% 
ggplot(aes(x = items)) +
geom_bar(aes(fill = answer), position = "fill") + coord_flip()

I am not exactly sure how to go about assigning the values in LMXnofive to the different groups from G1_ID (1 to 6) in order to have one bar for each group with different colours for the different percentages depending on the answers/ values in each group. The graph I get with the calculation above is the following

Every help is much appreciated, thank you.

Andreas G.
  • 190
  • 11
  • `geom_bar` plots the number of observations for each x value. – Jack Brookes May 23 '18 at 19:27
  • Adding a reproducible example where we can use data similar to yours will allow people to answer https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Jack Brookes May 23 '18 at 19:27
  • Thanks for the comments! This actually was my first question and I learned, also through your link, how to do it in a better way. I was writing a rather different and very much thought through question yesterday (https://stackoverflow.com/questions/50516487/creating-stacked-bar-chart-with-one-variable-for-each-bar-using-melt-and-ggplo) – Andreas G. May 25 '18 at 04:47

0 Answers0