I am new in R and I want to plot a map with the distribution/ contour of pollutants and I have trouble doing that.
I a sample of my dataset is:
> head(noxco2_contour)
latitude longitude NOX.CO2
1 52.45060 -1.933200 0.01240829
2 52.45061 -1.933198 0.01045806
3 52.45060 -1.933207 0.01235869
4 52.45059 -1.933202 0.01083601
5 52.45059 -1.933199 0.01243808
6 52.45058 -1.933207 0.01379813
After getting the needed map with ggmap, I'm trying to overplot filled contours of NOX.CO2. and I am using this code:
`ggmap(west_midlands, extent = "panel") + geom_density2d(data = noxco2_contour, aes(x = longitude, y = latitude, z=NOX.CO2), size = 0.3, contour=T) +
stat_density2d(data = noxco2_contour,
aes(x = longitude, y = latitude, z= NOX.CO2, fill = ..level.., alpha = ..level..), size = 0.05,
geom = "polygon") + scale_fill_gradient(low = "green", high = "red") +
scale_alpha(range = c(0, 1), guide = FALSE)
However I am getting the warning message of: ' Warning: Ignoring unknown aesthetics: z' and practically the fill is NOT the real values of the NOX.CO2 but a level OF unknown value/matterial. the image that i get is1 which is not the correct one.
I have checked the following relevant links Filled contour plot with R/ggplot/ggmap 3 but I get different errors and still not the correct fill. When using:
ggmap(west_midlands, extent = "panel") + geom_density2d(data = noxco2_contour, aes(x = longitude, y = latitude), size = 0.3) +
+ stat_density2d(data = noxco2_contour,
+ aes(x = longitude, y = latitude, group= NOX.CO2, colour= NOX.CO2), size = 0.05,
+ geom = "polygon") + scale_fill_gradient(low = "green", high = "red") +
+ scale_alpha(range = c(0, 1), guide = FALSE)
I get the following: Computation failed in stat_density2d()
:
missing value where TRUE/FALSE needed
When I am using this code
`ggmap( west_midlands, extent = "panel" ) +
+ stat_contour(data = noxco2_contour, geom="contour", bins=20,
+ aes(x = longitude, y = latitude, z = NOX.CO2, fill = ..level.. ) ) +
+ scale_fill_continuous(name = "NOx(ppb)/CO2(ppm)", low = "yellow", high = "red" )
I am getting this Computation failed in
stat_contour():
Contour requires single
zat each combination of
xand
y`.
I really appreciate any help
Many thanks, Bill