0

I want to annotate my plot to show the totals for each column. I have tried going over solutions shown on this site but have still been struggling. This is the data/code I am using:

    data <- read_csv("Book1.csv", col_types = cols(
  Year = col_factor(levels = c("2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019")), 
  Equity = col_factor(levels = c("LSES", "NESB", "ATSI", "DIS", "REG/REM", "WINTA")),
  Count = col_integer()))
str(data)

Output (summarised):

Year  : Factor w/ 9 levels "2011","2012",.. 
Equity: Factor w/ 6 levels "LSES","NESB",.. 
Count : int [1:54] 10 0 0 0 0 0 10 2 0 2 ...
Year = col_factor(levels = c("2011", "2012",..
Equity =col_factor(levels = c("LSES", "NESB", ..
Count = col_integer()_

table of my data

I want to be able to say "N=10" for 2011 (the sum of all equity values for the year 2011) and repeat that for all the years across the graph.

This is the code for my graph:

library(ggplot2)
library(readr)
p <- ggplot(data, aes(fill=Equity, y=Count, x=Year)) + 
  geom_bar(position="fill", stat="identity")+
  scale_fill_manual(values = c("#......"))+
  scale_y_continuous(labels = scales::percent)+
  ggtitle("Equity Distribution") +
  ylab("Percentage")+xlab("")+
  theme_bw() +
  theme(panel.border = element_blank(),
        legend.key = element_blank(),
        legend.background = element_blank(),
        axis.ticks = element_blank(),
        panel.grid = element_blank(),
        panel.grid.minor = element_blank(), 
        panel.grid.major = element_blank(),
        panel.background = element_blank(),
        plot.background = element_rect(fill = "transparent",colour = NA)
  )

p

ggsave("count3.png", bg = "transparent")

I have tried this and thishere amongst others and i cant seem to get anywhere. HELP!

AAA-3
  • 1
  • 3
  • Maybe [this post](https://stackoverflow.com/questions/28846348/add-number-of-observations-per-group-in-ggplot2-boxplot) will help you to find a solution. – Rémi Coulaud Jul 09 '20 at 18:05
  • @RémiCoulaud error message: 'Computation failed in `stat_summary()`: Can't convert a `spec_tbl_df/tbl_df/tbl/data.frame` object to function ' – AAA-3 Jul 10 '20 at 04:23

0 Answers0