gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:14:22 24 4
gpt4 key购买 nike

如何删除每个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com