Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中堆积条形图的强制顺序_R_Ggplot2_Stacked Chart - Fatal编程技术网

R中堆积条形图的强制顺序

R中堆积条形图的强制顺序,r,ggplot2,stacked-chart,R,Ggplot2,Stacked Chart,我的堆叠条形图有问题。堆叠顺序不相同,但按递减顺序堆叠。我想修理一下这堆东西 library(ggplot2) library(reshape2) df <- read.table(textConnection("title '2016 phased' '2017 phased' '2018 phased' '2019 fully loaded' 'Pillar 1 minimum requirement (p1min)'

我的堆叠条形图有问题。堆叠顺序不相同,但按递减顺序堆叠。我想修理一下这堆东西

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)

ggplot(df, aes(x = year, y = value, fill = factor(title,levels=c("Countercyclical Buffer","O-SII buffer","Conservation Buffer","Pillar 2 requirement (P2R)","Pillar 1 minimum requirement (p1min)")), arrange(desc(Level)), 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()
  ) +
  scale_fill_brewer()

此外,我无法摆脱的因素,水平。。。从显示为图例标题

您的值列是一个字符向量,但我很确定您希望它是数字。行df$值使用+labsfill=legend TitleThanks指定图例标题,但我无法解决订购问题
df$value <- gsub(",", ".", df$value)
df$value <- gsub("%", "", df$value)
df$value <- as.numeric(df$value)