【问题标题】:Stacked Bar Graph reproduction in RR中的堆叠条形图再现
【发布时间】:2018-02-22 13:15:26
【问题描述】:

我试图在 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%

理想情况下,颜色会将“标题”列作为标签(支柱 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")

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

输入(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")

【问题讨论】:

  • 您可以使用dput(df) 并将结果添加到您的问题中。这可能对愿意研究您的案例的人有所帮助..
  • 刚做了,谢谢不知道这个功能
  • 您的 df 有点乱 .. 在 value 列中,我找到了数值和文本 .. 可以在 @Foster 在他的回答中的建议下再做一次,并更新您的问题?

标签: r ggplot2 stacked-chart


【解决方案1】:

使用您提供的原始数据。

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

为您提供这个。

【讨论】:

    【解决方案2】:

    首先读取包含这些参数的数据:

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

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

    如果这没有帮助,您可以将数据框中的标题删除到 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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-21
      相关资源
      最近更新 更多