ggplot2: two different geom_point with different color styling

I want to create the earthquake graph with the look based on this reference Earthquake viz reference

Data
The data is look like this:

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|

Code used

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)

Output
Result1

Expectation
I'm expecting that the geom_points for the data where its magnitude is below or equal to 5 to have grey color and use transparency gradient (higher magnitude, higher alpha, vice versa).

Other test
I tried to add

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

But the result have no transparency gradient as expected
Result2

Look at scale_alpha()

Is it possible to apply scale_alpha() to only second geom_point()? (The one with Magnitude < = 5)

I'm using this

p <- 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(data = df %>% filter(Magnitude <= 5),
             alpha = .1, size = 1.7, shape = 16, stroke = 0)

Then adding

p + scale_alpha_continuous(range = c(0,1))

I can't see any differences with the the previous one (only p)
image

Once again, my expectation is to make the dots in the second geom_point() have an alpha gradient (the less magnitude, the more transparent) while keeping the >5 Magnitude dots' color solid

I found the solution. For anyone that have the same problem, here is it

ggplot(df, aes(Date, Magnitude, color = Magnitude)) +
  geom_point(data = df %>% filter(Magnitude > 5), aes(size = Magnitude),
             alpha = 0.9, shape = 16, stroke = 1, color = "#d00000") +
  geom_point(data = df %>% filter(Magnitude <= 5), aes(alpha = Magnitude, color = Magnitude),
             size = 1.7, shape = 16, stroke = 1) +
  scale_alpha_continuous(range = c(0.1, 1)) +
  scale_color_gradient(low = "grey", high = "grey") +
  scale_size_continuous(range = c(2,3),limits = c(min(df$Magnitude[df$Magnitude > 5]), max(df$Magnitude)))

This is a little bit different color, but the essence is the same

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.