代码之家  ›  专栏  ›  技术社区  ›  Yrden

R中的堆叠条形图复制

  •  0
  • Yrden  · 技术社区  · 6 年前

    我试图在R中复制此图,但没有成功:

    enter image description here

    但多年来

    以下是数据:

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

    这是我目前的图表(完全混乱)

    enter image description here

    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")
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Wyldsoul    6 年前

    使用您提供的原始数据。

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

    为您提供此功能。

    enter image description here

        2
  •  0
  •   David Foster    6 年前

    读取第一个实例中的数据,包括以下参数:

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