根据渐变图上的经纬度范围改变点形状
Change point shapes based on longitude and latitude range on gradient map
我有一个渐变贴图,代码如下:
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")
updated map
您可以根据预定义的阈值计算新列中的形状,这也将用于绘制线条:
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 package (v2.0.0)
于 2022-05-04 创建
我有一个渐变贴图,代码如下:
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")
updated map
您可以根据预定义的阈值计算新列中的形状,这也将用于绘制线条:
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 package (v2.0.0)
于 2022-05-04 创建