stat_count() can only have an x or y aesthetic - Issue

Hi, been stuck on this plot for ages, and I keep getting back the statement "stat_count() can only have an x or y aesthetic" can anyone please tell me whats wrong? :frowning:


fig2= ggplot(sample.bact,aes(x=i_identified_bacteria_final, y=n_isolate), stat="identity")+
  geom_bar(stat = "identity",width=0.6,position = "dodge")+
  scale_fill_manual(values=c("#BC3C2999","#0072B599","#7676B199"))+
  geom_text(stat="count",aes(label=..count..),position=position_stack(vjust = 0.5),size=2.5)+
  ylab("Number of isolates")+
  xlab("Bacterial Types")+theme_bw()+
  guides(fill=guide_legend(title ="Sample types"))+
  geom_text(aes(label=n_isolate),position=position_dodge(width =0.6),vjust=-0.4,size=5)+
  theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),
        panel.background = element_blank(),axis.line=element_line("black"),
        axis.text.y = element_text(face="bold", family = "Arial",
                                   size=15),
        
        title = element_text(hjust=0.5,face="bold",vjust=0.5,family = "Arial"),
        text=element_text(family = "Arial",size=20
        ),
        plot.background = element_rect("#EBEBEB"))+
  facet_grid(fram~.)+
  scale_y_continuous(limits = c(0, 200))+
  scale_x_discrete(guide = guide_axis(angle = 45),
                   labels=c("E.faecalis","E.faecium","E. coli",
                            "K.pneumoniae",
                            "Salmonella spp","S.aureus"))
fig2+plot_annotation(title =  "Swine Farm Isolates Summary",
                     theme=theme(plot.title = element_text(color="#292929",size=22,face="bold",family="Arial Black",hjust = 0.5),
                                 plot.caption = element_text(colour = "grey50",face="italic",family = "Arial Black"),
                                 plot.background = element_rect("#EbEBEB"),
                                 panel.background = element_rect("#EbEBEB"))) 

I made a minimal example of what I think your data look like and what you are trying to do. If the code below does not help you solve your problem, please make a similarly minimal example of your data and the simplest ggplot code that demonstrates the error.

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
DF <- data.frame(Major = c("A", "A", "B", "B"),
                 Minor = c("X", "Y", "X", "Y"),
                 Value = c(5,6,7,4))
DF
#>   Major Minor Value
#> 1     A     X     5
#> 2     A     Y     6
#> 3     B     X     7
#> 4     B     Y     4
ggplot(DF, aes(x = Major, y = Value, fill = Minor)) + 
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(label = Value), position = position_dodge(width=1), vjust = 1)

Created on 2021-08-26 by the reprex package (v0.3.0)

This topic was automatically closed 21 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.