简体   繁体   English

如何在 R 中使用 ggplot2 为点着色?

[英]How do I color points using ggplot2 in R?

I am attempting to graph two variables and then use a gradient colour scheme for visual emphasis - however, it keeps giving me a black scatterplot as if I didn't type the command.我试图绘制两个变量的图形,然后使用渐变颜色方案进行视觉强调 - 但是,它一直给我一个黑色散点图,就好像我没有输入命令一样。

Fake Dataset:假数据集:

Nature_DR = data.frame(Protect_LGA = runif(100, min=0, max=87.5),
                       DR_2011_Protect = runif(100, min=3, max=10))

Here is my code这是我的代码

    ggplot(Nature_DR, aes(x=Protect_LGA, y=DR_2011_Protect)) +
      geom_point() + 
      scale_fill_gradient(low="yellow", high ="green") + 
      geom_smooth(method = 'loess') +
      coord_cartesian(xlim = c(0,87.5), ylim = c(3,10)) + 
      labs(x="Proportion of LGA that is protected", 
           y = "Standardised Death Rates in 2011",
           title = "Relation between Protected Areas and Death Rates")

You have asked ggplot to apply a color gradient to the fill aesthetic, but you have not specified a fill aesthetic.您已要求ggplot将颜色渐变应用于fill美学,但您尚未指定fill美学。 In addition, geom_point() does not use the fill aesthetic, it uses the color aesthetic.此外, geom_point()不使用fill美学,它使用color美学。

So, for example, you could color the points by DR_2011_Protect using the following, which will use the default color gradient:因此,例如,您可以使用以下内容通过DR_2011_Protect为点DR_2011_Protect ,这将使用默认颜色渐变:

   ggplot(Nature_DR, aes(x=Protect_LGA, y=DR_2011_Protect, color=DR_2011_Protect)) +
     geom_point()

Or apply your color gradient to the points by changing which scale you modify:或者通过更改您修改的比例将您的颜色渐变应用于点:

   ggplot(Nature_DR, aes(x=Protect_LGA, y=DR_2011_Protect, color=DR_2011_Protect)) + 
     geom_point() + 
     scale_color_gradient(low="yellow", high ="green")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM