如何在R中用ggplot2改变图例的标题、键序和颜色为多图层柱状图。

我正在使用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)

这让我返回。

enter image description here

解决方案:

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

给你。

enter image description here

给TA打赏
共{{data.count}}人
人已打赏
解决方案

网络错误 Axios试图从本地主机获取数据。

2022-4-22 21:00:18

解决方案

如何在Asp.net MVC中从Azure访问App设置?

2022-4-22 21:00:21

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索