2
votes

I've got some issue creating a map with ggplot2 above which I project points using geom_point. When exporting in pdf or in an other support, the point size varies (because she's absolute and not axis-relative). I've searched how to change that and found a lot of answers saying, that it was on purpose, because if it wasn't the case it would be changing to ellipse each time the axis proprtions change. I understand that, however, because I work on a map, I use coord_fixed to fix the output and avoid distorsions of my map, so if I was able to fix the point size relatively to the plot size, it wouldn't be a problem.

Is there some solution to do that? I've read some interesting things suggesting using geom_polygon to artificially create ellipses. But I have two problems with this method:

  • First I don't know how to implement that with my data, now I know the place where the centers of my points are, but how could I then later say how to define all the centers and then defin a filled circled polygon around?

  • Second I have used scale_size_continuous to plot smaller or bigger points relatively to other variable. How could I implement that with geom_polygon?

Facit: I would be happy either with the possibility of override the impossibility to determine a relative unit for the point size, or with some help to make me understand how I can create the same thing with the function geom_polygon.

I tried to join a small reproducible example here. It is only an example, the problem with my data is that I have a lot of closed small values (mainly 1, like the small dot in the reproducible example), and so they seem really good, but when exporting it can become very bigger and create a lot of problems by overplotting, which is the reason why I need to fix this ratio.

Link for the map informations and second link for map informations

dat <- data.frame(postcode=c(3012, 2000, 1669, 4054, 6558), n=c(1, 20, 40, 60, 80))

ch <- read.csv("location/PLZO_CSV_LV03/PLZO_CSV_LV03.csv", sep=";")#first link, to attribute a geographical location for each postcode
ch <- ch%>%
  distinct(PLZ, .keep_all=TRUE)%>%
  group_by(PLZ, N, E)%>%
  summarise
ch <- ch%>%
  filter(PLZ %in% dat$postcode)
ch <- ch%>%
  arrange(desc(as.numeric(PLZ)))
dat <- dat%>%
  arrange(desc(as.numeric(postcode)))
datmap <- bind_cols(dat, ch)

ch2 <- readOGR("location/PLZO_SHP_LV03/PLZO_PLZ.shp")#second link, to make the shape of the country
ch2 <- fortify(ch2)

a <- ggplot()+
geom_polygon(dat=ch2, aes(x=long, y=lat, group=group), colour="grey75", fill="grey75")+
geom_jitter(data=datmap, aes(x=E, y=N, group=FALSE, size=n), color=c("red"))+ #here I put geom_jitter, but geom_point is fine too
scale_size_continuous(range=c(0.7, 5))+
coord_fixed()
print(a)

Thanks in advance for the help!

1

1 Answers

4
votes

You can use ggsave() to save the last plot and adjust the scaling factor used for points/lines etc. Try this:

ggplot(data = ch2) + 
  geom_polygon(aes(x=long, y=lat, group=group),
               colour="grey85", fill="grey90") +
  geom_point(data=datmap, aes(x=E, y=N, group=FALSE, size=n),
             color=c("red"), alpha = 0.5) +
  scale_size_continuous(range=c(0.7, 5)) +
  coord_fixed() +
  theme_void()

ggsave(filename = 'plot.pdf', scale = 2, width = 3, height = 3)

Play around with the scale parameter (and optionally the width and height) until you are happy with the result.

DO NOT use geom_jitter(): this will add random XY variation to your points. To deal with overplotting you can try adding transparency - I added an alpha parameter for this. I also used theme_void() to get rid of axes and background.

Your shape file with map information is quite heavy: you can try a simple one with Swiss cantons, like this one.enter image description here