Creating two geom_tile layers on ggmap - r

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?

Related

Plotting Points on a map in R Help needed

I am pretty new to R and want to learn how to plot points on R. I have managed to create a map of the UK with the following code
the Trust/longlat.csv data is a list of organisations with Longitude and Lattitude co-ordinates.
Any help greatly appreciated.
library(ggplot2)
library(maps)
worldmap = map_data('world')
knitr::kable(head(worldmap, 20))
ggplot() +
geom_polygon(data = worldmap,
aes(x = long, y = lat, group = group))
ggplot() + geom_polygon(data = worldmap,
aes(x = long,
y = lat,
group = group)) +
coord_fixed(xlim = c(-10,3),
ylim = c(50.3, 59))
ggplot() +
geom_polygon(data = worldmap,
aes(x = long,
y = lat,
group = group)) +
coord_fixed(ratio = 1.3,
xlim = c(-10,3),
ylim = c(50, 59))
library(tidyverse)
ggplot() +
geom_polygon(data = worldmap,
aes(x = long, y = lat,
group = group),
fill = 'gray90',
color = 'black') +
coord_fixed(ratio = 1.3,
xlim = c(-10,3),
ylim = c(50, 59))
ggplot() +
geom_polygon(data = worldmap,
aes(x = long, y = lat,
group = group),
fill = 'gray90',
color = 'black') +
coord_fixed(ratio = 1.3,
xlim = c(-10,3),
ylim = c(50, 59)) +
theme_void()
Data<-read.csv("C:/Users/Digital/Desktop/longlat.csv")

Change fill colour of polygon with ggplot2

I have a simple question which I've asked myself many times already. When I am plotting one or more polygons in R using ggplot2, how can I actually change the filling colour? I do not only want to change the outline of the polygon, but the whole thing.
In my first simple plots (ea_map and hb_map) it works fine to assign the colours "yellow" and "purple", however in my final plot "ea_hb_map" (which I've pictured below), the colours are set back to default.
ea <- readOGR("C:/Users/BASELINE/Eastern Arctic/Summer/2010_EA_S/cis_SGRDREA_20100628_pl_a.shp")
hb <- readOGR("C:/Users/BASELINE/Hudson Bay/Summer/2010_HB_S/cis_SGRDRHB_20100628_pl_a.shp")
ea_map <- ggplot() +
geom_polygon(data=ea, aes(x = long, y = lat, group = group), fill = "red")
plot(ea_map)
hb_map <- ggplot() +
geom_polygon(data=hb, aes(x = long, y = lat, group = group), fill = "purple")
plot(hb_map)
ea_df <- tidy(ea)
hb_df <- tidy(hb)
eastern_arctic_map <- ea_map +
geom_sf(data=world, fill = "antiquewhite1") +
coord_sf(xlim = c(-115, -50), ylim = c(50,83), expand = FALSE)+
scale_y_continuous(breaks = c(50, 60, 70, 80)) +
scale_x_continuous(breaks = c(-50, -70, -90, -110)) +
geom_polygon(data = ea_df, aes(x=long, y=lat, group=group, fill="Eastern Arctic"), alpha=0.4) +
labs(fill = "",
x = "lon",
y = "lat") +
theme_grey(base_size = 9) +
theme(legend.key.size = unit(0.8,"line"))
print(eastern_arctic_map)
ea_hb_map <- eastern_arctic_map +
geom_polygon(data=hb_df, aes(x = long, y = lat, group = group, fill = "Hudson Bay"), alpha=0.4)
print(ea_hb_map)

Adding geom_segment to faceted plot that have free x axis

I am trying to make a faceted plot with a free x axis (scales = "free_x"). The x axis is a categorical variable and I am having problems aligning a geom_segment. Is there an easy way to avoid the issue presented in the following example:
library(tidyverse)
library(HistData)
library(datasets)
data(Cholera)
Cholera
stats <- Cholera %>% group_by(region, water) %>% summarise(mean = mean(cholera_drate), max = max(cholera_drate))
# This works but I want to get rid of empty categories
ggplot() +
geom_boxplot(data = Cholera, aes(x = region, y = cholera_drate)) +
geom_point(data = stats, aes(x = region, y = mean), color = "red") +
geom_segment(data = stats, aes(x = as.numeric(region) - 0.25, xend = as.numeric(region) + 0.25, y = mean, yend = mean), color = "blue") +
facet_wrap(~ water)
# If I use free_x then geom does not align anymore
ggplot() +
geom_boxplot(data = Cholera, aes(x = region, y = cholera_drate)) +
geom_point(data = stats, aes(x = region, y = mean), color = "red") +
geom_segment(data = stats, aes(x = as.numeric(region) - 0.25, xend = as.numeric(region) + 0.25, y = mean, yend = mean), color = "blue") +
facet_wrap(~ water, scales = "free_x")
The first thing to do is to use facet_grid with scales = free_x and then space = free_x to get:
ggplot() +
geom_boxplot(data = Cholera, aes(x = region, y = cholera_drate)) +
geom_point(data = stats, aes(x = region, y = mean), color = "red") +
facet_grid(cols = vars(water), scales = "free_x", space = "free_x") +
geom_segment(data = stats,
aes(x = as.numeric(region) - 0.25, xend = as.numeric(region) + 0.25,
y = mean, yend = mean), color = "blue")
Then you'll see that the real problem is the fact that you're positioning the segment based on the numeric factor of region which changes when you use space = free_x & scales = free_x thereby leaving the segment behind in the first facet as seen below:
The only thing I can think of to solve this is a sort of a hack. We'll create a separate data for the segments from stats and then change the 'Kent' value to 'North' which is the desired position (of the segment left behind) after we do space = free_x & scales = free_x as seen below:
data_lab <- stats
data_lab$region[8] <- "North" ## change Kent to North
ggplot() +
geom_boxplot(data = Cholera, aes(x = region, y = cholera_drate)) +
geom_point(data = stats, aes(x = region, y = mean), color = "red") +
facet_grid(cols = vars(water), scales = "free_x", space = "free_x") +
geom_segment(data = data_lab,
aes(x = as.numeric(region) - 0.25, xend = as.numeric(region) + 0.25,
y = mean, yend = mean), color = "blue")
to get:
I hope it helps.

How do I combine a raster dataframe with a shapefile to create a map of species richness?

I want to plot the raster plot/dataframe on top of the shapefile but I keep getting various errors depending on how I write the code.
I've tried using a + with the finished objects, landmap+critmapped, I've tried adding the codes together and plotting both as data but that didn't work:
I've tried the following, as well as other things...
Thank you for any help/direction.
critmapped<-ggplot(df, aes(x, y, fill = layer)) +
geom_raster() +
scale_fill_viridis_c(na.value = "white") +
labs(fill = "Count") +
theme_minimal() + ggplot() + geom_path(data = land_df, aes(x = long, y = lat, group = group), color = 'black', fill = 'green')
#Error: Don't know how to add ggplot() to a plot
critmapped+landmap2
#Error: Don't know how to add landmap2 to a plot
#The shapefile code
require(rgdal)
land <- readOGR(dsn = "C:/Users/tjef631/Desktop/R Stats/Data/NE_10m_full",
layer = "ne_10m_land")
land_df<-fortify(land)
names(land_df)
landmap<-ggplot() + geom_path(data = land_df, aes(x = long, y = lat, group
= group),
color = 'black', fill = 'green')
landmap
#the raster/dataframe code
critmapped<-ggplot(df, aes(x, y, fill = layer)) +
geom_raster() +
scale_fill_viridis_c(na.value = "white") +
labs(fill = "Count") +
theme_minimal()
critmapped

How to take the actual contour fill in a ggmap

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

Resources