【问题标题】:How to assign a gradient of color to plotted points based on values of numeric column in ggplot如何根据ggplot中数字列的值为绘制点分配颜色渐变
【发布时间】:2019-12-24 18:58:28
【问题描述】:

我有一个包含大量观察结果的大型数据框。数据框名为totdets_per_month,第一行数据如下所示:

  month yearcollected deploy_long deploy_lat total
  <ord>         <int>       <dbl>      <dbl> <int>
1 Jan            2016       -54.6       39.9     1
2 Jan            2016       -54.6       39.9     2
3 Jan            2016       -54.6       39.9     9
4 Jan            2016       -54.4       39.9     9
5 Jan            2016       -54.4       39.9     2
6 Jan            2016       -54.4       39.9     2

我有一个坐标范围内所有月份的数据,

我正在尝试将这些坐标映射为点,但根据我的数字列 total 将这些点分配给颜色渐变。

这是我绘制数据框的代码

#Importing a map
world = ne_countries(scale = "medium", returnclass = "sf")


#map of total detections in each zone per month
ggplot(data = world) +
  geom_sf(fill = "lightgrey") +
  coord_sf(xlim=c(-64.5,-62.8), ylim=c(42.7,45), expand = FALSE) +
  geom_rect(data = rect1, aes(xmin = x1, xmax = x2, ymin = y1, ymax = y2), fill = NA, color = "black", size = 1) +
  geom_point(data = totdets_per_month ,
             mapping = aes(x = deploy_long, 
                           y = deploy_lat,
                           fill = total),
             pch = 21) +
  scale_colour_gradient(low="black", high="green") + 
  theme(panel.background = element_rect(fill = "#add8e6"),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        axis.ticks = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank()) +
  facet_wrap(vars(month))

这是代码输出的图像

如您所见,这些点都是一种颜色。有人知道如何解决这个问题吗?

【问题讨论】:

  • 从您发布的示例数据来看,典型值似乎小于 10,但色阶的最大值为 >2000。你确定你没有一个或两个极值并且剩余的值接近1吗?另一个尝试的选项是将color=total 添加到您的 aes 定义中。
  • 是的,你完全正确。有一些极端值,但为什么那些甚至没有显示在地图上。另外,如果这是原因,是否有更好的方法来映射它以显示所有值?此外,当我在 aes 定义中尝试 color = total 以及填充时,我遇到了同样的问题
  • 请注意色阶是从黑色变为蓝色而不是绿色。您的 aes 定义有问题。在您的 aes 中,您指定了 fill=total,但您只定义了scale_colour_gradient,首先将scale_colour_gradient 更改为scale_fill_gradient,然后进行绘图。
  • 是的,这就是问题所在。我想我必须尝试平均我的数据以尝试获得更小的值

标签: r ggplot2 colors


【解决方案1】:

这是基于我上面的 cmets 的综合答案:

请注意色阶是从黑色变为蓝色而不是绿色。您的 aes 定义有问题。在您的 aes 中,您指定了“fill=total”,但您只定义了scale_colour_gradient,首先将scale_colour_gradient 更改为scale_fill_gradient

#create sample data
x<- runif(20, 1, 20)
y<-x^4
df<-data.frame(x, y)

library(ggplot2)
#this is a linear scale for the colors
ggplot(df) + geom_point(aes(x=x, y=y, fill=y), pch=21, size=4) +
  scale_fill_gradient(low="black", high="green")

在上面的图中,y 的几个非常大的值导致大多数剩余点绘制黑色。为了调整这种倾斜的色标,您需要手动指定颜色中断。 在下面的示例中,我使用cut 函数将我的范围除以 10 的幂,并将中断分配给不同的颜色值。

#create color ramp
colfunc <-colorRampPalette(c("black", "green"))

#adjust scaling of the colors and 
#  add custom color color to dataframe
df$color<-cut(y, breaks=c(0, 10, 100, 1000, 10000, 100000, Inf))
df$color<-factor(df$color, labels=colfunc(5))

ggplot(df) + geom_point(aes(x=x, y=y, fill=color), pch=21, size=4) +
  scale_fill_identity(guide="legend", labels=c(10^(1:5)))

现在该图可以更好地显示分布。

【讨论】:

    猜你喜欢
    • 2020-04-27
    • 2016-05-18
    • 2017-04-27
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    相关资源
    最近更新 更多