Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing latitude and longitude labels in ggplot

Tags:

r

ggplot2

I'm creating a map of Minnesota

map.test <- ggplot(kt_geom)+
  geom_polygon(aes(long, lat, group=group), fill="white")+
  geom_path(color="gray48", mapping=aes(long, lat, group=group), size=0.2)

map.test

and it displays the following map.

enter image description here

How can I remove the light gray background as well as the longitude and latitude axes? I know these won't appear if I use choropleth, but I need to use some functions within ggplot.

like image 562
wwl Avatar asked Dec 05 '16 17:12

wwl


Video Answer


2 Answers

theme(rect = element_blank()) will remove the gray background and (the appearance of) grid lines.

Adding axis.ticks = element_blank() will remove the tick marks, and axis.text.x = element_blank(), axis.text.y = element_blank() will remove the lat/lon labels.

ggplot(kt_geom) +
  geom_polygon(aes(long, lat, group=group), fill="white") +
  geom_path(color="gray48", mapping=aes(long, lat, group=group), size=0.2) + 
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        rect = element_blank())

In cases with a non-white background, you'll want to set panel.grid.major = element_line(color = background_color), where, of course, background_color is the color of panel.background.

ggplot(kt_geom) +
  geom_polygon(aes(long, lat, group=group), fill="white") +
  geom_path(color="gray48", mapping=aes(long, lat, group=group), size=0.2) + 
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        panel.background = element_rect(fill = "red"),
        panel.grid.major = element_line(color = "red"))
like image 53
seasmith Avatar answered Oct 16 '22 14:10

seasmith


As hrbrmstr points out, theme_map() works:

library(ggthemes)
map.test <- ggplot(kt_geom)+
  geom_polygon(aes(long, lat, group=group), fill="white")+
  geom_path(color="gray48", mapping=aes(long, lat, group=group), size=0.2)+
  theme_map()

map.test

enter image description here

like image 44
wwl Avatar answered Oct 16 '22 14:10

wwl