How do I adjust the lengenda to Spatial Lines in ggplot?

Asked

Viewed 67 times

3

I’m filling lines on polygons, but when I try to insert the caption the lines get bad, how do I adjust it?

Without the Legend

library("ggplot2")
ggplot(mapa_mg) + 
  aes(x=long, y=lat, group=group) +
  geom_polygon(fill = "white") +
  geom_path(color="grey50") +
  labs(y="latitude", x="longitude") +
  geom_polygon(data=mapa_rod, 
               mapping=aes(x=long, y=lat, group=group), 
               color = cores[mapa_rod$idcores],
               fill="transparent",
               size = 1) +
  coord_equal()

map1

With the legend

library("ggplot2")
ggplot(mapa_mg) + 
  aes(x=long, y=lat, group=group) +
  geom_polygon(fill = "white") +
  geom_path(color="grey50") +
  labs(y="latitude", x="longitude") +
  geom_polygon(data=mapa_rod, 
               mapping=aes(x=long, y=lat, group=group), 
               color = cores[mapa_rod$idcores],
               fill="transparent",
               size = 1) +
  coord_equal()

map2

  • Solved: Just swap geom_polygon for geom_path and Fill for color reference -> link

  • 1

    Add the answer as a response for future visitors to orient themselves

1 answer

1


Solution:

legenda1 = c("BR-040","BR-116","BR-262","BR-381")
cores = c('#d01c8b', 'yellow', '#e66101', 'blue')

library("ggplot2")
library('ggsn')
library("ggrepel")

ggplot(mapa_mg) +       
  aes(x=long, y=lat, group=group) +
  geom_polygon(fill = "white") +
  geom_path(color="grey50") +
  labs(y="latitude", x="longitude") +

  geom_path(data=mapa_rod, 
               mapping=aes(x=long, y=lat, group=group, color=factor(idcores)), size = 1) +
  scale_colour_manual(values = cores, labels = legenda1) +
  labs(color = 'Rodovia') +

  theme(legend.title = element_text(face = 'bold', size = 10)) +
  theme(legend.text = element_text(face = NULL, size = 9)) +
  theme(legend.background = element_rect(fill="white", size=.5, linetype="dotted")) +
  theme(legend.position=c(0.11, 0.15)) +

  ggsn::scalebar(mapa_mg, dist = 100, st.size=3, height=0.01, dd2km = TRUE, model = 'WGS84') +
  ggsn::north(mapa_mg, symbol = 16, scale = 0.15) +

  coord_equal()

map

Browser other questions tagged

You are not signed in. Login or sign up in order to post.