R Adding Legend to ggplot - r
I am trying to create a legend denoting how the plot is color coded. However, this legend is not showing.
Here is what the output currently is:
Does this have something to do with the current plot already taking up the entire space?
Here is my code below to create the plot (and attempt to add a legend):
cols <- c("50 miles"="#B10026","100 miles"="#FC4E2A","150 miles"="#FEB24C", "200 miles"="#FFEDA0")
hurricane_plot <- ggplot(data = US_subset_counties) +
geom_polygon(mapping = aes(x = long, y = lat, group = group), size = 0.2, color="gray", fill=NA) +
geom_polygon(data=counties_200, aes(x = long, y = lat, group = group), size = 0.2, fill="#FFEDA0") +
geom_polygon(data=counties_150, aes(x = long, y = lat, group = group), size = 0.2, fill="#FEB24C") +
geom_polygon(data=counties_100, aes(x = long, y = lat, group = group), size = 0.2, fill="#FC4E2A") +
geom_polygon(data=counties_50, aes(x = long, y = lat, group = group), size = 0.2, fill="#B10026") +
geom_polygon(data=US_subset_states, aes(x = long, y = lat, group = group), size = 0.2, color = "black", fill=NA) +
theme_bw() +
theme(panel.background = element_blank()) +
scale_colour_manual(name="Distance From Radius",values=cols)
hurricane_plot
Thanks in advance!
EDIT:
After #Ben's comment, I tried putting fill inside the aes to no avail. However, when I changed fill to col and used guides, this is the result I get:
How would I change the colors of the boxes and labels to the cols vector above, and is there a way to remove the border colors on my plot?
This is my updated code:
hurricane_plot <- ggplot(data = US_subset_counties) +
geom_polygon(mapping = aes(x = long, y = lat, group = group), size = 0.2, color="gray", fill=NA) +
geom_polygon(data=counties_200, aes(x = long, y = lat, group = group, color="#FFEDA0"), size = 0.2, fill="#FFEDA0") +
geom_polygon(data=counties_150, aes(x = long, y = lat, group = group, color="#FEB24C"), size = 0.2, fill="#FEB24C") +
geom_polygon(data=counties_100, aes(x = long, y = lat, group = group, color="#FC4E2A"), size = 0.2, fill="#FC4E2A") +
geom_polygon(data=counties_50, aes(x = long, y = lat, group = group, color="#B10026"), size = 0.2, fill="#B10026") +
geom_polygon(data=US_subset_states, aes(x = long, y = lat, group = group), size = 0.2, color = "black", fill=NA) +
theme_bw() + theme(panel.background = element_blank()) +
guides(color=guide_legend("Distance From Radius"))
#Alan -
Here's a reproducible example of what you might need after subsetting US state data. It uses cols as you have done, fill in aes, and scale_fill_manual. Let me know if this is what you have in mind, hope this helps.
library(maps)
library(ggplot2)
us_states <- map_data("state")
states1 <- subset(us_states, region %in% c("california", "oregon", "washington"))
states2 <- subset(us_states, region %in% c("illinois", "indiana", "iowa"))
states3 <- subset(us_states, region %in% c("new york", "massachusetts", "connecticut"))
cols <- c("West"="#B10026","Midwest"="#FC4E2A","Northeast"="#FEB24C")
state_plot <- ggplot(data = us_states) +
geom_polygon(data=us_states, aes(x = long, y = lat, group = group), size = 0.2) +
geom_polygon(data=states1, aes(x = long, y = lat, group = group, fill="West"), size = 0.2) +
geom_polygon(data=states2, aes(x = long, y = lat, group = group, fill="Midwest"), size = 0.2) +
geom_polygon(data=states3, aes(x = long, y = lat, group = group, fill="Northeast"), size = 0.2) +
theme_bw() +
theme(panel.background = element_blank()) +
scale_fill_manual(name="Area of Country", values=cols)
state_plot
Here's a related approach but simplified a bit:
library(maps)
library(ggplot2)
library(tidyverse)
us_states <- map_data("state")
cols <- c("West"="#B10026","Midwest"="#FC4E2A","Northeast"="#FEB24C")
us_states <- us_states %>%
mutate(region_group = case_when(
region %in% c("california", "oregon", "washington") ~ "West",
region %in% c("illinois", "indiana", "iowa") ~ "Midwest",
region %in% c("new york", "massachusetts", "connecticut") ~ "Northeast",
TRUE ~ NA_character_
))
state_plot <- ggplot(data = us_states, aes(x = long, y = lat, group = group), size = 0.2) +
geom_polygon(aes(fill = region_group)) +
theme_bw() +
theme(panel.background = element_blank()) +
scale_fill_manual(name="Area of Country", values = cols)
state_plot
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")
White fill in the legends of the bubble maps in R using ggplot?
I'm posting this quite stupid question after unsuccessfully googleing for a while. I have the following situation: dta1 <- data.frame(LON = runif(20)*100, LAT = runif(20)*100, VALUE = runif(20)*10) dta2 <- data.frame(LON = runif(20)*100, LAT = runif(20)*100, VALUE = runif(20)*10) ggplot() + geom_point(data=dta1, color = "blue", alpha = 0.5, aes(x=LON, y=LAT, size = VALUE)) + geom_point(data=dta2, color = "red", alpha = 0.5, aes(x=LON, y=LAT, size = VALUE)) Which yields something like this (points' positions and sizes can change, it does not matter): The result is quite good, but I want the circles in the legend to be draw with black borders and filled with white. Any idea?
Would this be an acceptable result? I supposed your point was to make the size legend of an anonymous colour, unrelated to the blue or the red of the actual points. set.seed(1) dta1 <- data.frame(LON = runif(20)*100, LAT = runif(20)*100, VALUE = runif(20)*10) dta2 <- data.frame(LON = runif(20)*100, LAT = runif(20)*100, VALUE = runif(20)*10) library(ggplot2) ggplot() + geom_point(data=dta1, alpha = 0.5, aes(x=LON, y=LAT, size = VALUE, colour = "value1")) + geom_point(data=dta2, alpha = 0.5, aes(x=LON, y=LAT, size = VALUE, colour = "value2")) + scale_color_manual(values = c(value1 = "blue", value2 = "red")) Alternately, closely related to the "fill" answer: library(ggplot2) ggplot() + geom_point(data = dta1, shape = 21, alpha = 0.5, aes(x = LON, y = LAT, size = VALUE, fill = "value1")) + geom_point(data = dta2, shape = 21, alpha = 0.5, aes(x = LON, y = LAT, size = VALUE, fill = "value2")) + scale_fill_manual(values = c(value1 = "blue", value2 = "red")) + theme_light() + guides(fill = FALSE)
Hmm. I dont know of a way of changing something in the legend only. In a way thats contrary to the point of the legend. This is the best i got. library(ggplot2) dta1 <- data.frame(LON = runif(20)*100, LAT = runif(20)*100, VALUE = runif(20)*10) dta2 <- data.frame(LON = runif(20)*100, LAT = runif(20)*100, VALUE = runif(20)*10) ggplot() + geom_point(data=dta1, color = "blue", alpha = 0.5,shape = 21, aes(x=LON, y=LAT, size = VALUE)) + geom_point(data=dta2, color = "red", alpha = 0.5,shape = 21, aes(x=LON, y=LAT, size = VALUE)) + theme_classic() Created on 2020-12-09 by the reprex package (v0.3.0)
Alternatively, it could be something like this: dta1$C <- 1 dta2$C <- 2 dta1 <- rbind(dta1, dta2) ggplot(dta1, aes(x = LON, y = LAT, size = VALUE, fill = C)) + geom_point(shape=21) Which yields: But without the inferior (rectangle titled "C") part of the legend.
How to put geocoordinates from a dataframe on a map in R?
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")
Created labels for geom_point
library(ggplot2) usa <- map_data("state") myData <- data.frame( states = c("AL","AZ","AR","CA","CO","CT","DE","FL","GA","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","WV","WI","WY"), lat = c(32,33,34,36,39,41,39,38,27,33,44,40,39,42,38,37,31,44,39,42,43,45,32,38,46,41,38,43,40,34,42,35,47,40,35,44,40,41,33,44,35,31,40,44,37,47,38,44), long = c(-86,-111,-92,-119,-105,-72,-75,-77,-81,-83,-114,-88,-86,-93,-96,-84,-91,-69,-76,-71,-84,-93,-89,-92,-110,-98,-117,-71,-74,-106,-74,-79,-99,-82,-96,-122,-77,-71,-80,-99,-86,-97,-111,-72,-78,-121,-89,-89), pop = c(4.8,7,3,39.5,5.6,3.5,.961,20.9,10.4,1.7,12.8,6.7,3.1,2.9,4.4,4.6,1.3,6,6.8,9.9,5.5,2.9,6.1,1,1.9,2.9,1.3,9,2,19.8,10.2,.755,11.6,3.9,4.1,12.8,1,5,.869,6,28.3,3.1,.623,8.4,7.4,1.8,1,2)) ggplot() + geom_path(data = usa, aes(x = long, y = lat, group = group)) + geom_point(data = myData, aes(x = long, y = lat, size = pop), color = "blue") How would I go about adding labels to the map being created?
I assume that by "How would I go about adding labels to the map being created?" you mean how to add state labels. There seems to be something wrong with the data in myData. The names of the states and their lat/long don't seem to match. That aside, in general you can add labels in the following way: ggplot() + geom_path(data = usa, aes(x = long, y = lat, group = group)) + geom_point(data = myData, aes(x = long, y = lat, size = pop), color = "blue") + geom_text(data = myData, aes(long, lat, label = states))
How to deal with a global fixed scale_fill_gradient2 for multiple plot in loop
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.