0

I have a list of elements; here I chose letters "a, o, u, ü". and their frequencies in two databases.

df <- as.data.frame(cbind( letter = c("a", "o", "u", "ü"), Freq.english = c("10", "50", "20", ""), Freq.german = c("10", "50", "10", "10") ))

How can I display their frequencies in one graph with ggplot2?

I can display them separately and could also later join them:

p.df1 <- ggplot(df,aes(x = letter ,y=Freq.english))+geom_bar(stat ="identity")

p.df2 <- ggplot(df,aes(x = letter ,y=Freq.german))+geom_bar(stat ="identity")

However, it would be nice if they occur just next to each other, maybe even separated by color. (smimilar to these solutions ggplot bar plot side by side using two variables) THX

Update:

I managed to adapt the link to my data frame:

df <- reshape::melt(df, id = c("letter")) ggplot(data = df, aes(x = letter, y = value, fill = droplevels(variable) )) + geom_bar(stat = "identity")+ facet_wrap(~ variable)

Now, the value-axis does not start at 0, hence there is a bar at Freq.english at "ü", even though 0 occurences.

Also, I need to sort the values for both variables independently in ascending order.

THX

1 Answers1

1

This would do I guess..

df %>% 
  gather(key = langauge, value = Freq, -letter) %>% 
  mutate(Freq = as.integer(Freq)) %>% 
  ggplot(aes(letter, Freq, fill = langauge)) + 
  geom_bar(stat = "identity", position = "dodge")
AMS
  • 49
  • 1
  • 6