GGplot 图例显示缺失因子水平的空白填充

问题描述 投票:0回答:1

我正在尝试使用

ggplot
绘制频率图,其中删除的值仍然显示在图例中。我已经能够通过在
drop = FALSE
中设置
scale_fill_manual
来实现此目的,但是即使我已明确设置它,放置的字段在图例中没有颜色。我创建了一个与下面的数据类似的示例。在此示例中,我正在寻找 Male 在图例中显示,并在
scale_fill_manual
中设置颜色,但我似乎不知道如何执行此操作。

Data_Sample <- data.frame(CatType = c(rep("C1",3), rep("C2",7), rep("C3",8), rep("C4", 9), rep("C5", 2), rep("C6", 1)), 
Owner = c("Angela", "Karen", "Angela")) %>%
  mutate(Sex = ifelse(str_detect(CatType,"6"), "Male", "Female"),
         CatID = str_pad(row_number(), 2, pad="0"),
         Sex = factor(Sex, c("Male" = "Male", "Female" = "Female")),
         Owner = factor(Owner, c("Angela" = "Angela", "Karen" = "Karen")),
         CatType = factor(CatType))

Sample_Plot <- ggplot(Data_Sample %>% filter(., Owner == "Karen"), aes(x = CatType)) +
  geom_bar(colour = "#2E312F",aes(y = 100*((..count..)/sum(..count..)), fill = Sex)) +
  scale_x_discrete(drop = FALSE) +
  geom_text(aes(label = paste(stat(100*round((..count..)/sum(..count..),5)), "%", sep = ""), x = CatType, y = stat(100*round((..count..)/sum(..count..),5))), stat = "count", colour = "#2E312F",size = 3.5, fontface = "bold", vjust = -0.25) +
  theme(axis.text.x = element_text(face="bold"),
        axis.title.y = element_text(face="bold", size=11),
        axis.text.y = element_text(face="bold"),
        axis.title.x = element_text(face="bold", size=11),
        legend.title = element_text(face="bold", size=11),
        legend.text = element_text(face="bold", size=7.5),
        plot.title = element_text(face="bold", size=14, hjust = 0.5),
        legend.title.align = 0.5,
        legend.text.align = 0,
        legend.box.background = element_rect(colour = "black", fill = "transparent", size = 1.5),
        strip.text = element_text(face="bold", size=14),
        panel.border = element_rect(colour = "black", fill=NA, linewidth = 2)
  ) +
  scale_fill_manual(values = c("Female" = "#c90076", "Male" = "#2986cc"), drop = FALSE) +
  guides(fill = guide_legend(reverse=TRUE)) +
  labs(x = "Cat Type", y = "Percentage of Cats", title = paste("Karen's Cats", sep =" ")) +
  scale_y_continuous(label=scales::label_percent(scale = 1), limits = c(0,100))

r ggplot2 fill geom-bar
1个回答
0
投票

这是由于

ggplot2 3.5.0
中引入的更改所致,现在需要将
show.legend=TRUE
显式添加到
geom
以显示未使用因子水平的图例键。另外请注意,我切换到
after_stat
,因为
..
stat
自版本
3.4.0
起已弃用,并使用
hjust
来对齐图例文本和标题,因为
legend.xxx.align
中已弃用
3.5.0

library(ggplot2)
library(dplyr, warn = FALSE)

packageVersion("ggplot2")
#> [1] '3.5.0'

ggplot(Data_Sample %>% filter(., Owner == "Karen"), aes(x = CatType)) +
  geom_bar(colour = "#2E312F", aes(
    y = 100 * after_stat(count / sum(count)),
    fill = Sex
  ), show.legend = TRUE) +
  scale_x_discrete(drop = FALSE) +
  geom_text(
    aes(
      label = paste0(100 * round(after_stat(count / sum(count))), "%"),
      x = CatType,
      y = after_stat(count / sum(count))
    ),
    stat = "count", colour = "#2E312F",
    size = 3.5, fontface = "bold", vjust = -0.25
  ) +
  theme(
    axis.text.x = element_text(face = "bold"),
    axis.title.y = element_text(face = "bold", size = 11),
    axis.text.y = element_text(face = "bold"),
    axis.title.x = element_text(face = "bold", size = 11),
    legend.title = element_text(face = "bold", size = 11, hjust = .5),
    legend.text = element_text(face = "bold", size = 7.5, hjust = 0),
    plot.title = element_text(face = "bold", size = 14, hjust = 0.5),
    legend.box.background = element_rect(
      colour = "black",
      fill = "transparent", linewidth = 1.5
    ),
    strip.text = element_text(face = "bold", size = 14),
    panel.border = element_rect(colour = "black", fill = NA, linewidth = 2)
  ) +
  scale_fill_manual(values = c("Female" = "#c90076", "Male" = "#2986cc"), drop = FALSE) +
  guides(fill = guide_legend(reverse = TRUE)) +
  labs(x = "Cat Type", y = "Percentage of Cats", title = paste("Karen's Cats", sep = " ")) +
  scale_y_continuous(label = scales::label_percent(scale = 1), limits = c(0, 100))

© www.soinside.com 2019 - 2024. All rights reserved.