在R中堆叠的条形图再现 [英] Stacked Bar Graph reproduction in R
问题描述
我试图在R中重现此图,但没有成功:
但多年以上
这是数据:
title 2016分阶段2017分阶段2018分阶段2019满载
最低要求(p1min)的最低要求(p1min)4,50%4,50%4,50%4,50%
支柱2的要求P2R)4,63%1,75%1,75%1,75%
保护缓冲区0,63%1,25%1,88%2,50%
O-SII缓冲区0, 50%1,00%1,50%1,50%
反周期缓冲区0,00%0,15%0,25%0,35%
理想情况下,颜色会将'title'列作为标签(支柱1,2等)。
这是我的代码到目前为止
library(ggplot2)
library(xlsx)
library(reshape2)
mydata< - read.xlsx(C:/ Users / ken / Desktop / do ('2015分阶段','2016分阶段','2017年分阶段',sheetName =Feuil4,encoding =UTF-8,stringsAsFactors = F)
年(年,mydata)
df< -melt(df,id.vars =years)$ b(相当于'2018分阶段','2019完全加载')
df <-data.frame
$ b ggplot(df,aes(x = years,y = value,fill = variable))+
geom_bar(stat =identity)
这是我迄今为止的图表(完整乱七八糟)
pre $
结构(列表(年份=结构(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阶段,2016阶段
2018分阶段,2019完全加载),class =factor),variable = structu re(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)。标签= c(标题,X2016.phased,
X2017.phased,X2018.phased,X2019.fully.loaded ),class =factor),
value = c(支柱1最低要求(p1min),支柱2要求(P2R),
保护缓冲区,O-SII缓冲器,逆时针缓冲器,
0.045,0.04625,0.00625,0.005,0,0.045,0.0175,
0.0125,0.01 0.00150.0450.01750.01875
0.0150.00250.0450.01750.0250.0150.0035
)),row.names = c(NA,-25L),.Names = c(years,variable,
value),class =data.frame)
使用您提供的原始数据。
$ b $标题'2016分阶段''2017分阶段''201 8阶段''2019满载'
'支柱1最低要求(p1min)'4,50%4,50%4,50%4,50%
'支柱2要求(P2R)'4 ,63%1,75%1,75%1,75%
'保护缓冲区'0,63%1,25%1,88%2,50%
'O-SII缓冲区'0 ,50%1,00%1,50%1,50%
'反周期缓冲'0,00%0,15%0,25%0,35%),头= TRUE)
融化数据。
df <-melt(df,id.vars =title,variable.name =year)
从值中替换逗号。
df $ value < - gsub(,,。,df
并且修改这里提供的答案:
I am trying to reproduce this graph in R without success:
But for more years
This is the data:
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%
Ideally, the colours would take the 'title' column as labels (pillar1, 2 etc.)
Here is my code so far
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")
This is my graph so far (complete mess)
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")
Using the original data that you provided.
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)
melt data.
df<-melt(df, id.vars="title", variable.name = "year")
Replace commas from values.
df$value <- gsub(",", ".", df$value)
And adapting the answer provided here: 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()
)
Provides you with this.
这篇关于在R中堆叠的条形图再现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!