簡體   English   中英

使用R中的ggplot2在散點圖中指向具有單個橢圓的點

[英]Points in a scatterplot with individual ellipses using ggplot2 in R

我的數據集由4列組成,如下所示: 在此輸入圖像描述

左邊的兩列代表地理結構的坐標XY,左邊的兩列代表“每個”地理單位的大小(直徑南北和東西)

我想以圖形方式表示一個散點圖,其中繪制所有坐標,並在每個點上繪制一個橢圓,包括每個地理單位的直徑。

手動,只使用兩個點,圖像應該像這樣: 在此輸入圖像描述

我怎么能用ggplot2做到這一點?

您可以在此處下載數據

使用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()

reprex包創建於2019-06-01(v0.2.1)

我沒有在Claus Wilke上面發布的內容中添加任何新代碼。 所有的功勞都歸功於克勞斯。 我只是用實際數據測試它,並顯示OP如何發布數據,

需要加載包

# 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()

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM