gpt4 book ai didi

r - 添加图层时填充和颜色图例消失

转载 作者:行者123 更新时间:2023-12-04 15:27:02 25 4
gpt4 key购买 nike

我在 ggplot 对象中改变图层时发现了一个意想不到的行为。我从一个初始情节开始。

library(ggplot2)

p <-
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Petal.Width, size = Petal.Length)) +
geom_point() +
scale_x_continuous(position = 'top') +
scale_size_area('Size Name', max_size = 12) +
scale_color_gradientn(
'Color Name',
colors = scales::brewer_pal(type = 'div')(7),
limits = c(-3, 3),
breaks = c(-3, -1.5, 0 , 1.5, 3)
)

print(p)

Plot 1

然后我决定将点层更改为具有不同形状 21 的点层。形状 21 具有填充而不是颜色。但是,我想保持其他一切不变。为了节省打字时间,我首先删除了初始点层,然后添加了新的点层。但是,我发现填充的图例在应该显示的时候没有显示。


p$layers
#> [[1]]
#> geom_point: na.rm = FALSE
#> stat_identity: na.rm = FALSE
#> position_identity



p$layers[[1]] <- NULL


p_with_name <-
p +
geom_point(aes(fill = Petal.Width), shape = 21, color = 'black') +
scale_fill_gradientn(
'Color Name',
colors = scales::brewer_pal(type = 'div')(7),
limits = c(-3, 3),
breaks = c(-3, -1.5, 0 , 1.5, 3)
)

print(p_with_name)

Plot 2

但是,当我添加没有标题的填充比例,或者使用与色标不同的标题时,它会正确显示。

p_no_name <-
p +
geom_point(aes(fill = Petal.Width), shape = 21, color = 'black') +
scale_fill_gradientn(
colors = scales::brewer_pal(type = 'div')(7),
limits = c(-3, 3),
breaks = c(-3, -1.5, 0 , 1.5, 3)
)

print(p_no_name)

Plot 3


p_diff_name <-
p +
geom_point(aes(fill = Petal.Width), shape = 21, color = 'black') +
scale_fill_gradientn(
'Diff Name',
colors = scales::brewer_pal(type = 'div')(7),
limits = c(-3, 3),
breaks = c(-3, -1.5, 0 , 1.5, 3)
)

print(p_diff_name)


Created on 2020-05-28 by the reprex package (v0.3.0)

Plot 4

是否可以添加一个与色标同名的填充标尺并让它出现?这将帮助我大大简化我的代码。

最佳答案

默认情况下,ggplot2 尝试通过合并具有相同名称的不同美学的图例来最小化图例的数量,...。经过一些试验后,我发现了一种防止图例合并的技巧,并且有两个具有相同名称的“相同”图例。我只是将 NA 添加到 scale_fill_gradientn 中的中断向量,这打破了颜色和填充比例的特性,并防止图例被合并。试试这个:

library(ggplot2)

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Petal.Width, size = Petal.Length)) +
geom_point() +
scale_x_continuous(position = 'top') +
scale_size_area('Size Name', max_size = 12) +
scale_color_gradientn(
'Color Name',
colors = scales::brewer_pal(type = 'div')(7),
limits = c(-3, 3),
breaks = c(-3, -1.5, 0 , 1.5, 3)
) +
geom_point(aes(fill = Petal.Width), shape = 21, color = 'black') +
scale_fill_gradientn(
'Color Name',
colors = scales::brewer_pal(type = 'div')(7),
limits = c(-3, 3),
breaks = c(-3, -1.5, 0 , 1.5, 3, NA)
)

reprex package 创建于 2020-05-28 (v0.3.0)

编辑:问题的“干净”解决方案

# clean solution
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, size = Petal.Length)) +
scale_x_continuous(position = 'top') +
scale_size_area('Size Name', max_size = 12)

p +
geom_point(aes(color = Petal.Width)) +
scale_color_gradientn(
'Color Name',
colors = scales::brewer_pal(type = 'div')(7),
limits = c(-3, 3),
breaks = c(-3, -1.5, 0 , 1.5, 3)
)

p +
geom_point(aes(fill = Petal.Width), color = "black", shape = 21) +
scale_fill_gradientn(
'Color Name',
colors = scales::brewer_pal(type = 'div')(7),
limits = c(-3, 3),
breaks = c(-3, -1.5, 0 , 1.5, 3)
)

关于r - 添加图层时填充和颜色图例消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62058600/

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