我的数据集由4列组成,如下所示:
左侧的两列表示地理结构的坐标XY,右侧的两列表示“每个”地理单元的大小(南北直径和东西直径)。
我想绘制一个散点图,其中绘制所有坐标并在每个点上绘制一个包括每个地理单元直径的椭圆。
手动地,仅使用两个点,图像应该像这样:
如何使用ggplot2实现它?
您可以在此处下载数据。
![enter image description here](https://istack.dev59.com/kmD1m.webp)
我想绘制一个散点图,其中绘制所有坐标并在每个点上绘制一个包括每个地理单元直径的椭圆。
手动地,仅使用两个点,图像应该像这样:
![enter image description here](https://istack.dev59.com/b6eDu.webp)
您可以在此处下载数据。
geom_ellipse()
:library(ggplot2)
library(ggforce)
d <- data.frame(
x = c(10, 20),
y = c(10, 20),
ns = c(5, 8),
ew = c(4, 4)
)
ggplot(d, aes(x0 = x, y0 = y, a = ew/2, b = ns/2, angle = 0)) +
geom_ellipse() +
coord_fixed()
2019年6月1日创建,使用reprex包(v0.2.1)
# install.packages(c("tidyverse"), dependencies = TRUE)
library(tidyverse)
读取数据,
tbl <- read.table(
text = "
X Y Diameter_N_S Diameter_E_W
-4275 1145 77 96
-4855 1330 30 25
-4850 1612 45 90
-4990 1410 15 15
-5055 1230 60 50
-5065 1503 43 45
-5135 1305 40 50
-5505 1190 55 70
-5705 1430 90 40
-5645 1535 52 60
", header = TRUE, stringsAsFactors = FALSE) %>% as_tibble()
tbl
#> # A tibble: 10 x 4
#> X Y Diameter_N_S Diameter_E_W
#> <int> <int> <int> <int>
#> 1 -4275 1145 77 96
#> 2 -4855 1330 30 25
#> 3 -4850 1612 45 90
#> 4 -4990 1410 15 15
#> 5 -5055 1230 60 50
#> 6 -5065 1503 43 45
#> 7 -5135 1305 40 50
#> 8 -5505 1190 55 70
#> 9 -5705 1430 90 40
#> 10 -5645 1535 52 60
正在加载所需的更多软件包
library(ggforce) # devtools::install_github("thomasp85/ggforce")
执行中
ggplot(tbl, aes(x0 = X, y0 = Y, a = Diameter_E_W, b = Diameter_N_S, angle = 0)) +
geom_ellipsis() + geom_point(aes(X, Y), size = .5) + coord_fixed() + theme_bw()
df <- data.frame(X = c(-4275, -4855, ...), Y = (1145, 1330, ...), ....)
再次强调,所有细节都在我提供的文章中详细解释了。 - Eric Fail