R 中的堆叠条形图再现

Stacked Bar Graph reproduction in R

我试图在 R 中重现此图但没有成功:

但更多年

这是数据:

title   2016 phased 2017 phased 2018 phased 2019 fully loaded
Pillar 1 minimum requirement (p1min)    4,50%   4,50%   4,50%   4,50%
Pillar 2 requirement (P2R)  4,63%   1,75%   1,75%   1,75%
Conservation Buffer 0,63%   1,25%   1,88%   2,50%
O-SII buffer    0,50%   1,00%   1,50%   1,50%
Countercyclical Buffer  0,00%   0,15%   0,25%   0,35%

理想情况下,颜色会将 'title' 列作为标签(支柱 1、2 等)

到目前为止,这是我的代码

library(ggplot2)
library(xlsx)
library(reshape2)
mydata <- read.xlsx("C:/Users/ken/Desktop/donnees regulation kbc.xlsx", sheetName = "Feuil4", encoding = "UTF-8", stringsAsFactors = F)

years<-c('2015 phased','2016 phased','2017 phased','2018 phased','2019 fully loaded')
df<-data.frame(years,mydata)
df<-melt(df, id.vars="years")

ggplot(df, aes(x= years, y=value, fill=variable)) +
  geom_bar(stat = "identity")

这是我到目前为止的图表(一团糟)

dput(df)

structure(list(years = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 
3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 
4L, 5L), .Label = c("2015 phased", "2016 phased", "2017 phased", 
"2018 phased", "2019 fully loaded"), class = "factor"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L), .Label = c("title", "X2016.phased", 
"X2017.phased", "X2018.phased", "X2019.fully.loaded"), class = "factor"), 
    value = c("Pillar 1 minimum requirement (p1min) ", "Pillar 2 requirement (P2R)", 
    "Conservation Buffer", "O-SII buffer", "Countercyclical Buffer", 
    "0.045", "0.04625", "0.00625", "0.005", "0", "0.045", "0.0175", 
    "0.0125", "0.01", "0.0015", "0.045", "0.0175", "0.01875", 
    "0.015", "0.0025", "0.045", "0.0175", "0.025", "0.015", "0.0035"
    )), row.names = c(NA, -25L), .Names = c("years", "variable", 
"value"), class = "data.frame")

首先读取数据,包括这些参数:

read.xlsx(..., header = T, check.names = F)

这将阻止您的 headers 包含在长格式数据框中,并阻止 R 将 X 和 . 附加到您的图例标签中。希望这将通过将所有值设为数字(它当前包含字符串,使其成为字符类型)来修复你的 y-axis 刻度线。

如果这没有帮助,您可以将标题从数据框中移除到 legend_labs 向量中。您可以使用它向图例添加自定义标签:

legend_labs <- c("Pillar 1", "Pillar 2"...)
ggplot(...)
+ scale_color_manual(labels = legend_labs)

然后您可以使用它来标记您的 x、y 和图例标题:

+ labs(x = "X Title", y = "Y title", fill = "Legend Title")

使用您提供的原始数据。

library(ggplot2)
library(reshape2)

df <- read.table(textConnection("title   '2016 phased' '2017 phased' '2018 phased' '2019 fully loaded'
                            'Pillar 1 minimum requirement (p1min)'    4,50%   4,50%   4,50%   4,50%
                            'Pillar 2 requirement (P2R)'  4,63%   1,75%   1,75%   1,75%
                            'Conservation Buffer' 0,63%   1,25%   1,88%   2,50%
                            'O-SII buffer'    0,50%   1,00%   1,50%   1,50%
                            'Countercyclical Buffer'  0,00%   0,15%   0,25%   0,35%"), header=TRUE)

熔化数据。

df<-melt(df, id.vars="title", variable.name = "year")

替换值中的逗号。

df$value <- gsub(",", ".", df$value)

并调整此处提供的答案: Showing data values on stacked bar chart in ggplot2

ggplot(df, aes(x = year, y = value, fill = title, label = value)) +
             geom_bar(stat = "identity") +
             geom_text(size = 3, position = position_stack(vjust = 0.5)) +
             theme(
              axis.text.y = element_blank(),
              axis.ticks.y = element_blank(),
              axis.title.y = element_blank(),
              panel.grid.major = element_blank()
                  )

为您提供。