r - 使用scale_shape_manual时图例中的颜色丢失

标签 r ggplot2

我正在尝试使用形状的位置 21 处的填充形状来绘制点。

d <- data.frame(expand.grid(a=letters[1:4],
      b=factor(1:2)), y=rnorm(8),x=rnorm(8))

这非常有效:

ggplot(d, aes(x=x, y=y,fill=a,color=a))  + 
      geom_point(aes(shape=b,fill=a),size=4) +
      scale_shape_manual(values=c(21,22))

我希望形状的轮廓颜色为黑色,所以我认为在 aes() 之外设置 color="black"就可以解决问题:

 ggplot(d, aes(x=x, y=y,fill=a,color=a))  + 
   geom_point(aes(shape=b,fill=a),color="black",size=4) + 
   scale_shape_manual(values=c(21,22))

这在情节中起作用。然而,图例中的颜色全部设置为黑色。

如何获得上面的图例?

最佳答案

由于某种原因,填充图例默认为形状符号 1(实心圆),因此它显示颜色而不是填充美感。将其添加到 ggplot 命令中:

  + guides(fill=guide_legend(override.aes=list(shape=21))) 

enter image description here

关于r - 使用scale_shape_manual时图例中的颜色丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28683854/

相关文章:

json - 在 R 中解析嵌套的 Json 文件

R roxygen2 preref.parsers[[tag]] %||% parse.unknown 中出现错误 : attempt to use zero-length variable name

r - 一个图中的两个 ggplots,靠得很近

r - 在geom_sf_text中,如何在美学上微调x和y?

r - ggvis 带有工具提示的多行

r - ggplotly 没有适用于 'plotly_build' 的方法应用于类 "NULL"if 语句的对象

r - 向脊线图添加显着性括号

r - 制作全幻灯片大小的 ggplot2 输出 {Xaringan}

r - geom_tile 和 facet_grid/facet_wrap 用于相同高度的瓷砖

r - 当 hjust 和 vjust 是字符串时,为什么 geom_text() 会抛出强制错误?