R 手动从图例中删除标高,同时保留数据中不存在的标高

R 手动从图例中删除标高,同时保留数据中不存在的标高,r,ggplot2,legend,R,Ggplot2,Legend,我有使用ggplot2在发散条形图中绘制的数据。它实际上是一个交互式绘图,用户可以在其中选择他们正在查看的因素。级别从强烈不同意到强烈同意。有时,正如在这个最小的示例中,并非所有级别都存在。即使如此,我仍然希望在图例中显示所有可能的值,以免误导用户 我发现在scale\u fill\u手册中实现这一点的方法是使用限制(而不是中断)来指定级别 问题是,作为一个发散的条形图,我不得不对数据进行一些修改,以获得跨越零的“无意见”。你会看到我“没有意见”和“没有意见”。在本例中,12%的受访者没有意见,

我有使用ggplot2在发散条形图中绘制的数据。它实际上是一个交互式绘图,用户可以在其中选择他们正在查看的因素。级别从强烈不同意到强烈同意。有时,正如在这个最小的示例中,并非所有级别都存在。即使如此,我仍然希望在图例中显示所有可能的值,以免误导用户

我发现在scale\u fill\u手册中实现这一点的方法是使用限制(而不是中断)来指定级别

问题是,作为一个发散的条形图,我不得不对数据进行一些修改,以获得跨越零的“无意见”。你会看到我“没有意见”和“没有意见”。在本例中,12%的受访者没有意见,分为“没有意见”=6和“没有意见”=6

其结果是“无意见”在图例中弹出,我想将其删除

只是重申一下,如果我在scale\u fill\u手册中使用“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")
          
库(ggplot2)
图书馆(tidyr)
图书馆(RColorBrewer)
#制作数据帧

级别试试这个,我添加了一个
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")
#制作数据帧
水平