3

My dataset is formed by 4 columns, as shown below: enter image description here

The two columns on the left represent the coordinates XY of a geographical structure, and the two on the left represent the size of "each" geographical unit (diameters North-South and East-West)

I would like to graphically represent a scatterplot where to plot all the coordinates and draw over each point an ellipse including the diameters of each geographical unit.

Manually, and using only two points, the image should be like this one: enter image description here

How can I do it using ggplot2?

You can download the data here

antecessor
  • 2,688
  • 6
  • 29
  • 61
  • 1
    It is more likely that you will get a good, and prompt, response if you provide [a complete minimal reproducible example](http://stackoverflow.com/help/mcve) to go along with your question. Something we can work from and use to show you how it might be possible to answer your question. – Eric Fail Feb 07 '18 at 17:17
  • Hope my manual image helps! – antecessor Feb 07 '18 at 17:26
  • 1
    It does, differently. It's great! Did you look at the article I liked to? What is next is to make some data that can be used, i.e. `df <- data.frame(X = c(-4275, -4855, ...), Y = (1145, 1330, ...), ....)` Again, it's all explained in detail in the post I liked to. – Eric Fail Feb 07 '18 at 17:33
  • 2
    If someone wants to try to help you, they will need your data. To get data from an image is hard. Instead, please post valid R code to generate the data so it can by copy/pasted. – Gregor Thomas Feb 07 '18 at 17:40
  • I added a link to download the data – antecessor Feb 07 '18 at 17:52

2 Answers2

5

Use geom_ellipse() from ggforce:

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

Created on 2019-06-01 by the reprex package (v0.2.1)

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
3

I'm not adding any new code to what Claus Wilke already posted above. All credit should go to Claus. I'm simply testing it with the actual data, and showing OP how to post data,

Loading packages needed

# install.packages(c("tidyverse"), dependencies = TRUE)
library(tidyverse)

Reading data,

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

showing data,

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

loading more packages needed

library(ggforce) # devtools::install_github("thomasp85/ggforce")

executing

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

enter image description here

Eric Fail
  • 8,191
  • 8
  • 72
  • 128