r - 如何在 ggplot2 R 中为地理/位置质心绘制误差线?
问题描述
我在geom_errorbar
收到错误的参数时遇到问题Error: geom_errorbar requires the following missing aesthetics: x or y, xmin and xmax
。
我有几个数据集,想用它们来创建一个地理 ggplot。下面是一个工作流程和一些示例数据。所需的绘图将具有背景位置数据,在数据帧中计算的质心dat
的质心centroids
和x和ycentroids
误差条/标准偏差范围(即,“Longitude_weighted_sd”和“Latitude_weighted_sd”。
#packages
packages<-c('tidyverse','sf','rgdal','rnaturalearth','ggspatial','raster','sp', 'cowplot',
'dplyr','ggplot2','lubridate','stargazer', 'purrr', 'geosphere', 'purrr')
lapply(packages, library, character.only = T)
library(ggplot2)
library(sf)
library(rnaturalearth)
library(rgdal)
library(ggspatial)
library(spData)
library(cowplot)
library(tidyverse)
#download geographical and upload personal/mock data
world <- ne_countries(scale = "medium", returnclass = "sf")
states <- map_data("state")
data("us_states", package = "spData")
dat <- data.frame(Latitude = c(35.8, 35.85, 36.7, 35.2, 36.1, 35.859, 36.0, 37.0, 35.1, 35.2),
Longitude = c(-89.4, -89.5, -89.4, -89.8, -90, -89.63, -89.7, -89, -88.9, -89),
Period = c("early", "early", "early", "early", "early", "late", "late", "late", "late", "late"),
State = c("A", "A", "A", "T", "T", "T", "T", "A", "A", "A"))
#function to calculate weighted variance, sd, and se
weighted.var <- function(x, w = NULL, na.rm = FALSE) {
if (na.rm) {
na <- is.na(x) | is.na(w)
x <- x[!na]
w <- w[!na]
}
sum(w * (x - weighted.mean(x, w)) ^ 2) / (sum(w) - 1)
}
weighted.sd <- function(x, w, na.rm = TRUE) sqrt(weighted.var(x, w, na.rm = TRUE))
weighted.se <- function(x, w, na.rm = TRUE) sqrt(weighted.var(x, w, na.rm = TRUE))/sqrt(length(x))
#calculate centroids for "early" and "late" periods weighted by "State" observations
centroids <- dat %>%
group_by(Period, State) %>%
mutate(weight = 1/n()) %>%
group_by(Period) %>%
summarise(across(starts_with("L"),
list(weighted_mean = ~ weighted.mean(.x, w = weight),
weighted_sd = ~ weighted.sd(.x, w = weight),
weighted_se = ~ weighted.se(.x, w = weight))))
如果我拿出geom_errorbar
论点,一切都会很好。但是,当我添加它时,我收到错误,geom_error requires the following missing aesthetics:x or y, xmin and xmax
但是,我认为我已经指定了所有内容。下面是 ggplot2 代码。任何帮助将不胜感激!
plot1 <- ggplot(data = world) +
geom_sf(fill = "gray92") + #light gray
geom_polygon(data = states, aes(x = long, y = lat, group = group), #states outline
color = "black", fill = NA) +
geom_point(data = dat, aes(x = Longitude, y = Latitude, color = Period), #background data
alpha = 0.2, size = 1) +
geom_point(data = centroids, aes(x = Longitude_weighted_mean, y = Latitude_weighted_mean,
fill = period), size = 6, pch = 21) + #centroids
geom_errorbar(data = centroids,
aes(ymin = Latitude_weighted_mean - Latitude_weighted_sd,
ymax = Latitude_weighted_mean + Latitude_weighted_sd,
xmin = Longitude_weighted_mean + Longitude_weighted_sd,
xmax = Longitude_weighted_mean + Longitude_weighted_sd), #errorbars
) +
theme_bw() +
coord_sf(crs = "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs") +
coord_sf(xlim = c(-92, -88), ylim = c(33.5, 36.7), expand = TRUE) +
theme(plot.title = element_text(size = 20),
legend.title = element_text(size = 20),
legend.text = element_text(size = 16),
axis.title = element_text(size = 20),
axis.text = element_text(size = 16),
axis.text.x = element_text(angle = 45, hjust = 1),
element_line(color = "black"))+
annotate("text", label = "TN", size = 7, x = -88.3, y = 35.3) +
annotate("text", label = "AR", size = 7, x = -91.7, y = 36) +
annotate("text", label = "MS", size = 7, x = -89, y = 34) +
xlab("Longitude") + ylab("Latitude")
plot1
预先感谢任何愿意提供帮助的人。-纳米
解决方案
问题是您尝试两个通过一个添加错误栏geom_errorbar
,因为错误消息告诉您既没有提供x
也没有y
. 相反,我建议通过两个geom_errorbar
s 添加您的错误栏,如下所示:
library(ggplot2)
library(rnaturalearth)
library(tidyverse)
ggplot(data = world) +
geom_sf(fill = "gray92") + # light gray
geom_polygon(
data = states, aes(x = long, y = lat, group = group), # states outline
color = "black", fill = NA
) +
geom_point(
data = dat, aes(x = Longitude, y = Latitude, color = Period), # background data
alpha = 0.2, size = 1
) +
geom_point(data = centroids, aes(
x = Longitude_weighted_mean, y = Latitude_weighted_mean,
fill = Period
), size = 6, pch = 21) + # centroids
geom_errorbar(
data = centroids,
aes(
x = Longitude_weighted_mean,
ymin = Latitude_weighted_mean - Latitude_weighted_sd,
ymax = Latitude_weighted_mean + Latitude_weighted_sd
)
) +
geom_errorbar(
data = centroids,
aes(
y = Latitude_weighted_mean,
xmin = Longitude_weighted_mean - Longitude_weighted_sd,
xmax = Longitude_weighted_mean + Longitude_weighted_sd
)
) +
theme_bw() +
coord_sf(xlim = c(-92, -88), ylim = c(33.5, 36.7), expand = TRUE) +
theme(
plot.title = element_text(size = 20),
legend.title = element_text(size = 20),
legend.text = element_text(size = 16),
axis.title = element_text(size = 20),
axis.text = element_text(size = 16),
axis.text.x = element_text(angle = 45, hjust = 1),
element_line(color = "black")
) +
annotate("text", label = "TN", size = 7, x = -88.3, y = 35.3) +
annotate("text", label = "AR", size = 7, x = -91.7, y = 36) +
annotate("text", label = "MS", size = 7, x = -89, y = 34) +
xlab("Longitude") +
ylab("Latitude")
推荐阅读
- twilio - 如何订阅 twilio number 的错误信息?
- javascript - PHP 将简单的 3 行 JavaScript 转换为等效的 PHP
- c++ - C ++使用函数获取向量内对象的引用
- ios - 如何在 onReceive 计时器关闭 SwiftUI iOS 中导航另一个视图
- c# - 我怎样才能让相机也上下轨道?
- android - 在真机上测试Android App时如何获取错误信息?
- c# - 访问抽象类列表中的信息
- python - 手动调整 torch.nn.Module 的参数
- java - 音频样本混合或改变音量会导致饱和度和白噪声
- tensorflow - 在 Keras 中创建 VAE 时,调用方法未实现运行时错误。模型子类化