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 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
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))
This question already has answers here:
Show the value of a geom_point using geom_text
(2 answers)
Closed 5 years ago.
I have created a world map with the use of the ggplot2 library.
I am attempting to label the two cities (Shanghai and Sao Paulo) respectively through using labels in ggplot2. However, when I try to add labels I get the error message:
Warning: Ignoring unknown aesthetics: labels
Error: geom_text requires the following missing aesthetics: x, y, label
Here is the full code:
require(maps)
require(mapdata)
library(ggplot2)
countries = c("Sao Paulo","Shanghai")
global <- map_data("world")
ggplot() + geom_polygon(data = global, aes(x=long, y = lat, group =
group)) +
coord_fixed(1.3)
ggplot() +
geom_polygon(data = global, aes(x=long, y = lat, group = group),
fill = NA, color = "red") +
coord_fixed(1.3)
gg1 <- ggplot() +
geom_polygon(data = global, aes(x=long, y = lat, group = group),
fill = "green", color = "blue") +
coord_fixed(1.3)
gg1
labs <- data.frame(
long = c(-46.625290,121.4580600),
lat = c(-23.533773,31.2222200),
stringsAsFactors = FALSE
)
gg1 +
geom_point(data = labs, aes(x = long, y = lat), color = "red", size
= 5) + ggtitle("World Map") + geom_text(aes(labels=countries),vjust=0,
colour="red")
Clearly, I'm using ggplot wrong in some way but can't figure out how.
labs$countries <- countries
gg1 +
geom_point(data=labs, aes(long, lat), colour="red", size=5) +
ggtitle("World Map") +
geom_text(data=labs, aes(long, lat, label=countries))
I would like to draw a map of French departements (ie districts in France) and plot points on the map. I use ggplot2 to draw the map but when I want to add points on the map, R returns an error which says that it cannot find 'group'.
library("ggplot2")
library("ggmap")
# load the contour of French departements
wg <- read.csv("data/departements.csv")
metropoles <- c("Paris", "Rennes", "Rouen", "Lille", "Marseille")
geo <- geocode(metropoles)
met <- data.frame(ville = metropoles, geo)
map <- ggplot(data = wg, aes(x = long, y = lat, group = group)) +
geom_polygon() +
scale_x_continuous(limits = c(-7,10)) +
scale_y_continuous(limits = c(40,53)) +
coord_map() +
theme(axis.text = element_blank(), axis.title = element_blank())
map + geom_point(data = met, aes(x = lon, y = lat))
For replication, you can find the raw data here and the R program here
#Roland had the good answer. The problem is that I had defined group as a general argument.
ggplot() +
geom_polygon(data = wg, aes(x = long, y = lat, group = group)) +
scale_x_continuous(limits = c(-7,10)) +
scale_y_continuous(limits = c(40,53)) +
coord_map() +
theme(axis.text = element_blank(), axis.title = element_blank())
geom_point(data = met, aes(x = lon, y = lat))