0

I am trying to plot lat long points on a plot using ggplot2 in R. The axes are lats on y and longs on x. I want a given location point to be the center of my plot and rest of the points on the scatter plot with respect to how far they are from this point (these points' lat long values are coming from a data frame). How can that particular point be at the center of it all? I tried making two separate geom_point layers and added the one point I want in the center first, and then added the second geom layer with the rest of the data. But it doesn't work. I also tried coord_fixed by using the lat long limits from the first geom layer when I only plotted the main center point on plot, but after adding second layer, it does not remain in the center. I also wonder why is there no function or attribute to set the center of the plot around a particular point, so that rest of the points can fall wherever on the plot, but the focus point is there where I want, but maybe it is too specific of a thing. Also, could the units on the axes be converted to meters?

bee5911
  • 516
  • 4
  • 9
  • 1
    Can you please post a data example with `dput` and a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). In general, it sounds that you have to do the data preprocessing before plotting with `ggplot` – starja Jun 10 '20 at 19:25
  • Hi @starja, I am pasting the dput output of my data.frame object: structure(list(latitude = c(38.55042047, 38.47350069, 38.65784584, 38.50677377, 38.6374478, 38.52697863, 38.537173, 38.56433456, 38.6374478, 38.60960217), longitude = c(-121.3914158, -121.4901858, -121.4621009, -121.4269508, -121.3846125, -121.4513383, -121.4875774, -121.4618826, -121.3846125, -121.4918375)), row.names = c(NA, 10L), class = "data.frame"). And these are the coords for my center point: x=-121.4282497, y=38.4138742. – bee5911 Jun 11 '20 at 05:37

1 Answers1

2

The easiest way to do this is figure out what you want the range of your x axis to be, and the range that you want your y axis to be. Measure the distance along the x axis to the furthest point from your target point, and just make sure the x axis range is this big on both sides. Do the same for the y axis.

To demonstrate, I'll make a random sample of points with x and y co-ordinates, each made small and black:

set.seed(1234) # Makes this example reproducible

df <- data.frame(x = rnorm(200), y = rnorm(200), colour = "black", size = 1)

Now I'll choose one at random as my target point, making it big and red:

point_of_interest            <- sample(200, 1)
df$colour[point_of_interest] <- "red"
df$size[point_of_interest]   <- 5

So let's work out the furthest points North-South and East-West from our target and calculate a range which would include all points but have the target in the centre:

max_x_diff <- max(abs(df$x[point_of_interest] - df$x))
max_y_diff <- max(abs(df$y[point_of_interest] - df$y))
x_range    <- df$x[point_of_interest] + c(-max_x_diff, max_x_diff)
y_range    <- df$y[point_of_interest] + c(-max_y_diff, max_y_diff)

And now we just need to plot:

ggplot(df, aes(x, y, colour = colour, size = size)) +
  geom_point() +
  scale_colour_identity() +
  lims(x = x_range, y = y_range) +
  scale_size_identity() +
  coord_equal()

enter image description here

We can see that even though our target is well off the center of the cluster, the target remains in the center of the plot.

With regards to changing latitude and longitude to meters, this requires a co-ordinate transformation. This has been answered many times on Stack Overflow and I won't duplicate those answers here. You could check out packages like rgdal or perhaps SpatialEpi which has the latlong2grid function.

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Thanks @allan-cameron, however, I am stuck at making the 'point_of_interest'. First, when I was trying out my code (before asking this question), I was giving this point manually by geom_point(aes(x=..., y=...), colour="red", size=5) as it wasn't inside the data frame. But even if I copy this value inside my csv from which this df derives, how do I extract two cell values from two different columns as one point_of_interest to plot at the center? – bee5911 Jun 10 '20 at 22:37
  • @stackuser if your data frame is called `df` and you have a column called `longitude` plus another called `latitude`, you just find the row of your df that contains the co-ordinates. Suppose the point of interest is in your first row. Then you do `df$longitude[1]` to get the x value and `df$latitude[1]` to get the y value. – Allan Cameron Jun 10 '20 at 23:08
  • I did that part, but there are errors when I try to assign colour and size to those x and y values. "Error in `$<-.data.frame`(`*tmp*`, colour, value = character(0)) : replacement has 0 rows, data has 300" – bee5911 Jun 10 '20 at 23:22
  • 2
    Let's suppose your data frame from above is called `df`. Then you have to add the columns colour and size with `df$colour <- "black"` and `df$size <- 1`, and you can add your point of interest with `df <- rbind(df, data.frame(latitude = 38.4138742, longitude = -121.4282497, colour = "red", size = 5))` – starja Jun 11 '20 at 08:19