我是一个绝对的初学者,在ggplot2
,并试图使用这个软件包绘制美丽的人物。我正在阅读手册,我不明白scale_colour_gradient()
与scale_fill_gradient()
之间的差异。在我的分析中,我将使用“假设”进行观察,因为我不确定我的结论。ggplot2的scale_colour_gradient与scale_fill_gradient
这里是为我工作的原代码:
实验1
erupt <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
geom_raster()
erupt
假设:它绘制使用默认的颜色(即蓝色)爆发的密度
我想进一步试验ggplot2
的scale_colour_gradient()
和scale_fill_gradient()
实验2
erupt <- ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill=density)) +
scale_colour_gradient(low = "white", high = "black")
erupt
现在,我得到了相同的图形作为实验1.这是否意味着,如果我使用补充=在ggplot美学XYZ,我设置的颜色渐变和不填充渐变?要理解为什么我问这个,让我们来看看实验3
实验3
erupt <- ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill=density)) +
scale_fill_gradient(low = "white", high = "black")
erupt
在这里,我已经改变的仅仅是使用scale_fill_gradient
,而不是scale_color_gradient
。但是,图表完全不同。范围从黑到白。
有人能帮我理解发生了什么吗?具体来说,我有两个问题:
a)当我们设置fill = xyz时,它是设置颜色还是填充?
b)scale_fill_gradient
和scale_color_gradient
有什么区别?
我很抱歉,如果我的问题听起来对你来说太基本了。我不是专家,还有很长的路要走。
更新:讨论后,这里的结论是什么:
在实验1:采用连续可变的 “密度” 的情节simplys罢了。
在实验2,我填充“密度”,但是然后覆盖“颜色”。因此,ggplot试图遮蔽栅格图的边界(因为我压倒了颜色),并且与填充无关,因为我不覆盖任何东西。
在实验3,我填充和重写,所以我看到白色和黑色的情节。
为了解决这个问题,我做了两两件事:
实验2: 修复#1:
ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
geom_raster(aes(colour=density)) +
scale_colour_gradient(low = "white", high = "red") +
scale_fill_gradient(low = "white", high = "green")
OR
修复#2:
ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
geom_raster(aes(colour=density)) +
scale_colour_gradient(low = "white", high = "black")
此外,添加到斧头兵的角度来看,这是我做的:
ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
geom_tile(aes(fill=density, col=density)) +
scale_colour_gradient(low = "white", high = "red") +
scale_fill_gradient(low = "white", high = "green")
在输出中,我们将看到瓷砖的边界得到阴影按照密度(即红色),并且填充颜色按照绿色着色。所以,这样,我们就能够展示填充和颜色的效果。
http://docs.ggplot2.org/current/scale_gradient.html – Hackerman
你没有任何东西映射到'color',你只有东西映射到'fill'。这就是为什么改变“颜色”的比例没有任何作用。没有什么是'color',你看到的只是'fill'。 – Axeman
玩弄'geom_tile(aes(fill = density,col = density))'来了解更多。 – Axeman