R ggplot2:不同geom_point的不同颜色和alpha样式取决于y值

brjng4g3  于 2023-02-17  发布在  其他
关注(0)|答案(3)|浏览(193)

我想创建外观基于此引用Earthquake viz reference的地震图

数据data如下所示:

head(df)
|Date      |Time.UTC|Latitude|Longitude|Depth|Depth.Type|Magnitude.Type|Magnitude|Region.Name                |Last.Update     |Eqid   |X  |color  |
|----------|--------|--------|---------|-----|----------|--------------|---------|---------------------------|----------------|-------|---|-------|
|2023-02-10|06:11:56|-0.13   |123.12   |137  |          | M            |2.5      |SULAWESI, INDONESIA        |2023-02-10 06:20|1221400|   |#C4C4C4|
|2023-02-10|06:00:14|-1.79   |100.42   |27   |          | M            |2.9      |SOUTHERN SUMATRA, INDONESIA|2023-02-10 06:10|1221398|   |#C4C4C4|
|2023-02-10|05:59:27|-1.31   |120.44   |10   |          | M            |2.7      |SULAWESI, INDONESIA        |2023-02-10 06:05|1221396|   |#C4C4C4|
|2023-02-10|05:26:25|-6.14   |104.72   |35   |          | M            |3.9      |SUNDA STRAIT, INDONESIA    |2023-02-10 05:35|1221388|   |#C4C4C4|
|2023-02-10|05:10:06|-8.08   |117.78   |18   |          | M            |2.8      |SUMBAWA REGION, INDONESIA  |2023-02-10 05:15|1221377|   |#C4C4C4|
|2023-02-10|04:55:01|0.99    |98.06    |25   |          | M            |3.3      |NIAS REGION, INDONESIA     |2023-02-10 05:05|1221370|   |#C4C4C4|

使用的代码

ggplot(df,aes(Date, Magnitude)) +
  geom_point(data = df %>% filter(Magnitude > 5),
             alpha = 0.9, size = 1.7, shape = 16, stroke = 0,
             color = "#264653")+
  geom_point(aes(color = Magnitude),
             data = df %>% filter(Magnitude <= 5),
             alpha = 1/20, size = 1.7, shape = 16, stroke = 0)

输出

期望我期望geom_points对于其量级小于或等于5的数据具有灰色并使用透明度渐变(量级越大,alpha越高,反之亦然)。而geom_points对于大于5的数据,仅使用纯纯色
其他测试我尝试添加

scale_color_gradient(low=alpha("#BFBFBF",0),high = alpha("#6B6B6B",0.9))

但结果没有预期的透明梯度

yyhrrdl8

yyhrrdl81#

这里是另一个选项。ggplot有一个geom_point,其中数据过滤到Magnitude > 5。这在aes中有color = Magnitude。这使得可以使用scale_color_gradiant2设置这些点的颜色样式,如下所示。其他数据也会被过滤(Magnitude <= 5)和Magnitude被重命名为mag。这样就可以独立于其他点设置这些点的样式。您可以使用geom_point。但这可能会导致一些重叠,并给人以alpha未应用的印象。我使用geom_jitter来避免过度绘制。
我也采取了自由使用不同的大小为不同的子集和一些颜色。

library(tidyverse)

ggplot() +
  geom_point(data = data |> filter(Magnitude > 5), aes(
    Date, Magnitude,
    color = Magnitude 
  ), size = 3, show.legend = FALSE) +
  # here you can also use geom_point
  geom_jitter(data = data |>
    filter(Magnitude <= 5) |> rename(mag = Magnitude), aes(
    Date, mag,
    alpha = mag
  ), color = "grey", size = 1, show.legend = FALSE) +
  scale_colour_gradient2(
    low = "green",
    mid = "orange",
    high = "red",
    midpoint = 6,
    guide = "none",
    aesthetics = "colour"
  )

f45qwnt8

f45qwnt82#

你可以用分段来伪造你的alpha。这比使用scale_alpha(在我看来)给你更好的控制。
我已经调整了外观,使其更接近你的链接示例图。诀窍是将alpha值分配给你的幅值。在这样做的数据框中,你可以影响alpha值在哪里会有什么值。

library(ggplot2)
df <- data.frame(Date = rep(seq(as.Date("2021-01-01"), as.Date("2021-12-31"), by = "day"), 10),
                 Magnitude = runif(3650, max = 5))
## generate your alpha segments
df_seg <- data.frame(y = seq(min(df$Magnitude), max(df$Magnitude), len = 1000), 
                     alpha = seq(1, 0.3, len = 1000))

ggplot(df, aes(Date, Magnitude)) +
  ## use alpha as aesthetic
  ## color for the same look as in your linked example graph
  geom_point(aes(color = Magnitude),
    size = 1.7, shape = 16, stroke = 0
  ) +
  geom_segment(
    data = df_seg, aes(
      y = y, yend = y,
      x = as.Date("2021-01-01"),
      xend = as.Date("2021-12-31"),
      alpha = alpha
    ),
    ## you need to play around with the line width a bit
    linewidth = .2,
    color = "white", 
    ## to remove the alpha legend
    show.legend = F
  ) +

  ## add some random divergent colors
  scale_color_gradientn(values = c(0, .4, 1), colors = c("darkgreen", "darkgreen", "red")) +
  theme_classic() +
  coord_cartesian(expand = F)

我是说我发现添加段来伪造alpha更容易。正如用户AllanCameron正确指出的,这是有争议的。直接将alpha添加到scale_color_gradientn中的颜色也会给予人一个很好的视觉效果,而且确实要短得多。

ggplot(df, aes(Date, Magnitude)) +
  geom_point(aes(color = Magnitude),
             size = 1.7, shape = 16, stroke = 0
  ) +
  scale_color_gradientn(values = c(0, .4, 1), 
                        colors =c(alpha("darkgreen", 0.1), alpha("darkgreen", 0.5), "red")) +
  theme_classic() +
  coord_cartesian(expand = F)

t40tm48m

t40tm48m3#

可以通过更改scale_alpha()中的low和high参数来调整透明度渐变。要使震级较低的地震具有较浅的色调,可以增加low值,例如:

ggplot(df,aes(Date, Magnitude)) +
  geom_point(data = df %>% filter(Magnitude > 5),
             alpha = 0.9, size = 1.7, shape = 16, stroke = 0,
             color = "#264653")+
  geom_point(aes(color = Magnitude),
             data = df %>% filter(Magnitude <= 5),
             alpha = 0.5, size = 1.7, shape = 16, stroke = 0) +
  scale_alpha(limits = c(0, 5), range = c(0.1, 0.5))

相关问题