r - ggplot : why is automated text printed in legend's fill?

标签 r ggplot2 plot legend

如何删除每个 fill 中打印的 a? (我也对为什么a首先出现的原因感兴趣)

enter image description here

我有

> head(p)
  studie n_otte
1      B     N0
2      B    N3b
3      B    N3b
4      B     N0
5      B     N0
6      B    N3b

还有

# Colors
colsze = c("#E1B930", "#2C77BF","#E38072","#6DBCC3", "grey40", "black", "#8B3A62")
# Data and plot
p %>% as_tibble() %>% 
  mutate(nystudie=as.character(studie),
         n_otte=as.factor(n_otte)) %>% 
  bind_rows(., mutate(., nystudie="all")) %>% 
  count(nystudie, n_otte) %>%
  ggplot(aes(nystudie, n, color = n_otte, fill= n_otte, label=n))  +
  geom_col(position = position_dodge2(preserve = "single", padding = 0.1))+
  geom_text(aes(label=n),position = position_dodge2(0.9), vjust=0, fontface=2) +
  scale_fill_manual(values = alpha(colsze, .2), 
                    name="") + 
  scale_color_manual(values = colsze, 
                     name="") + 
  guides(fill = guide_legend(nrow = 1)) + theme(legend.position="top")

产生这个情节

enter image description here

我尝试添加

+ guides(fill = guide_legend(nrow = 1, override.aes = list(fill = alpha(colsze,0.5), color = colsze, lwd = .8)))  

但收到错误:

[[<-.data.frame(*tmp*, i, value = c("#E1B93080", "#2C77BF80",  : 
  replacement have 7 rows, data have 6

我知道 colsze 指定了七种颜色,我只是简单地假设包含 6 个值的数据将选择初始的 6 种颜色。

> colsze
[1] "#E1B930" "#2C77BF" "#E38072" "#6DBCC3" "grey40"  "black"   "#8B3A62"

数据

p <- structure(list(studie = c("B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B"), n_otte = structure(c(1L, 6L, 6L, 1L, 1L, 
6L, 1L, 6L, 2L, 6L, 6L, 6L, 6L, 1L, 1L, 1L, 3L, 1L, 6L, 1L, 1L, 
2L, 1L, 1L, 1L, 1L, 4L, 5L, 1L, 4L, 5L, 6L, 1L, 1L, 5L, 1L, 1L, 
6L, 1L, 1L, 1L, 6L, 1L, 1L, 1L, 6L, 6L, 6L, 1L, 6L), .Label = c("N0", 
"N1", "N2a", "N2b", "N2c", "N3b"), class = "factor")), row.names = c(NA, 
-50L), class = "data.frame")

最佳答案

试试这个。由于 aes() 定义,您过去常常遇到此类问题。当您启用诸如 fill 之类的选项时,geom_text() 用于获取所有这些选项并在图例中显示。一种选择是使用参数 show.legend ,该参数允许隐藏图例中不需要的元素。代码如下:

library(tidyverse)
# Colors
colsze = c("#E1B930", "#2C77BF","#E38072","#6DBCC3", "grey40", "black", "#8B3A62")
# Data and plot
as_tibble(p) %>% 
  mutate(nystudie=as.character(studie),
         n_otte=as.factor(n_otte)) %>% 
  bind_rows(., mutate(., nystudie="all")) %>% 
  count(nystudie, n_otte) %>%
  ggplot(aes(nystudie, n, color = n_otte, fill= n_otte, label=n))  +
  geom_col(position = position_dodge2(preserve = "single", padding = 0.1))+
  geom_text(aes(label=n),position = position_dodge2(0.9),
            vjust=0, fontface=2,show.legend = F) +
  scale_fill_manual(values = alpha(colsze, .2), 
                    name="") + 
  scale_color_manual(values = colsze, 
                     name="") + 
  guides(fill = guide_legend(nrow = 1)) + theme(legend.position="top")

输出:

enter image description here

关于r - ggplot : why is automated text printed in legend's fill?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63884500/

相关文章:

r - 在igraph中生成社区图

python - Matplotlib 等同于 ggplot geom_ribbon?

r - 将箭头放置在R中现有的背景图片上

r - 在 R 中从列表创建多个饼图

r - 在 ggplot2 中使用循环进行多个绘图

r - 从 R 中的 Document-Term-Matrix 过滤行/文档

r - 在 R 中合并列具有相同值但大小写不同的行

r - 用NA水平分割一个因子

r - 绘制 4 组 - 女性录取和 Not_Admitted,男性录取和不录取 ggplot2

r - 如何更改 Donut ggplot 图中的plot.background?