ggplot2中的scale_colour_gradient与scale_fill_gradient [英] scale_colour_gradient vs. scale_fill_gradient in ggplot2

查看:297
本文介绍了ggplot2中的scale_colour_gradient与scale_fill_gradient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绝对是 ggplot2 的初学者,并尝试使用此程序包绘制精美的图形.我正在阅读本手册,但我不了解 scale_colour_gradient() scale_fill_gradient()之间的区别.在我的分析中,我将使用假设"进行观察,因为我不确定我的结论.

I am an absolute beginner in ggplot2 and trying to draw beautiful figures using this package. I am going through the manual and I didn't understand the difference between scale_colour_gradient() vs. scale_fill_gradient() In my analysis, I will use "hypothesis" for observations because I am unsure about my conclusions.

这是对我有用的原始代码:

Here's the original code that worked for me:

实验1

  erupt <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
    geom_raster() 
  erupt

假设:它使用默认颜色(即蓝色)绘制喷绘密度

Hypothesis: It plots the density of eruptions using the default color (i.e. blue)

我想进一步尝试 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的美学设计中使用fill = xyz,那么我将设置颜色渐变而不是填充渐变吗?要了解我为什么要问这个,让我们看一下实验3.

Now, I got the same graph as Experiment 1. Does it mean that if I use fill= xyz in ggplot's aesthetics, I am setting the color gradient and NOT fill gradient? To understand why I am asking this, let's see Experiment 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 .但是,该图是完全不同的.从黑色到白色.

Here, the only thing I have changed is using scale_fill_gradient instead of scale_color_gradient. However, the graph is completely different. It ranges from Black to White.

有人可以帮我了解发生了什么吗?具体来说,我有两个问题:

Can someone please help me understand what's going on? Specifically, I have two questions:

a)当我们设置fill = xyz时,它是设置颜色还是填充?

a) When we set fill = xyz, does it set color or the fill?

b) scale_fill_gradient scale_color_gradient 有什么区别?

如果我的问题对您来说太基础了,我深表歉意.我不是专家,任重道远.

I apologize if my question sounds too basic for you. I am not an expert and have a long way to go.

更新:经过讨论,结论如下:

Update: After discussion, here's what the conclusion is:

实验1 中:该图使用连续变量密度"简单地填充.

In Experiment 1: the plot simplys fills using the continuous variable "density".

实验2 中,我填写密度",但随后覆盖颜色".因此,ggplot尝试着色栅格图的边框(因为我覆盖颜色)并且与填充无关,因为我没有覆盖任何内容.

In Experiment 2, I am filling "density" but then overriding "color". So, ggplot tries to shade the border of the raster graph (because I am overriding color) and has nothing to do with fill because I am not overriding anything.

实验3 中,我正在填充和覆盖,所以我看到白色和黑色的图.

In Experiment 3, I am filling and overriding so I see white and black plot.

要解决此问题,我做了两件事:

To fix this, I did two things:

实验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")

此外,要说明的是斧头侠,这是我所做的:

Also, to add to axeman's point, Here's what I did:

  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") 

在输出中,我们将看到瓷砖的边框按照密度(即红色)着色,而填充色按照绿色着色.因此,通过这种方式,我们可以演示填充和颜色的效果.

In the output, we will see that the borders of the tiles get shaded as per density (i.e. red color) and the fill colors get shaded as per green color. So, this way, we are able to demonstrate effects of fill and color.

推荐答案

ggplot2 中, color fill 分别映射. color 是指点和线的颜色,而 fill 是指条形填充(即内部颜色),密度填充等.如果您映射 fill = xyz aes()中的code>,然后使用 scale_color _... ,它不会执行任何操作,因为您必须使用控制 fill > scale_fill _... .如果在这种情况下映射 color ,它将改变浓度周围线条的颜色;那么您可以调用 scale_color _... 进行更改.

In ggplot2, color and fill are mapped separately. color refers to point and line color, whereas fill refers to bar fill (i.e. inner color), density fill, etc. If you map fill=xyz within aes() and then use scale_color_..., it won't do anything since you have to control fill with scale_fill_.... If you map color in this case, it will alter the color of the line surrounding the density; then you could call scale_color_... to alter it.

这篇关于ggplot2中的scale_colour_gradient与scale_fill_gradient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆