0

I'm trying to plot a bar that uses 3 data sets. This bar chart, p4, is a summary of the previous 3 bar charts, The first 3 graphs work, its p4 that is the issue. Once i tried making it i got the error:

Error: stat_bin() must not be used with a y aesthetic

I'm not sure what the error could be, i'm very new to r and ggplot2.

Code:

library(ggplot2)

p<-ggplot(data=english, aes(x = reorder(V1, -V3), y=V3))+
  geom_bar(stat="identity", 
           fill="steelblue", 
           color="black", 
           width = 0.8, 
           position = position_dodge(width = 0.9)) +
  ggtitle("English") +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(x="Letter", y="Frequency") +
  coord_flip() +
  scale_y_continuous(limits = c(0,max(english[,"V3"])+3)) + 
  geom_text(aes(label=V3), vjust=0.4, hjust=-0.1, size=3.5)

p2<-ggplot(data=french, aes(x = reorder(V1, -V3), y=V3))+
  geom_bar(stat="identity", 
           fill="steelblue", 
           color="black", 
           width = 0.8, 
           position = position_dodge(width = 0.9)) +
  ggtitle("French") +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(x="Letter", y="Frequency") +
  coord_flip() +
  scale_y_continuous(limits = c(0,max(french[,"V3"])+3)) + 
  geom_text(aes(label=V3), vjust=0.4, hjust=-0.1, size=3.5)

p3<-ggplot(data=german, aes(x = reorder(V1, -V3), y=V3))+
  geom_bar(stat="identity", 
           fill="steelblue", 
           color="black", 
           width = 0.8, 
           position = position_dodge(width = 0.9)) +
  ggtitle("German") +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(x="Letter", y="Frequency") +
  coord_flip() +
  scale_y_continuous(limits = c(0,max(german[,"V3"])+3)) + 
  geom_text(aes(label=V3), vjust=0.4, hjust=-0.1, size=3.5)

p4<-ggplot(aes(x=V1, y=V3))+
  geom_bar(data=english,
           stat="identity", 
           fill="steelblue", 
           color="black", 
           width = 0.8, 
           position = position_dodge(width = 0.9))+
  geom_bar(data=french,
           stat="identity", 
           fill="green", 
           color="black", 
           width = 0.8, 
           position = position_dodge(width = 0.9))+
  geom_bar(data=german,
           stat="identity", 
           fill="red", 
           color="black", 
           width = 0.8, 
           position = position_dodge(width = 0.9))+
  ggtitle("Laguages") +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(x="Letter", y="Frequency")

require(gridExtra)
grid.arrange(p, p2, p3, p4, ncol=2)
Oblivion
  • 585
  • 2
  • 8
  • 26
  • 2
    Provide a reproducible example. See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – MLavoie Feb 28 '18 at 14:00
  • It's hard to provide any concrete suggestions without a reproducible example, but things generally work better in ggplot if you have all your data in one dataframe, rather than pulling multiple dataframes into one plot – Jan Boyer Feb 28 '18 at 15:32

0 Answers0