为什么fill传说有时有点,有时没有?例如,给定

library(ggplot2) # ggplot2_3.4.2

data.frame(x = 1, y = 2) |> 
  ggplot(aes(x, y)) + 
  geom_col(aes(fill = 'a')) +
  scale_fill_grey(start = .5) ->
  p

为什么下面的两个地块在填充图例中没有点,

p # fill legend has no dot

p + geom_point(aes(color = 'b')) # fill legend has no dot

但是下面的情节是否有一个点呢?

p + geom_point(aes(color = 'b', fill = 'a')) # fill legend HAS A DOT

注:此问题与其他类似问题(例如,Avoiding color in fill legend with geom_col and geom_point)的不同之处在于,ggplot2如何决定是否在图例中放置一个点(或点)(而不是如何删除它).

更新

请阅读《S excellent answer》.下面是一些额外的例子(基于他写的东西),我觉得它们很有帮助.

如果我们从

ggplot(mapping = aes(x = 1, y = 2)) + 
  geom_col(aes(fill = c('a', 'b'))) +
  geom_point(
    aes(
      color = c('x', 'y'),
      # fill = c('m', 'n') # (*)
    ), 
    shape = 'circle filled',
    size = 9,
    position = position_jitter(seed = 1)) + 
  # geom_point(aes(fill = 'p'), shape = 'diamond', size = 3) + # (**)
  # geom_point(aes(fill = 'q'), shape = 'plus', size = 4, color = 'white') + # (***)
  scale_color_brewer('COLOR', palette = 'Set1') +
  scale_fill_brewer('FILL', palette = 'Accent')

then we get enter image description here which should be as expected; namely two different fills for the bars and hence two entries in the fill legend FILL, and two different ink colors for the circles (just large points) and hence two entries in the color legend COLOR.

If we uncomment (*) then we get enter image description here which adds two colors to FILL as expected since there are two new mapped values (namely 'm' and 'n'). What's interesting is that ggplot2 also adds the symbols that might be using the new fill colors to FILL. I believe that all the different graphical elements that might use a mapped fill are called "glyphs" so the legends superimposes all the glyphs (filled rectangle plus circle) in the legend. You can see the pattern continue by uncommenting (**) to get: enter image description here which new sports a FILL legend with colored squares, circles and diamonds in it. The extra fill color might be surprising but if you change the shape to 'diamond filled' it makes more sense. One can keep going and uncomment (***) to get: enter image description here which also illustrates a glyph of a different color (not mapped) which also appears in FILL.

A final example of the mechanical superposition of all glyphs happens if you move the geom_col to after all the geom_points: enter image description here where the points in both the plot and legend have become obscured by the rectangles from the geom_col.

推荐答案

你在fill图例中得到一个点,因为你在不同的地理上映射了fill美学,也就是说,你在geom_colgeom_point中都映射了fill美学.

图例反映了映射美学的变量的比例或值的范围,而图例中使用的关键字形(又名符号)反映的是geom,即对于geom_col,您将得到一个矩形,对于geom_point,您将获得一个"点".

如果您在不同的地理位置绘制相同的美学 map ,则两个或所有关键字形都会显示在图例中.

  • 在你的第一个情节中,你只在fill上映射了一次,但只有一个geom_col.因此,fill图例的关键字形是一个矩形.

  • 在您的第二个绘图中,您在color AES上映射的位置添加了geom_point,即fill图例仍然只显示geom_col的关键字形,但现在您也可以获得color图例,它使用geom_point的关键字形,也就是点.

  • 在第三个图中,您还映射了geom_point中的fill aes,这将geom_point的关键点添加到fill图例中.

为了明确这与color个AES上的映射无关,这里有一个更简单的说明:

library(ggplot2) # ggplot2_3.4.2

data.frame(x = 1, y = 2) |>
  ggplot(aes(x, y)) +
  geom_col(aes(fill = "a")) +
  scale_fill_grey(start = .5) ->
p

p +
  geom_point(aes(fill = "a"))

R相关问答推荐

变量计算按R中的行更改

R创建一个数据透视表,计算多个组的百分比

无法将传奇添加到cowplot多情节中

ggplot2中的X轴显示数值,单位为百,而不是十

使用geom_segment()对y轴排序

如何调整曲线图中的y轴标签?

plotly hover文本/工具提示在shiny 中不起作用

DEN扩展包中的RECT树形图出现异常行为

如何根据数据帧中的值从该数据帧中提取值?

优化从每个面的栅格中提取值

创建在文本字符串中发现两个不同关键字的实例的数据框

是否可以将线性模型的p值添加到tbl_summary中

主题(Legend.key=Element_RECT(Fill=&Quot;White&Quot;))不起作用

在同一单元格中创建包含整数和百分比的交叉表

我已经运行了几个月的代码的`Palette()`中出现了新的gglot错误

使用其他DF中的文件名将列表中的每个元素保存到文件中

将某个阈值以下的列中的值分类到不同的列中,否则保持该列的原样

R中从因数到数字的转换

将`magick`对象转换为原始向量

使用相对风险回归计算RR