How to take the actual contour fill in a ggmap - r

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 instat_contour():
Contour requires singlezat each combination ofxandy`.
I really appreciate any help
Many thanks,
Bill

Related

Adding Coordinates to A Map created in R

I created a map of California subdivided with counties:
CAL17 <- ca_base +
theme_nothing() +
geom_polygon(data = ca_county, fill = NA, color = "yellow") +
geom_polygon(color = "black", fill = NA)
Now I am trying to add the coordinates.
Every time I try to add the coordinates the California map disappears and a single purple dot appears on an X and Y axis plot.
labs17 <- data.frame(lon17, lat17, stringsAsFactors = FALSE) +
ggplot() +
geom_point(data = labs17, aes(x = lon17, y = lat17), color = "White", size = 5) +
geom_point(data = labs17, aes(x = lon17, y = lat17), color = "yellow", size = 4)
CAL17 +
geom_point(data = labs17, aes(x = lon17, y = lat17), color = "black", size = 5) +
geom_point(data = ca_df, aes(x = lon17, y = lat17), color = "purple", size = 2)
and I get the following error
Error in FUN(X[[i]], ...) : object 'group' not found

ggplot2 + geom_point + bubble proportional to size (error "Discrete value supplied to continuous scale")

I am trying to represent dots/bubble corresponding to cities that increase size proportional to population.
despite several attempts, I get always the same error ("Discrete value supplied to continuous scale"), even though my variable is numeric.
Example Data used can be found here
https://github.com/Lulliter/r-ggplot-points-question
Brazil_bound_sf = boundary
Brazil_cit_longlat = list of cities with Population # over few years
Plot does not Work
ggplot() +
geom_sf (data = Brazil_bound_sf,
aes(fill = NULL), alpha = .1,colour = "darkgrey",size = 0.3) +
geom_point(data = Brazil_cit_longlat, aes( x = lon, y = lat, size =
"2016"), fill = "red", color = "grey", alpha = .2) +
scale_size_continuous() +
coord_sf(crs = st_crs(Brazil_bound_sf), datum = NA) +
theme_void()
# Error: Discrete value supplied to continuous scale
Thanks!
This worked for me (I simply replaced "2016" with 2016. This way you are referring to the column in the data frame and not to the string "2016":
ggplot() +
geom_sf (data = x,
aes(fill = NULL), alpha = .1,colour = "darkgrey",size = 0.3) +
geom_point(data = y, aes( x = lon, y = lat, size =
`2016`), fill = "red", color = "grey", alpha = .2) +
scale_size_continuous() +
sf::plot_sf(crs = st_crs(x), datum = NA)

Plotting Heatmap with geom_raster in ggmap

I have a data frame containing longitude, latitude and a intensity variable (var1.pred).
I would like to plot a smooth filled contour plot on a ggmap map. I have done it using a geom_tile plot but it doesn't look smooth. I have decreased the tile sizes but the plot takes up to much storage. What I want to do is use geom_raster to plot a nice clean contour plot over the map object.
Using the bellow code I have done it using ggplot:
raster <- ggplot(idw.output, aes(x = lon, y = lat, z = var1.pred))+
geom_raster(aes(fill = var1.pred), alpha = 0.5) +
scale_fill_gradient(low = "white", high = "blue")+
geom_contour(colour = "white", binwidth = 1) +
labs(fill = "Frequency", title = "Frequency per area", x = 'Longitude', y = 'Latitude')
raster
This returns the plot:
However I cannot work out how to combine this with a ggmap object. I have tried various things such as:
ggmap(fr) + #fr is a map from get_map
ggplot(idw.output, aes(x = lon, y = lat, z = var1.pred))+
geom_raster(aes(fill = var1.pred), alpha = 0.5) +
scale_fill_gradient(low = "white", high = "blue")+
geom_contour(colour = "white", binwidth = 1) +
labs(fill = "Frequency", title = "Frequency per area", x = 'Longitude', y = 'Latitude')
But I get the error:
Error: Don't know how to add o to a plot
I know the issue has something to do with combining a ggplot and ggmap object but I cannot work out how to get it to work. Any help would be appreciated.
Thanks,
Robin
Without a reproducible example, it is impossible to test your code, but it is likely that you can move the ggplot call to the base_layer argument so that you can keep adding geoms.
ggmap(fr, base_layer = ggplot(idw.output, aes(x = lon, y = lat, z = var1.pred))) +
geom_raster(aes(fill = var1.pred), alpha = 0.5) +
scale_fill_gradient(low = "white", high = "blue")+
geom_contour(colour = "white", binwidth = 1) +
labs(fill = "Frequency", title = "Frequency per area", x = 'Longitude', y = 'Latitude')

Creating two geom_tile layers on ggmap

I'm trying to represent two variables (crime and rent prices) on a ggmap using geom_tile. Individually, both maps look great.
heat_crime = ggmap(cmap) +
geom_tile(data = LatLonCounts_crime, aes(x = Longitude, y = Latitude, alpha = Frequency), fill="red") +
scale_alpha(range = c(0, 0.8))
heat_price = ggmap(cmap) +
geom_tile(data = mean_price_per_bedroom_per_tile, aes(x = Longitude, y = Latitude, alpha = mean_price), fill="blue") +
scale_alpha(range = c(0, 0.8))
I would like to add both geom_tiles on the same map. I've tried this:
heat_all = ggmap(cmap) +
geom_tile(data = LatLonCounts_crime, aes(x = Longitude, y = Latitude, alpha = Frequency), fill="red") +
scale_alpha(range = c(0, 0.8)) +
geom_tile(data = mean_price_per_bedroom_per_tile, aes(x = Longitude, y = Latitude, alpha = mean_price), fill="blue") +
scale_alpha(range = c(0, 0.8))
But the geom_tile overlay then combines the price and crime variable into one (as becomes clear when you look at the legend).
How can I keep both variables fully separated and show them both on the same ggmap?

Mapping issue with fill when plotting multiple countries

I am trying to plot a map with Peru and Ecuador, that presents two simple lat and long points (San Jose and Lima in Peru).
I'm having some issue with the fill when incorporating both Peru and Ecuador into my map.
library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)
peru_ecuador <- map_data("world",c("peru", "ecuador"))
# Add study site points (San Jose and Lima)
points <- data.frame(
long = c(-79.81, -77.04),
lat = c(-6.77, -12.04),
names = c("San Jose", "Lima"),
stringsAsFactors = FALSE
)
# Plot the map
ggplot() +
geom_polygon(data = peru_ecuador, aes(x=long, y = lat), fill = "grey40", color
= "grey90", alpha = 1) +
geom_point(data = points, aes(x = long, y = lat), color = "red", size = 2,
alpha = 0.8) +
geom_text(aes(x = long, y = lat, label = c("San Jose", "Lima")), data =
points, size = 2, hjust = 1.3) +
geom_area(x = 10) +
coord_fixed(1.3) +
labs(x = "Longitude", y = "Latitude", size = 2) +
theme_bw(base_size = 5)
I would also really love some advise on how best to resize the plot area window of the map. As in change the x and y axis width and length. When I plot just a map of Peru, some of my map information is cut off by the size of the plot area. Please see below:
peru <- map_data("world","peru")
ggplot() +
geom_polygon(data = peru, aes(x=long, y = lat), fill = "grey40", color =
"grey90", alpha = 1) +
geom_point(data = points, aes(x = long, y = lat), color = "red", size = 2,
alpha = 0.8) +
geom_text(aes(x = long, y = lat, label = c("San Jose", "Lima")), data =
points, size = 2, hjust = 1.3) +
geom_area(x = 10) +
coord_fixed(1.3) +
labs(x = "Longitude", y = "Latitude", size = 2) +
theme_bw(base_size = 5)

Resources