ggplot2图例为什么不包含手动填充和缩放值? [英] Why won't the ggplot2 legend combine manual fill and scale values?
问题描述
预期行为
如果我使用ggplot2创建一个绘图,并使用单独的(比如说)形状和填充比例来描绘数据,我会希望图例会在白色填充点(看起来很空洞)和黑色填充点(看起来不太空)。
在下面的示例代码中,是一个白色的空心点,对于Linux应该是一个黑色的填充点实际行为
操作系统下的图例项目描述了两个视觉上相同的点,它们是明显不同的操作系统,这些操作系统的点在图上用不同的填充清楚地绘制。在下面的示例代码中,Windows和Linux在图例中都显示为无法区分的黑色空心点,即使它们在绘图本身上的绘制方式不同。
在图例http: //f.cl.ly/items/2w2i09103Q2p0i3M1Y2d/BrokenFillRplot.png
示例代码
<$ (n = 30)$ b $< - rnorm(n = 30)
处理< p $ gt; ; 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(治疗),
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
Session Info
R版本2.15.1(2012-06-22)
平台:x86_64-apple-darwin9.8.0 / x86_64(64位)
语言环境:
[1] C / en_US.UTF-8 / C / C / C / C
附加基本软件包:
[1] stats graphics grDevices utils数据集方法基数
其他附加软件包:
[1] ggplot2_0.9.2.1 reshape2_1.2.1 plyr_1.7.1 ProjectTemplate_0.4-2
[5] testthat_0.7
通过命名空间加载(并未附加):
[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] labels_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
您必须覆盖形状这正在图例中使用,如这个问题所示。
因此,使用您的示例代码(顺便提一下,感谢您提供清晰,可重现的问题),您需要做的就是:
p + guides(fill = guide_legend(override.aes = list(shape = 21)))
哪个给你你想要的:
Expected Behavior
If I create a plot with ggplot2 and use separate, say, shape and fill scales to delineate data, I would expect the legend would delineate between "white" filled points (which look hollow) and "black" filled points (which do not look hollow).
In the example code below, the legend item for Windows should be a white hollow point, and that for Linux should be a black filled in point
Actual Behavior
The legend items under "Operating System" depict two visually identical points for what are clearly different operating systems whose points are clearly drawn with different fills on the graph. In the sample code below, both Windows and Linux appear as indistinguishable black hollow points in the legend, even though they're properly plotted differently on the plot itself.
Sample Plot
Sample Code
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
Session Info
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
You have to override the shape that's being used in the legend, as seen in this question.
So using your example code (thanks for the clear, reproducible question, by the way), all you need to do is:
p + guides(fill = guide_legend(override.aes = list(shape = 21)))
Which gives you what you wanted:
这篇关于ggplot2图例为什么不包含手动填充和缩放值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!