Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

dictionary - ggplot map in R - title inside border and more space within border

I have some code written to make a map with USA states and territories. For some territories I need to draw a border outside the title, giving more space to the map. See below a chunk extracted for Hawaii, the grey border is the one that I have right now but I need something similar to the red border that I edited in paint (yes, very bad using paint I know). Any suggestions on how to solve this? I tried to move the border using panel.border without success. The most important task is to add the border giving more space to the plot.

map produced in ggplot and edited in paint

library("choroplethrMaps")

# **************************
# Shapefiles for Hawai
# **************************
data(state.map)

# Subset for Hawaii
map.HI.dat <- subset(map.dat, State=="HI" )
dim(map.HI.dat) #[1] 3  95
map.HI.freq <- data.frame(table(map.HI.dat$state.name))
colnames(map.HI.freq) <- c("region", "n")
class(map.HI.freq$region) #[1] "factor"
map.HI.freq$region <- as.character(map.HI.freq$region)
map.HI.freq$region[map.HI.freq$region=="hawai"] <- "hawaii"
#View(map.HI.freq)
hawaii.st <- subset(state.map, region=="hawaii")

map.HI.freq <- data.frame(region="hawaii",
                          n=4)

hawaii.map.dat <- plyr::join(hawaii.st, map.HI.freq, by="region")
hawaii.map.dat$brks <- cut(hawaii.map.dat$n,
                           breaks = c(0, 0.99, 5,10,50,max.breaks),
                           labels = c("0", "1 - 5", "6 - 50", "51 - 150", paste("151 - ",max.breaks,sep="")),
                           include.lowest = TRUE)

s2 <-ggplot() + 
  geom_polygon(data=hawaii.map.dat, aes(x=long, y=lat, group = group, fill=brks), colour="black", size=1, show.legend = FALSE) +
  scale_fill_manual(name="",
                    values = colors.for.maps, 
                    breaks = breaks.for.maps, 
                    drop = FALSE) +
  theme(panel.grid       = element_blank(),
        panel.border     = element_rect(colour = "grey50", size = 2, fill=NA),
        panel.background = element_blank(),
        plot.title       = element_text(colour="black", size = 14, face = "bold", hjust=0.5)) + 
  labs(fill  = "Frequencies",
       title = "Hawaii", x="", y="") + 
  scale_y_continuous(breaks=c()) + 
  scale_x_continuous(breaks=c()) + 
  coord_map() 
s2


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can set the plot.background to be a rectangle and then remove your panel.border:

library("choroplethrMaps")

# **************************
# Shapefiles for Hawai
# **************************
data(state.map)

# Subset for Hawaii
hawaii.st <- subset(state.map, region=="hawaii")

ggplot(hawaii.st) +
    geom_polygon(aes(long, lat, group = group)) +
    theme(panel.grid       = element_blank(),
          panel.background = element_blank(),
          plot.background = element_rect(fill = "white", color = "grey20",size = 2),
          plot.title       = element_text(colour="black", size = 14, face = "bold", hjust=0.5)) + 
    labs(title = "Hawaii", x="", y="") + 
    scale_y_continuous(breaks=c()) + 
    scale_x_continuous(breaks=c()) + 
    coord_map()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...