让图例显示填充颜色并让geom_points带有黑色轮廓

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

在下图中,我想让图例显示变量“颜色”的填充颜色。我还希望图中的所有点都有黑色轮廓。看来我可以做其中之一,但不能两者都做(即,图例有颜色,点有黑色轮廓)。我选择了具有黑色轮廓的形状,如“剪切”图例中所示。

类似的问题:

在点周围保留黑色轮廓,但图例全黑

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

r ggplot2
1个回答
0
投票

您可以覆盖填充的美观性以获得您正在寻找的结果:

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)
        )
      )
    )
© www.soinside.com 2019 - 2024. All rights reserved.