在ggplot2的堆叠条形图中显示每列的总计

时间:2020-07-09 17:02:32

标签: r ggplot2 stacked-chart

我想注释我的情节以显示每一列的总数。我已经尝试过查看本网站上显示的解决方案,但仍在努力。 这是我正在使用的数据/代码:

    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)

输出(摘要):

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

我希望能够说出2011年的“ N = 10”(2011年所有权益价值的总和),并在图表中的所有年份重复使用。

这是我的图形的代码:

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")

我已经尝试了this和这个here等,但我似乎无所适从。帮助!

0 个答案:

没有答案