Juan Carlos
Juan Carlos

Reputation: 227

Overlap map created in ggplot and shapefile

I am trying to overlap a shp layer on a map created with ggplot, when I plot the two graphs are independent, what do you suggest me to do to overlap both layers?

data.shape<-readOGR(dsn="departamentos",layer="DEPARTAMENTOS")


ggplot()+
  geom_tile(data = tx_trend, aes(x = longitude, y = latitude, fill = slope))+
  scale_fill_gradientn(colors = rev(pals::linearlhot(100)), name = "ºC/10y", limits = c(0.1,0.5)) +
  #scale_fill_gradientn(colors = (pals::isol(100)), name = "ºC/10y", limits = c(0.1,0.45)) +
#  geom_point(data = filter(tx_trend, sign < 0.01),aes(x = longitude, y = latitude, color = "Sign. trend \n p-value <0.01"),
  geom_point(data = filter(tx_trend, sign < 0.01),aes(x = longitude, y = latitude, color = "Sign. trend \n p-value <0.01"),
                        size = 0.7, show.legend = T) +
  scale_color_manual(values = c("black"), name = "")+
  coord_fixed(1.3)+
  xlab("Longitude") + ylab("Latitude")+
  labs(title = "Decadal trend Summer", 
       subtitle = "(1981-2016)",
       caption = "")+
  theme_bw() +
  guides(fill = guide_colourbar(barwidth = 9, barheight = 0.5, title.position="right"))+
  theme(legend.position = "bottom")

enter image description here enter image description here

Upvotes: 0

Views: 1435

Answers (1)

lovalery
lovalery

Reputation: 4652

Difficult without a reproducible example but I give it a try! So here are my suggestions:

  1. Install the 'sf' package with install.packages("sf") and load library with library(sf)

  2. Import your layer in sf format with st_read():

data.shape <- st_read(dsn="departamentos", layer="DEPARTAMENTOS")

  1. Use the dedicated geom_sf() to plot your shape on your tile and point layers. You just need to add the following line of code in the ggplot() chunk of code:

geom_sf(data = st_geometry(data.shape), fill = NA, color = "red") +

coord_sf(default_crs = sf::st_crs(4326)) +

So I suggest:

ggplot()+
  geom_tile(data = tx_trend, aes(x = longitude, y = latitude, fill = slope))+
  scale_fill_gradientn(colors = rev(pals::linearlhot(100)), name = "ºC/10y", limits = c(0.1,0.5)) +
  #scale_fill_gradientn(colors = (pals::isol(100)), name = "ºC/10y", limits = c(0.1,0.45)) +
#  geom_point(data = filter(tx_trend, sign < 0.01),aes(x = longitude, y = latitude, color = "Sign. trend \n p-value <0.01"),
  geom_point(data = filter(tx_trend, sign < 0.01),aes(x = longitude, y = latitude, color = "Sign. trend \n p-value <0.01"),
                        size = 0.7, show.legend = T) +
  geom_sf(data = st_geometry(data.shape), fill = NA, color = "red") + # ADDED HERE
  coord_sf(default_crs = sf::st_crs(4326)) +       # ADDED HERE
  scale_color_manual(values = c("black"), name = "")+
  coord_fixed(1.3)+
  xlab("Longitude") + ylab("Latitude")+
  labs(title = "Decadal trend Summer", 
       subtitle = "(1981-2016)",
       caption = "")+
  theme_bw() +
  guides(fill = guide_colourbar(barwidth = 9, barheight = 0.5, title.position="right"))+
  theme(legend.position = "bottom")

Upvotes: 1

Related Questions