3

I'm trying to plot some cities stored in my rda file naobrazokSR.rda on a map from GADM while having a background of the map in color #2C3E4F. Without using the coord_map() this works as intended but looks deformed. After using the coord_map() white bars appear on top and bottom. I'm trying to use the plot inside shiny application and I believe this can be solved by fixing the aspect ratio somehow, but I'm not sure how. Is there a way to change the whitebars to my color without adjusting the aspect ratio?

library(ggmap)
library(ggplot2)
SVK <- readRDS("D:\\Downloads\\SVK_adm1.rds") 
alfa = 0.5
load("D:\\Downloads\\naobrazokSR.rda")
p <- ggplot() + geom_polygon(data=SVK, aes(long,lat,group=group))
p <- p + geom_path(data=SVK, aes(long,lat, group=group), color="white",size=0.1)
p <- p + geom_point(aes(x=pomoc$X1, y=pomoc$X2) , size=2,col = "red", alpha=alfa) 
p <- p + theme(axis.line=element_blank(),axis.text.x=element_blank(),
           axis.text.y=element_blank(),axis.ticks=element_blank(),
           axis.title.x=element_blank(),
           axis.title.y=element_blank(),legend.position="none",
           panel.background=element_rect(fill = "#2C3E4F", colour = "#2C3E4F"),
           panel.border=element_blank(),panel.grid.major=element_blank(),
           panel.grid.minor=element_blank(),
           plot.background= element_rect(fill = "#2C3E4F", colour = "#2C3E4F")) 
p <- p + coord_map()
p

enter image description here

1 Answer 1

1

cowplot to the rescue!

I used a different vector file for this example. White borders on left and right sides:

enter image description here

Use cowplot and set again panel.background:

library(cowplot)
ggdraw(p) + theme(panel.background = element_rect(fill = "#2C3E4F", colour = "#2C3E4F"))

enter image description here


Work-around with ggplot2 and image export:

pdf(filename = '~/test.pdf',width = 3000, height = 2000,bg = "#2C3E4F") # could be jpeg and png also
p <- ggplot() + geom_polygon(data=SVK, aes(long,lat,group=group))
p <- p + geom_path(data=SVK, aes(long,lat, group=group), color="white",size=0.1)
p <- p + geom_point(aes(x=pomoc$X1, y=pomoc$X2) , size=2,col = "red", alpha=alfa) 
p <- p + theme(axis.line=element_blank(),axis.text.x=element_blank(),
           axis.text.y=element_blank(),axis.ticks=element_blank(),
           axis.title.x=element_blank(),
           axis.title.y=element_blank(),legend.position="none",
           panel.background=element_blank(),
           panel.border=element_blank(),panel.grid.major=element_blank(),
           panel.grid.minor=element_blank(),
           plot.background= element_blank()) 
p <- p + coord_map()
p
dev.off()
2
  • Thank you this worked perfectly, but to me the solution seems rather strange. It looks like it only redraws the same plot on a colored rectangle. Why does this not work in basic ggplot? Can it be solved by only using ggplot2 library or is there a bug in the order of operations thats solved by cowplot? Or is this another question i should rather post on StackOverflow?
    – ExabytE
    Jan 25, 2018 at 19:28
  • cowplot use grob method, it transforms ggplot object to grob and uses a grid to place the plot. Using ggplot2, you have to change xlim and ylim to fit your device size (in terms of width/height) or to use the approach that I include in my answer, so check the edit
    – aldo_tapia
    Jan 25, 2018 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.