Geom_label 有条件改变颜色文本和填充

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

我正在创建一个饼图,当标记的部分填充黑色时,我希望标签文本和填充的颜色为白色。目前,其他标签遵循标记段的颜色,我想保持这种状态。黑色填充的部分也有黑色标签文本,因此尽管存在,但看不到。

ggplot(pigs2.proportions,
       aes(x="", y=comp_percent, fill = percent_class, width = 0.4)) +
  geom_bar(stat="identity") +
  geom_label(
    aes(
      label = ifelse(comp_percent %% 1 != 0, sprintf("%.2f%%", comp_percent),
                     sprintf("%.0f%%", comp_percent))),
            position = position_stack(vjust = 0.5), 
    size = 4,
    label.padding = unit(0.25, "lines"),
    show.legend = FALSE) +
  coord_polar("y", start=0) +
  facet_wrap(~factor(source, levels = c("fish", "soy", "skim")), labeller = custom_labeller("source")) +
  scale_fill_manual(values = c("low" = "black", "moderate" = "darkgray", "high" = "lightgray")) +
  theme(
    axis.text.x = element_blank(), # Hide labels on polar coordinate axis
    panel.grid = element_blank(),  # Remove grid lines
    panel.border = element_blank(),  # Remove round outline
    axis.text = element_blank(),  # Remove axis text
    axis.title = element_blank()  # Remove axis titles
  )
structure(list(source = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L), levels = c("fish", "soy", "skim"), class = "factor"), 
    percent_class = structure(c(3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
    2L), levels = c("low", "moderate", "high"), class = "factor"), 
    count = c(3L, 2L, 5L, 1L, 3L, 6L, 1L, 3L, 5L), comp_percent = c(30, 
    20, 50, 10, 30, 60, 11.1111111111111, 33.3333333333333, 55.5555555555556
    ), pos = c(15, 10, 25, 35, 35, 80, 45.5555555555556, 66.6666666666667, 
    137.777777777778)), class = c("grouped_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -9L), groups = structure(list(
    percent_class = structure(1:3, levels = c("low", "moderate", 
    "high"), class = "factor"), .rows = structure(list(c(2L, 
    5L, 8L), c(3L, 6L, 9L), c(1L, 4L, 7L)), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L), .drop = TRUE))

output from code

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

正如@AllanCameron 在评论中所建议的,您可以例如:将条件

percent_class == "low"
映射到
color
aes 上,并使用
scale_color_manual
设置所需的文本颜色。但是,这样做会更改
geom_label
的数据分组,因此标签的堆叠不再与条形对齐。要解决此问题,您必须显式地将
percent_class
映射到
group
aes。

library(ggplot2)

p <- ggplot(
  pigs2.proportions,
  aes(x = "", y = comp_percent, fill = percent_class, width = 0.4)
) +
  geom_bar(stat = "identity") +
  coord_polar("y", start = 0) +
  facet_wrap(
    ~ factor(source, levels = c("fish", "soy", "skim")),
    # labeller = custom_labeller("source")
  ) +
  scale_fill_manual(
    values = c("low" = "black", "moderate" = "darkgray", "high" = "lightgray")
  ) +
  theme(
    axis.text.x = element_blank(), # Hide labels on polar coordinate axis
    panel.grid = element_blank(), # Remove grid lines
    panel.border = element_blank(), # Remove round outline
    axis.text = element_blank(), # Remove axis text
    axis.title = element_blank() # Remove axis titles
  )

p +
  geom_label(
    aes(
      label = ifelse(comp_percent %% 1 != 0,
        sprintf("%.2f%%", comp_percent),
        sprintf("%.0f%%", comp_percent)
      ),
      color = percent_class == "low",
      group = percent_class
    ),
    position = position_stack(vjust = 0.5),
    size = 4,
    label.padding = unit(0.25, "lines"),
    show.legend = FALSE
  ) +
  scale_color_manual(values = c("black", "white"))

第二个选项是使用

after_scale
根据
color
的值有条件地设置
fill
,如下所示:

p +
  geom_label(
    aes(
      label = ifelse(comp_percent %% 1 != 0,
        sprintf("%.2f%%", comp_percent),
        sprintf("%.0f%%", comp_percent)
      ),
      color = after_scale(
        ifelse(fill == "black", "white", "black")
      )
    ),
    position = position_stack(vjust = 0.5),
    size = 4,
    label.padding = unit(0.25, "lines"),
    show.legend = FALSE
  )

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