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")
Related
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)
I would like to draw points and text labels for multiple locations defined by longitude and latitude. It works fine for a single location, but I struggle to extend this in geom_point for a group of data.
library(ggplot2)
require(maps)
GER <- map_data("world", region = "Germany")
ggplot(GER, aes(x = long, y = lat, group = group)) +
geom_polygon(fill="lightgray", colour = "white")+
geom_point(aes(x = 13.404954, y = 52.520008), color="red")+
geom_text(aes(x = 13.404954, y = 52.520008), label = "Berlin", color = "red", nudge_y = .2)
df <- data.frame(name = c("Berlin", "Hamburg"),
long = c(13.404954, 9.993682),
lat = c(52.520008, 53.551086))
# How to do it?
ggplot(GER, aes(x = long, y = lat, group = group)) +
geom_polygon(fill="lightgray", colour = "white")+
geom_point(aes(x = df$long, y = df$lat), color="red")
Two different data sets have to be made explicit
ggplot(data = GER, aes(x = long, y = lat, group=group)) +
geom_polygon(fill="lightgray", colour = "white")+
geom_point(data = df, aes(x = long, y = lat, group=name), color="red")
I have a data.frame (CAMANOC) of species and Latitude/Longitude.
I applied a function and a for-loop to krige my data and usegrid.arrange to plot all plot in one window.
Unfortunately, when I use scale_fill_gradient2 the color gradient appears to have min limit and max limit from all my values. When scale_fill_gradient2 is masked, everythings work.
Here is my code:
CAMANOCKrigeage <- function(CAMANOC,CAMANOC.grid2){
plotCAMANOC <- list() #Defining a list to save my maps
for (j in 1:8) #Calling species
{
var.exp <- variogram((CAMANOC[[j]])~1, CAMANOC)
var.mod=vgm(psill = 1, model = c("Exp", "Mat", "Sph"), nugget = 100, range = 2000,alpha = c(0, 45, 90, 135))
var.fit=fit.variogram(var.exp,var.mod)
krigeage = krige(formula = (CAMANOC[[j]])~1,CAMANOC,CAMANOC.grid2,model = var.fit)
krig.output=as.data.frame(krigeage)
names(krig.output)[1:4]<-c("long","lat","var1.pred","var1.var")
layer1 <- c(geom_tile(data=krig.output,aes(fill=var1.pred)))
plotCAMANOC[[j]] <- ggplot(data = krig.output,aes(x = long, y = lat)) +
geom_polygon(data = oceanfort, aes(x = long, y = lat, group = group), fill = "white") +
geom_polygon(data = coastlinefort, aes(x = long, y = lat, group = group), fill = "grey") +
geom_path(data = landfort, aes(x = long, y = lat, group=group)) +
ggtitle(names(CAMANOC)[j]) +
coord_cartesian(xlim = c(-6,1),ylim = c(48,51)) +
scale_fill_gradient2(name=bquote(atop("", ~cell.cm^-3)),
high="green", mid="blue", low="red",
space="Lab", midpoint = median(krig.output$var1.pred)) +
labs(x = "Longitude") +
labs(y = "Latitude") +
theme_bw() +
layer1
}
return(plotCAMANOC)
}
plotCAMANOC <- CAMANOCKrigeage(CAMANOC,CAMANOC.grid2)
do.call(grid.arrange,plotCAMANOC)
For a better view, two maps I have:
I am sorry for the lack of reproductible example, I am not familiar with that.
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?
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)