0

I get a problem for setting the position of legend and wonder if anyone can help. I follow this example: http://www.thisisthegreenroom.com/2009/choropleths-in-r/ My code is:

 require(maps)
    require(ggmap)
    library(openxlsx)
    rm(list = ls())
    map("state", "Arizona")
    setwd('M:/SCC/Q-Board')
    PM25 <- read.xlsx("PM2.5_Emission_AZ_60 EIS emission sectors.xlsx", sheet = 'Emission_County', colNames = TRUE)
    colors = c("#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", 
               "#980043")
    PM25$colorBuckets <- as.numeric(cut(PM25$PM25, c(0, 5, 10, 20, 30,40, 50)))
    map("county",'Arizona', col = colors[PM25$colorBuckets], fill = TRUE,boundary = TRUE, resolution = 0, 
        lty = 1, projection = "polyconic")
    title("PM2.5 Emission by county, 2011")
    leg.txt <- c("<5", "5-10", "10-20", "20-30", "30-40", ">40")
    legend("bottom", leg.txt, horiz = F, fill = colors,bty="n",title = 'Unit:1000 tons')

Then, the output figure was shown in below. I try to change the position by setting "top", "left".... But the legend are still overlap with the figure. Thank you for your help !

MAP

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
lyggd
  • 3
  • 1
  • see this post, it will help you: http://stackoverflow.com/questions/8929663/r-legend-placement-in-a-plot – Jonathan Rhein May 18 '16 at 18:41
  • Hi Joni, I saw this before but I still can not solve my problem. Maybe, I just a new user for R. Would you help me modify the code ? Thanks a lot ! – lyggd May 18 '16 at 18:46
  • can you provide the excel sheet which you are reading in? otherwise it is difficult to help... – Jonathan Rhein May 18 '16 at 18:47

1 Answers1

0

It seems to me that you simply run of out the plotting region. This is very common for spatial plot, which will often occupy a significant amount of your plotting domain. I would split the domain into two: one for spatial plot, the other for legend. The following code does this:

## a function to set up plotting region
## l: ratio of left region
## r: ratio of right region
split.region <- function(l, r) {
  layout(matrix(c(rep(1, l), rep(2, r)), nrow = 1))
  mai <- par("mai")
  mai[2] <- 0.1
  mai[4] <- 0
  par(mai = mai)
  }

# use 80% region for main image
# use 20% region for legend
split.region(4, 1)
## produce your main plot
image(x = 0:10/10, y = 0:10/10, matrix(rbinom(100, 1, 0.3), 10), bty= "n", xaxt = "n", yaxt = "n", ann = FALSE, main = "sample plot")
## set up 2nd plot, with nothing
plot(1:2, bty="n", ann=FALSE, xaxt = "n", yaxt = "n", col = "white")
## add your legend to your second plot
leg.txt <- c("<5", "5-10", "10-20", "20-30", "30-40", ">40")
## place legend at bottom left
legend("bottomleft", leg.txt, horiz = F, pch = 15, col = 1:6, bty="n", title = 'Unit:1000 tons', cex = 1.5)

sample

Adjust l, r until you are satisfied.

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
  • Do you have wechat that I can add you ? I am new to R and would like to learn more about this powerful software! Thanks – lyggd May 18 '16 at 20:44