繁体   English   中英

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%

理想情况下,颜色应使用“标题”列作为标签(pillar1、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)

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

如果这样做没有帮助,则可以将标题从数据legend_labs删除到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)

并调整此处提供的答案: 在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()
                  )

为您提供。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM