How to change ggplot legend labels and names with two layers? - r

I am plotting the longitude and latitude coordinates of two different data frames in São Paulo map using ggmap and ggplot packages and want to label manually each legend layer:
update: I edited my code below to become fully reproducible (I was using the geocode function instead of get_map).
update: I would like to do this without combining the data frames.
require(ggmap)
sp <- get_map('sao paulo', zoom=11, color='bw')
restaurants <- data.frame(lon=c(-46.73147, -46.65389, -46.67610),
lat=c(-23.57462, -23.56360, -23.53748))
suppliers <- data.frame(lon=c(-46.70819,-46.68155, -46.74376),
lat=c(-23.53382, -23.53942, -23.56630))
ggmap(sp)+geom_point(data=restaurants, aes(x=lon, y=lat),color='blue',size=4)+geom_point(data=suppliers, aes(x=lon, y=lat), color='red', size=4)
I have looked to several questions and tried different ways without success. Does anyone know how can I insert legend and label the blue points as restaurants and the red points as suppliers?

Now that your code is reproducible (thanks!):
dat <- rbind(restaurants,suppliers)
dat$grp <- rep(c('Restaurants','Suppliers'),each = 3)
ggmap(sp) +
geom_point(data=dat, aes(x=lon, y=lat,colour = grp),size = 4) +
scale_colour_manual(values = c('red','blue'))

Related

How to overlay a heat map onto spatial map in R?

I am looking for help in creating a heat map of sorts that can overlay a spatial map. I have multiple data points, such as temperature, along coordinates in a river and would like to be able to visualize the change in temperature along the river (for example: higher temperature would be shown as red and lower temperature blue). Is this possible? I've gone through the different heat map questions and can't seem to piece it together.
This code gives the closest representation I think, but I don't know how to adapt it to what I need:
sample <- data.frame(Longitude=c(-1+rnorm(50,0,.5),-2+rnorm(50,0,0.5),-4.5+rnorm(50,0,.5)),
Latitude =c(52+rnorm(50,0,.5),54+rnorm(50,0,0.5),56+rnorm(50,0,.5)))
CanMap <- readOGR(dsn="gadm-CAN-shapefile",layer="gadm36_CAN_2")
map.df <- fortify(CanMap)
ggplot(sample, aes(x=Longitude, y=Latitude)) +
stat_density2d(aes(fill = ..level..), alpha=0.5, geom="polygon")+
geom_point(colour="red")+
geom_path(data=map.df,aes(x=long, y=lat,group=group), colour="grey50")+
scale_fill_gradientn(colours=rev(brewer.pal(7,"Spectral")))+
xlim(-10,+2.5) +
coord_fixed()

mapping by ggplot2 geom_polygon goes crazy after merging data

I am trying to make a grid containing maps of megaregions in the us. I create a SpatialPolygonDataframe from a shape file. then convert it into a data.frame to use ggplot2. as soon as I add the data into the frame, the polygon plots.
the file containing SpatialPolygon and the data frame are here:
https://drive.google.com/open?id=1kGPZ3CENJbHva0s558vWU24-erbqWUGo
the code is as follow:
load("./data.rda")
prop.test <- proptest.result[which(proptest.result$variable=="Upward N"),]
#transforming the data
# add to data a new column termed "id" composed of the rownames of data
shape#data$id <- rownames(shape#data)
#add data to our
shape#data <- data.frame(merge(x = shape#data, y = prop.test, by.x='Name', by.y="megaregion"))
# create a data.frame from our spatial object
mega.prop <- fortify(shape)
#merge the "fortified" data with the data from our spatial object
mega.prop.test <- merge(mega.prop, shape#data, by="id")
Plotting the first one (mega.prop) works fine:
ggplot(data = mega.prop, aes(x=long, y=lat, group=group), fill="blue")+
geom_polygon()
but plotting after adding the analytics data:
ggplot(data = mega.prop.test, aes(x=long, y=lat, group=group), fill="blue")+
geom_polygon()
In the new plot:
The filling of polygons is messed up. (Is it about the order of points?how?)
two of the polygons are totally missed.
What is the problem?
Thank you very much for your help.
Use geom_map() (which requires a slight tweak of your shapefile for some reason) so you don't have to do the merge/left join.
Also, you merged a great deal of different factors, not sure which ones you want to plot.
Finally, it's unlikely the coastal areas need that fine level of detail. rgeos::gSimplify() will definitely speed things up and you're already distorting areas, so a smaller bit of additional distortion won't impact the results.
library(ggplot2)
library(tidyverse)
shape_map <- tbl_df(fortify(shape, region="Name"))
colnames(shape_map) <- c("long", "lat", "order", "hole", "piece", "region", "group")
prop.test <- proptest.result[which(proptest.result$variable=="Upward N"),]
ggplot() +
geom_map(data=shape_map, map=shape_map, aes(long, lat, map_id=region)) +
geom_map(
data=filter(prop.test, season=="DJF"),
map=shape_map, aes(fill=prop.mega, map_id=megaregion)
)

Incorrect polygon for New York State in ggplot2 maps

I am trying to plot data on New York state map. I am using map_data code. But If you look at polygon, It shows extra piece which is actually not part of New York state? Any ideas how can I apply filter on map data to remove that?
ny <- map_data("state", region="new york")
s1 <- ggplot() + geom_polygon(data=ny, aes(x=long, y=lat))
s2 <- ggplot() + geom_point(data=ny, aes(x=long, y=lat))
grid.arrange(s1, s2, ncol=2)
Output:
geom_point shows correct boundary, but not polygon
The state is actually composed of multiple polygons which are not connected. You just need to tell ggplot which points go with which groups. This is done by mapping your data to the group argument of aes(). See the documentation here, although it would be nicer if they had a map example.
So how do you know which points go with which groups? The data frame returned by map_data() contains a group column. See:
head(ny)
ny$group
To plot the map correctly, use:
ggplot() + geom_polygon(data = ny, aes(x = long, y = lat, group = group))

maps r plot distances with color on these

perhaps you have an idea and could help me. I have following data:
lon.x <- c(11.581981, 13.404954, 9.993682, 7.842104 , 11.741185)
lat.x <- c(48.135125, 52.520007, 53.551085, 47.999008, 48.402880)
lon.y <- c(8.801694, 7.842104 , 11.581981, 13.404954, 7.842104 )
lat.y <- c(53.079296,47.999008, 48.135125, 52.520007, 47.999008)
pred <- c(1,2,3,4,5)
data <- data.frame(cbind(lon.x, lat.x, lon.y, lat.y, pred))
where "lon.x" and "lat.x" are longitude-latitude points of a city and "lon.y" and "lat.y" of another city. So there are pairs of cities.
Now, I would like to make a map in R, with
(1) the direct distances between the x and y coordinates as a line
(2) which will receive a different color based on the variable "pred", this could be red for higher values and blue for lower, or thicker lines with higher values of "pred".
The result should be a simple map, with lines between the cities, that are shaped based on the variable "pred". For instance, the line between the first pair of cities would be thinner, while the last one would be thicker. Is that possible?
I have currently only made to receive a (very complicated) google map of Germany:
library(mapproj)
map <- get_map(location = 'Germany', zoom = 6.2)
ggmap(map)
But I am not sure how to plot points and especially relations between the points that differ based on "pred". Also a very simple map (not so detailed google map) would be best! Any idea? THANKS!
You can use ggplot2 to add lines onto the plot.
library(ggplot2)
library(ggmap)
map <- get_map(location = 'Germany', zoom = 6)
ggmap(map) +
geom_segment(data=data, aes(x=lon.x, xend=lon.y, y=lat.x, yend=lat.y, color=pred), size=2) +
scale_color_continuous(high="red", low="blue")
As for the simpler map, you can download shape files (just the outlines of countries) from www.gadm.org. Level 0 maps are just the country, level 1 have state boundaries, etc. To use one of these, download the file from the website and use this code:
load("DEU_adm0.RData")
gadm <- fortify(gadm)
ggplot(gadm) +
geom_path(aes(x=long, y=lat, group=group)) +
geom_segment(data=data, aes(x=lon.x, xend=lon.y, y=lat.x, yend=lat.y, color=pred), size=2) +
scale_color_continuous(high="red", low="blue")

Plotting Choropleth Maps from KML Data Using ggplot2

How do I plot a choropleth or thematic map using ggplot2 from a KML data source?
Example KML: https://dl.dropbox.com/u/1156404/nhs_pct.kml
Example data: https://dl.dropbox.com/u/1156404/nhs_dent_stat_pct.csv
Here's what I've got so far:
install.packages("rgdal")
library(rgdal)
library(ggplot2)
fn='nhs_pct.kml'
#Look up the list of layers
ogrListLayers(fn)
#The KML file was originally grabbed from Google Fusion Tables
#There's only one layer...but we still need to identify it
kml=readOGR(fn,layer='Fusiontables folder')
#This seems to work for plotting boundaries:
plot(kml)
#And this:
kk=fortify(kml)
ggplot(kk, aes(x=long, y=lat,group=group))+ geom_polygon()
#Add some data into the mix
nhs <- read.csv("nhs_dent_stat_pct.csv")
kml#data=merge(kml#data,nhs,by.x='Name',by.y='PCT.ONS.CODE')
#I think I can plot against this data using plot()?
plot(kml,col=gray(kml#data$A.30.Sep.2012/100))
#But is that actually doing what I think it's doing?!
#And if so, how can experiment using other colour palettes?
#But the real question is: HOW DO I DO COLOUR PLOTS USING gggplot?
ggplot(kk, aes(x=long, y=lat,group=group)) #+ ????
So my question is: how do I use eg kml#data$A.30.Sep.2012 values to colour the regions?
And as a supplementary question: how might I then experiment with different colour palettes, again in the ggplot context?
Plotting maps in R is very often a pain. Here's an answer which largely follows Hadley's tutorial at https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles
library(maptools)
library(rgdal)
library(ggplot2)
library(plyr)
fn='nhs_pct.kml'
nhs <- read.csv("nhs_dent_stat_pct.csv")
kml <- readOGR(fn, layer="Fusiontables folder")
Note: I got a message about orphan holes. I included the following line after reading https://stat.ethz.ch/pipermail/r-help/2011-July/283281.html
slot(kml, "polygons") <- lapply(slot(kml, "polygons"), checkPolygonsHoles)
## The rest more or less follows Hadley's tutorial
kml.points = fortify(kml, region="Name")
kml.df = merge(kml.points, kml#data, by.x="id",by.y="Name",sort=FALSE)
kml.df <- merge(kml.df,nhs,by.x="id",by.y="PCT.ONS.CODE",sort=FALSE,all.x=T,all.y=F)
## Order matters for geom_path!
kml.df <- kml.df[order(kml.df$order),]
nhs.plot <- ggplot(kml.df, aes(long,lat,group=group,fill=A.30.Sep.2012)) +
geom_polygon() +
geom_path(color="gray") +
coord_equal() +
scale_fill_gradient("The outcome") +
scale_x_continuous("") + scale_y_continuous("") + theme_bw()

Resources