我试图在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")
答案 0 :(得分:0)
首先读取包含以下参数的数据:
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")
答案 1 :(得分:0)
使用您提供的原始数据。
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()
)
为您提供此服务。