我有一個帶有以下代碼的漸變圖:
library(ggplot2)
ggplot(data = world)
geom_sf()
coord_sf(xlim = c(125.8, 129.8), ylim = c(33.0, 38.95), expand = FALSE)
geom_point(aes(Lon, Lat,color=meanrow),data=master,size=3)
scale_color_gradient(low = "green", high="red")
labs(title = "Mean Annual Precipitation from 2001-2020", x = "Longitude", y="Latitude", color="Annual Precipitation (mm)")
#geom_text(aes(Lon, Lat),data=master,label=master$Ename,nudge_y = .1,size=2.8)
geom_hline(yintercept=37, linetype="dashed", color="red")
geom_segment(x=127,y=33, xend=127, yend=37, linetype="dashed", color="red")
它看起來像這樣:

現在我想做的是根據它們在虛線的哪一側來改變點的形狀。這是否可以通過代碼輕松完成,因為我預計我將不得不經常更改邊界線的位置。
編輯:感謝@danlooo 的幫助,我能夠完全按照我的意愿去做,這就是現在的樣子:
x=master$Lon
y=master$Lat
x_intercept <- 127
y_intercept <- 37
shape=case_when(
y > y_intercept ~ "top",
y < y_intercept & x < x_intercept ~ "lower left",
TRUE ~ "other")
ggplot(data = world)
geom_sf()
coord_sf(xlim = c(125.8, 129.8), ylim = c(33.0, 38.95), expand = FALSE)
geom_point(aes(x=Lon, y=Lat,color=meanrow,shape=shape),data=master,size=3)
scale_color_gradient(low = "green", high="red")
labs(title = "Mean Annual Precipitation from 2001-2020", x = "Longitude", y="Latitude", color="Annual Precipitation (mm)")
#geom_text(aes(Lon, Lat),data=master,label=master$Ename,nudge_y = .1,size=2.8)
geom_hline(yintercept=37, linetype="dashed", color="red")
geom_segment(x=127,y=33, xend=127, yend=37, linetype="dashed", color="red")
更新地圖
uj5u.com熱心網友回復:
您可以根據預定義的閾值計算新列中的形狀,該閾值也將用于繪制線條:
library(tidyverse)
y_intercept <- 1
x_intercept <- 5.5
iris %>%
mutate(
x = Sepal.Length,
y = Petal.Width,
shape = case_when(
y > y_intercept & x > x_intercept ~ "top right",
y > y_intercept & x < x_intercept ~ "top left",
TRUE ~ "other"
)
) %>%
ggplot(aes(x, y))
geom_point(aes(shape = shape), size = 5)
geom_vline(xintercept = x_intercept)
geom_hline(yintercept = y_intercept)

由reprex 包(v2.0.0)于 2022-05-04 創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471606.html
上一篇:反轉離散資料的y軸
