I have the following issue. I want to create isolines around spatial points in Europe which represent airports. Around all the data points, I want to create Isolines that represent the Emissions for the flights. The departure of all flights is Zurich and the arrival is around Europe.
df <- data.frame(flight_number = c(1,2,3,4,5,6,7),
dep_lon = c(8.548056,8.548056,8.548056,8.548056,8.548056,8.548056,8.548056),
dep_lat = c(47.45806,47.45806,47.45806,47.45806,47.45806,47.45806,47.45806),
arr_lon = c(11.548056,16.569700,13.548056, 27.825100, 16.569700,11.569700,19.569700),
arr_lat = c(47.45806,48.11030,47.45806,43.23210,48.11030,44.45806,49.45806),
EMISSIONS_KGCO2EQ = c(100,200,300,400,1000,400,200))
ggplot()+
geom_point(data = df, aes(x = dep_lon, y = dep_lat), col = "red")+
geom_point(data = df, aes(x = arr_lon, y = arr_lat), col = "blue")+
geom_contour(data = df, aes(x = arr_lon, y = arr_lat, z= EMISSIONS_KGCO2EQ))
I used this code to plot the points for departure in Zurich and arrival. I want isolines for the "EMISSIONS_KGCO2EQ" for each flight. But what I get is the following error message:
Warning messages:
1: stat_contour(): Zero contours were generated
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
I don't get the error message as there are no. Inf values or calculations where its divided by 0.
What I want is Isolines where the emissions is 0 for Zurich and growing according to the values for each flight with an Isoline for each emissions- value.
Thank you for the help.
If you look at the documentation for geom_contour
is says that the x and y values should be a regular grid. So you need to interpolate a regular grid and use that for your contours. Here is one way of doing it...
library(tidyverse)
library(interp)
interpolated <- interp(df$arr_lon, df$arr_lat, df$EMISSIONS_KGCO2EQ,
duplicate = "mean", #you have duplicated values
output = "grid")
#convert this to a long form dataframe
interp_df <- expand_grid(i = seq_along(interpolated$x),
j = seq_along(interpolated$y)) %>%
mutate(lon = interpolated$x[i],
lat = interpolated$y[j],
emissions = map2_dbl(i, j, ~interpolated$z[.x,.y])) %>%
select(-i, -j)
#then you can use this in your plot
ggplot()+
geom_point(data = df, aes(x = dep_lon, y = dep_lat), col = "red") +
geom_point(data = df, aes(x = arr_lon, y = arr_lat), col = "blue") +
geom_contour(data = interp_df, aes(x = lon, y = lat, z = emissions))
It still doesn't look great with this small dataset, but hopefully you have more than this in your actual data! You will also need to decide how you want to treat duplicates.