在下图中,我想让图例显示变量“颜色”的填充颜色。我还希望图中的所有点都有黑色轮廓。看来我可以做其中之一,但不能两者都做(即,图例有颜色,点有黑色轮廓)。我选择了具有黑色轮廓的形状,如“剪切”图例中所示。
类似的问题:
library(tidyverse)
diamonds %>%
slice(1:20) %>%
ggplot(aes(x = x,
y = y,
shape = cut,
fill = color)) +
geom_point(size = 3,
alpha = 0.8) +
scale_shape_manual(values = c('Ideal' = 21,
'Premium' = 22,
'Good' = 23,
'Very Good' = 24,
'Fair' = 25)) +
scale_fill_manual(values = c('E' = '#ca0020',
'F' = '#f4a582',
'H' = '#ffffbf',
'I' = '#92c5de',
'J' = '#0571b0')) +
theme_bw()
diamonds %>%
slice(1:20) %>%
ggplot(aes(x = x,
y = y,
shape = cut,
fill = color,
color = color)) +
geom_point(size = 3,
alpha = 0.8) +
scale_shape_manual(values = c('Ideal' = 21,
'Premium' = 22,
'Good' = 23,
'Very Good' = 24,
'Fair' = 25)) +
scale_fill_manual(values = c('E' = '#ca0020',
'F' = '#f4a582',
'H' = '#ffffbf',
'I' = '#92c5de',
'J' = '#0571b0')) +
scale_color_manual(values = c('E' = '#ca0020',
'F' = '#f4a582',
'H' = '#ffffbf',
'I' = '#92c5de',
'J' = '#0571b0')) +
theme_bw()
diamonds %>%
slice(1:20) %>%
ggplot(aes(x = x,
y = y,
shape = cut,
fill = color,
color = color)) +
geom_point(size = 3.5,
alpha = 0.5,
color = 'black') +
geom_point(size = 3,
alpha = 0.8) +
scale_shape_manual(values = c('Ideal' = 21,
'Premium' = 22,
'Good' = 23,
'Very Good' = 24,
'Fair' = 25)) +
scale_fill_manual(values = c('E' = '#ca0020',
'F' = '#f4a582',
'H' = '#ffffbf',
'I' = '#92c5de',
'J' = '#0571b0')) +
scale_color_manual(values = c('E' = '#ca0020',
'F' = '#f4a582',
'H' = '#ffffbf',
'I' = '#92c5de',
'J' = '#0571b0')) +
theme_bw()
创建于 2024 年 11 月 13 日,使用 reprex v2.1.1
您可以覆盖填充的美观性以获得您正在寻找的结果:
diamonds %>%
slice(1:20) %>%
ggplot(aes(x = x,
y = y,
shape = cut,
fill = color)) +
geom_point(size = 3,
alpha = 0.8) +
scale_shape_manual(values = c('Ideal' = 21,
'Premium' = 22,
'Good' = 23,
'Very Good' = 24,
'Fair' = 25)) +
scale_fill_manual(values = c('E' = '#ca0020',
'F' = '#f4a582',
'H' = '#ffffbf',
'I' = '#92c5de',
'J' = '#0571b0')) +
theme_bw()+
guides(
fill = guide_legend(
title = 'color',
override.aes = list(
shape = 21,
fill = c('#ca0020', '#f4a582', '#ffffbf', '#92c5de', '#0571b0'),
color = rep('#000000',5)
)
)
)