如何使用ggplot2将图例标题,键顺序和颜色更改为R中的多堆叠条形图

丹妮

我正在将R与一个多堆栈的条形图配合使用,我想更改图例的名称并编辑有问题的变量的颜色(我完全希望使用蓝色调色板)。

但是,我一直试图徒劳地编辑图例和颜色。我想知道你能不能帮我!

以下是我到目前为止使用的代码。

    library(ggthemes)
    library(tidyverse)
    library(reshape2)

    theme_set(theme_classic())


    multiBP <- data.frame(Countries = c('Romania','Hungary','Spain','Italy','poland','Greece','France','Finland','Germany','Denmark','Netherlands','Sweden'),

                          worried_emigration <- c(0.55, 0.39, 0.34, 0.32, 0.30, 0.28, 0.14, 0.14, 0.08, 0.06, 0.06, 0.04),

                          both <- c(0.26, 0.34, 0.38, 0.35, 0.30, 0.54, 0.31, 0.20, 0.29, 0.12, 0.15, 0.14),

                          immigration <- c(0.10, 0.2, 0.19, 0.24, 0.27, 0.07, 0.25, 0.44, 0.31, 0.53, 0.55, 0.47),

                          neitherdontknow <- c(0.09, 0.07, 0.09, 0.09, 0.13, 0.11, 0.30, 0.22, 0.32, 0.29, 0.24, 0.35))





    data.m <- melt(multiBP, id.vars='Countries')

    data.m['value'] <- data.m['value']*100


    levels(data.m$variable) <- c("% Worried about emigration", "% Worried both immigration and emigration",
                                 "% Worried about immigration", "% Neither / don't know")


    data.m$variable <- factor(data.m$variable, levels = c("% Worried about emigration", "% Worried about immigration",
                                                          "% Worried both immigration and emigration", "% Neither / don't know" ))



    # ggplot(arrange(mdat,variable,desc(value)), aes(variable, value, fill=day)) + 



    ag<-filter(data.m, variable == "% Worried about emigration")

    data.m$Countries <- factor(data.m$Countries, levels=ag$Countries[order(ag$value)], ordered = TRUE)



    # ggplot(arrange(mdat,variable,desc(value)), aes(variable, value, fill=day)) + 



    ggplot(arrange(data.m, variable, desc(value), Countries), aes(fill = forcats::fct_rev(variable), Countries, value))+
      geom_bar(position = 'stack', stat = 'identity', colour='grey')+
      expand_limits(x=c(0,0))+
      coord_flip()+
      labs(title="Multiple Time Series Chart", 
           subtitle="BLA BLA ", 
           caption="Source: The Guardian")+
      # guide_legend(title=  'TItolo')+
      theme(legend.background = element_rect(colour = 'black', fill = 'grey90', size = 1, linetype='solid')) +
      geom_text(aes(label=paste0((value),"%")),
                position=position_stack(vjust=0.5),size=3)

那让我回来了:

在此处输入图片说明

彼得
library(ggplot2)

arrange(data.m, variable, desc(value), Countries) %>% 
ggplot(aes(Countries, value, fill = fct_rev(variable)))+
  geom_bar(position = 'stack', stat = 'identity' ) +
  scale_fill_brewer(guide = guide_legend(reverse = TRUE), palette = "Blues") +
  geom_text(aes(label = paste0(value,"%")), position = position_stack(vjust = 0.5), size = 3)+
  coord_flip()+
  labs(title = "Multiple Time Series Chart", 
       subtitle = "BLA BLA ", 
       caption = "Source: The Guardian",
       fill = "Legend Title")+
  theme(legend.background = element_rect(colour = 'black', fill = 'grey90', size = 1, linetype='solid'))

给你:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在多索引堆叠条形图和条形颜色中编辑图例?

堆叠条形图ggplot2中的标签顺序

按顺序指示在ggplot2中堆叠条形图的y方向上的更改顺序

如何使用ggplot2更改堆积条形图的顺序和配色方案?

在条形图ggplot2中更改内部图例顺序

如何使用R的ggplot2包更改条形图的颜色?

向R中的ggplot2中的堆叠条形图添加水平线,并在图例中显示

如何使用ggplot2将连续数作为离散数字堆叠条形图

在ggplot2中设置堆叠条形图的条形顺序

如何将图例与ggplot2中的条形图同步?

ggplot更改R中的条形颜色和图例标题

如何使用 ggplot2 在 R 中创建分组和堆积条形图

用R中的ggplot2更改geom_bar中的条形图颜色

ggplot2:图例和条形图顺序不匹配

ggplot2 堆叠和组合条形图

使用ggplot2的堆叠条形图的数据帧的保留顺序

ggplot2:使用组均值堆叠条形图

使用 ggplot2 的 5 维堆叠条形图

使用 ggplot2 堆叠条形图

更改条形图图例中的颜色

R ggplot2条形图更改特定条形的颜色

反向堆叠顺序,但不影响ggplot2条形图中的图例顺序

更改叠加条形图 ggplot2 的颜色

将ggplot2中的图例标题和图例键居中对齐以获取长图例标题

如何使用ggplot2将金字塔图的顺序更改为数据集顺序?

使用ggplot2将混淆矩阵绘制为堆叠的条形图

如何在 R 中使用 ggplot2 创建带有数据标签的非堆叠条形图?

R中的ggplot2堆叠条形图

向ggplot2 R中的堆叠条形图添加误差线-已解决