为什么不是传说GGPLOT2结合手动充气和刻度值?(Why won't the ggplot

2019-07-31 17:07发布

预期的行为

如果我创建GGPLOT2的阴谋,并使用独立的,也就是说,外形和填充尺度划定的数据,我希望传说将“白色”填充点(看起来空心)和“黑色”填充点之间划定(不看空心)。

在下面的示例代码,为Windows图例项目应该是白色空心点,并为Linux应该是一个黑色填充点

实际行为

“操作系统”下图例项目描绘了什么是显然不同的操作系统,其百分点图形上的不同罢了,显然得出两个视觉上的相同点。 在下面的示例代码,Windows和Linux在图例显示为黑色区分空心点,即使它们正确地画出不同的情节本身。

样地

样地与传说中的破填充行为http://f.cl.ly/items/2w2i09103Q2p0i3M1Y2d/BrokenFillRplot.png

示例代码

library(ggplot2)

x <- rnorm(n = 30)
y <- rnorm(n = 30)
treatment <- rep(c("red", "green", "blue"), times = 20)
operatingSystem <- rep(c("Windows", "Linux"), times = 30)

dd <- data.frame(x, y, treatment, operatingSystem)

fillScaleValues <- c(
  "Windows" = "white",
  "Linux" = "black"
)

shapeScaleValues <- c(
  "red" = 21,
  "green" = 22,
  "blue" = 23
)

p <- ggplot(
      aes(x = x, 
          y = y,
          shape = factor(treatment),
          fill = factor(operatingSystem)
      ), data = dd
     )

p <- p + geom_point()
p <- p + scale_fill_manual(values = fillScaleValues, name = "Operating System")
p <- p + scale_shape_manual(values = shapeScaleValues, name = "Treatment")

p

会议信息

R version 2.15.1 (2012-06-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] C/en_US.UTF-8/C/C/C/C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_0.9.2.1       reshape2_1.2.1        plyr_1.7.1            ProjectTemplate_0.4-2
[5] testthat_0.7         

loaded via a namespace (and not attached):
 [1] MASS_7.3-21        RColorBrewer_1.0-5 colorspace_1.1-1   dichromat_1.2-4   
 [5] digest_0.5.2       evaluate_0.4.2     grid_2.15.1        gtable_0.1.1      
 [9] labeling_0.1       memoise_0.1        munsell_0.4        proto_0.3-9.2     
[13] scales_0.2.2       stringr_0.6.1      tools_2.15.1      

Answer 1:

你必须重写在传说中被使用,如在看的形状这个问题 。

因此,使用您的示例代码(感谢明确的,可重复的问题,顺便说一句),所有你需要做的是:

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

它给你你想要的:



文章来源: Why won't the ggplot2 legend combine manual fill and scale values?
标签: r ggplot2