Rtab
Rtab

Reputation: 123

ggplot group mean line

Trying to add a mean line for each group to bar chart. Receive same error each time:

Error in FUN(X[[i]], ...) : object 'Count' not found


ggplot(df_melted2  %>% 
         filter(EDNAME =='Blanchardstown-Corduff'), aes(Resource, Count, fill=Resource)) + 
  geom_bar(stat="identity", position = position_dodge()) +
  facet_wrap(~ EDNAME, scales = "free") +
  coord_flip() +
  theme(panel.background = element_blank())  +
  geom_point(data = df_melted2 %>% 
               group_by(Resource) %>% 
               summarise(Mean_Resource = mean(Count))) 

It works fine without the attempt to add the mean line:

ggplot(df_melted2  %>% 
         filter(EDNAME =='Blanchardstown-Corduff'), aes(Resource, Count, fill=Resource)) + 
  geom_bar(stat="identity", position = position_dodge()) +
  facet_wrap(~ EDNAME, scales = "free") +
  coord_flip() +
  theme(panel.background = element_blank()) 

Why is it not recognizing the column 'Count' and how did I get overall group mean lines in the chart?

enter image description here

Upvotes: 0

Views: 97

Answers (1)

Rtab
Rtab

Reputation: 123

Major oversight on my part. The summarized column must be the same name as the original and I had given it a new name.

ggplot(df_melted2  %>% 
         filter(EDNAME =='Blanchardstown-Corduff'), aes(Resource, Count, fill=Resource)) + 
  geom_bar(stat="identity", position = position_dodge()) +
  facet_wrap(~ EDNAME, scales = "free") +
  coord_flip() +
  theme(panel.background = element_blank())  +
  geom_point(data = df_melted2 %>% 
               group_by(Resource) %>% 
               summarise(***Count*** = mean(Count))) 

Upvotes: 1

Related Questions