【问题标题】:Manually removing level from legend while retaining levels not present in data手动从图例中删除级别,同时保留数据中不存在的级别
【发布时间】:2020-08-27 23:02:32
【问题描述】:

我有数据正在使用 ggplot2 绘制在发散条形图中。它实际上是一个交互式绘图,用户可以在其中选择他们正在查看的因素。级别从非常不同意到非常同意。有时,就像在这个最小的示例中一样,并非所有级别都存在。尽管如此,我还是想在图例中呈现所有可能的值,以免误导用户。

我发现在 scale_fill_manual 中执行此操作的方法是使用限制(而不是中断)来指定级别。

问题是,作为一个发散的条形图,我不得不稍微修改数据以使“没有意见”跨越零。你会看到我“没有意见”和“没有意见2”。在这个例子中,12% 的受访者没有意见,这分为“没有意见”= 6 和“没有意见2”= -6。

结果图例中弹出“no opinion2”,我想去掉。

重申一下,如果我在 scale_fill_manual 中使用“breaks”而不是“limits”,我不会得到图例中表示的所有因子水平(在本例中,“弱同意”和“强烈同意”将是不见了)。

我不确定这是否可以做到,但我希望一些有经验的人以前遇到过这个问题并想出了如何解决它。

下面的可重现代码:

library(ggplot2)
library(tidyr)
library(RColorBrewer)

#Make a dataframe
levels <-c("strongly disagree", "disagree", "weakly disagree", "no opinion",
           "weakly agree", "agree", "strongly agree","no opinion2")
df <- data.frame("name" = rep("Factor 1", 6),
                 "response" = factor(c("strongly disagree", "disagree", 
                                     "weakly disagree", "no opinion2","no opinion","agree"), 
                                      levels = levels),
                 "percentage" = c(-25, -13, -25, 6, -6, 25))

#colours for plotting
pal <- rev(brewer.pal(7, "PRGn"))
palette <- c("strongly disagree" = pal[1],"disagree" = pal[2] ,"weakly disagree" = pal[3],
             "no opinion2" = "darkgrey","no opinion" = "darkgrey","weakly agree" = pal[5], 
             "agree"= pal[6],"strongly agree"= pal[7])

#using limits, rather than breaks, means all possible response levels are represented in the plot legend
fill_limits <- c("strongly disagree", "disagree", "weakly disagree", "no opinion2",
                 "no opinion","weakly agree", "agree", "strongly agree")
#plot
ggplot(df, aes(x =name , y = percentage, fill = response)) +
  geom_bar(stat="identity", width = 0.5) +
  scale_fill_manual (values=palette, limits = levels) +
  scale_x_discrete(position = "top") +
  coord_flip() +
  
  theme_classic() +
  theme(axis.text.y=element_text(size=10),
        axis.text.x=element_blank(),
        axis.title.x=element_blank(),
        axis.title.y=element_blank(),
        axis.line.y = element_blank(),
        axis.line.x = element_blank(),
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_blank(),
        plot.title = element_text(size = 12, hjust = 0),
        legend.title = element_blank(),
        legend.justification=c("right", "bottom"),
        legend.box.just = "right",
        legend.position ="bottom",
        legend.direction="horizontal",
        legend.text=element_text(size = 6, hjust = 0)
  ) +
  guides(fill = guide_legend(nrow = 1, byrow = TRUE)) +
  geom_hline(yintercept = 0, colour = "dark grey", linetype = "dashed")
          

这是结果(您可以在右下角看到我要删除的图例元素):

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    试试这个,我添加了一个v2 向量。如果这是您想要的,请告诉我:

    #Make a dataframe
    levels <-c("strongly disagree", "disagree", "weakly disagree", "no opinion",
               "weakly agree", "agree", "strongly agree","no opinion2")
    df <- data.frame("name" = rep("Factor 1", 6),
                     "response" = factor(c("strongly disagree", "disagree", 
                                           "weakly disagree", "no opinion2","no opinion","agree"), 
                                         levels = levels),
                     "percentage" = c(-25, -13, -25, 6, -6, 25))
    
    #colours for plotting
    pal <- rev(brewer.pal(7, "PRGn"))
    palette <- c("strongly disagree" = pal[1],"disagree" = pal[2] ,"weakly disagree" = pal[3],
                 "no opinion2" = "darkgrey","no opinion" = "darkgrey","weakly agree" = pal[5], 
                 "agree"= pal[6],"strongly agree"= pal[7])
    
    #using limits, rather than breaks, means all possible response levels are represented in the plot legend
    fill_limits <- c("strongly disagree", "disagree", "weakly disagree", "no opinion2",
                     "no opinion","weakly agree", "agree", "strongly agree")
    v2 <- c("strongly disagree", "disagree", "weakly disagree",
            "no opinion","weakly agree", "agree", "strongly agree")
    #plot
    ggplot(df, aes(x =name , y = percentage, fill = response)) +
      geom_bar(stat="identity", width = 0.5) +
      scale_fill_manual (values=palette, limits = levels, breaks=v2) +
      scale_x_discrete(position = "top") +
      coord_flip() +
      
      theme_classic() +
      theme(axis.text.y=element_text(size=10),
            axis.text.x=element_blank(),
            axis.title.x=element_blank(),
            axis.title.y=element_blank(),
            axis.line.y = element_blank(),
            axis.line.x = element_blank(),
            axis.ticks.y = element_blank(),
            axis.ticks.x = element_blank(),
            plot.title = element_text(size = 12, hjust = 0),
            legend.title = element_blank(),
            legend.justification=c("right", "bottom"),
            legend.box.just = "right",
            legend.position ="bottom",
            legend.direction="horizontal",
            legend.text=element_text(size = 6, hjust = 0)
      ) +
      guides(fill = guide_legend(nrow = 1, byrow = TRUE)) +
      geom_hline(yintercept = 0, colour = "dark grey", linetype = "dashed")
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      相关资源
      最近更新 更多