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)
Related
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")
I'm using ggmap to plot some filled points across the ocean but they overlap so I used colour=black to give them an outline. When I use pch=21 the legend doesn't change and the points are outlined in black like I want. But, I am trying to get 3 different symbols on the map as well. When I specify the different symbols my points in the legend all turn black.
Here is some sample data and the code I used to get the map with the correct legend but wrong symbols
#library
library(ggmap)
library(mapdata)
library(ggplot2)
#sample
mapoc_temp = data.frame(longitude= c(-64.5, -63.1, -62.4, -62.2, -66.0, -61.9),
latitude= c(42.7, 44.8, 45.8, 45.6, 43.0, 40.2),
Zone = sample(c(1,4,6,7), 6, replace = T),
location = sample(c(1,2,3), 6, replace = T))
#map
canada = map_data("worldHires")
ggplot(data = canada) +
borders("world", xlim = c(-130, -60), ylim = c(20, 50), colour = "black", fill = "grey50") +
geom_polygon(data = canada, aes(x=long, y = lat, group = group), fill = "grey50") +
#coordinates of my map
coord_sf(xlim=c(-84, -41), ylim=c(24,51), expand = FALSE) +
#map the receiver locations
geom_point(data = mapoc_temp,
mapping = aes(x = longitude,
y = latitude,
fill = Zone),
pch = 21,
size = 15, colour = "black") +
#fill the zones
scale_color_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361")) +
scale_fill_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361"))
and this is what the map looks like
When I try to add the symbols, I get the correct symbols but my legend isn't correct anymore. Below is the code and image.
ggplot(data = canada) +
borders("world", xlim = c(-130, -60), ylim = c(20, 50), colour = "black", fill = "grey50") +
geom_polygon(data = canada, aes(x=long, y = lat, group = group), fill = "grey50") +
#coordinates of my map
coord_sf(xlim=c(-84, -41), ylim=c(24,51), expand = FALSE) +
#map the receiver locations
geom_point(data = mapoc_temp,
mapping = aes(x = longitude,
y = latitude,
fill = as.factor(Zone)),
pch = pch = if_else(mapoc_temp$location == 1,25,
if_else(mapoc_temp$location == 3, 23, 21)),
size = 15, colour = "black") +
scale_x_continuous(label = abs) +
scale_y_continuous(label = abs) +
#fill the zones in with viridis
scale_color_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361")) +
scale_fill_manual(values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361"))
Does anyone know how to fix the legend in the second image so that the points are the correct colours? I don't need the symbols to be in the legend
Actually, your issue is due to a bug described here: https://github.com/tidyverse/ggplot2/issues/2322.
To go over, you can do:
ggplot(data = canada) +
borders("world", xlim = c(-130, -60), ylim = c(20, 50), colour = "black", fill = "grey50") +
geom_polygon(data = canada, aes(x=long, y = lat, group = group), fill = "grey50") +
#coordinates of my map
coord_sf(xlim=c(-84, -41), ylim=c(24,51), expand = FALSE) +
#map the receiver locations
geom_point(inherit.aes = FALSE, data = mapoc_temp,
mapping = aes(x = longitude,
y = latitude,
fill = factor(Zone),
shape = factor(location)),
size = 5) +
scale_x_continuous(label = abs) +
scale_y_continuous(label = abs) +
#fill the zones in with viridis
scale_fill_manual(name = "Zone", values=c("#01579B", "#4FC3F7", "#ffa600", "#ff6361"), breaks = c("1","4","6","7")) +
scale_shape_manual(values = c("1" = 21, "2" = 22, "3" = 24))+
guides(fill = guide_legend(override.aes=list(shape=21)), shape = FALSE)
As mentioned by #Edward, you can get rid of the shape legend by adding guides(shape = FALSE).
Does it answer your question ?
I don't have your data, so I will use the good ol' mtcars dataset. The trick is to assign the shapes manually, otherwise, ggplot will complain "A continuous variable can not be mapped to shape".
mtcars$shape <- ifelse(mtcars$cyl==4, 21, ifelse(mtcars$cyl==6, 23, 25))
ggplot(mtcars, aes(wt, mpg)) +
geom_point(aes(fill = factor(cyl), shape=factor(cyl)), col="black", size=3) +
scale_shape_manual(values=c(21,23,25))
For your own data, try:
mapoc_temp$shape <- factor(mapoc_temp$location + 20)
geom_point(data = mapoc_temp,
mapping = aes(x = longitude,
y = latitude,
fill = factor(Zone),
shape= shape),
size = 15, colour = "black") +
scale_shape_manual(values=c(21,22,23)) + # Must be same length as unique location.
guides(shape=FALSE) + # If you don't want an extra shape legend.
guides(fill = guide_legend(override.aes=list(shape=21))) # A necessary fudge
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
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 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)