Creating Barplot of multiple dataframe columns

Hey, I´m trying to create a grouped bar plot for the following data frame:

Using my code, it will plot the same bars for every value and I don´t really know whats the problem right here.

ggplot(buttons_long, aes(button, wert, fill = Relevanz)) + geom_col(position = "dodge")

ggplot(buttons_long,aes(x = button,y = wert)) + 
  geom_bar(aes(fill = Relevanz),position = "dodge",stat = "identity") + 

Would be awesome if someone got an answer why it´s plotting the same bars. Also I´d like to change the Color to a grey palet and get the total amount on the y-axis.

I think you want the following ggplot code. The geom_bar() will count how many times each value of button appears, subdivided by values of relevanz.

ggplot(buttons_long,aes(x = button)) + 
  geom_bar(aes(fill = Relevanz),position = "dodge") + 

In your original code

ggplot(buttons_long,aes(x = button,y = wert)) + 
  geom_bar(aes(fill = Relevanz),position = "dodge",stat = "identity") + 

you set stat to identity. The geom_bar() then plotted the value of wert for each level of Relevanz. Since wert and Relevanz are the same information expressed in different forms (gar_nicht_wichtig = 1, Nicht_wichtig = 2, etc.), your plot was identical for every level of button.

Thanks a lot! Is there a way to show the actual counts inside the bars for each rating?

Here is a rough version of showing the actual values on the plot. You can adjust the vertical position of the numbers with the vjust argument in geom_text.

buttons_long <- read.csv("~/R/Play/dataframe.csv")

buttons_long |> count(button, Relevanz) |> 
ggplot(aes(x = button, y = n, fill = Relevanz)) + 
  geom_col(position = position_dodge()) + 
  geom_text(aes(label = n), position = position_dodge(width = 0.9)) +

Created on 2022-07-26 by the reprex package (v2.0.1)

thanks a lot, added some more parameters and got it perfectly for me!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.