ggplot geom_point随窗口大小而变化 [英] ggplot geom_point varying size with windows size

查看:41
本文介绍了ggplot geom_point随窗口大小而变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 ggplot2 创建地图时遇到了一些问题,在上面使用 geom_point 投影点.以pdf或其他支持格式导出时,点大小会有所不同(因为她是绝对的,而不是相对于轴的).我已经研究了如何更改它,并找到了很多答案,说这是有目的的,因为如果不是这种情况,则每当轴的属性发生变化时,它就会变为椭圆形.我知道,但是,因为我在地图上工作,所以我使用 coord_fixed 来修复输出并避免地图失真,因此,如果我能够相对于地块大小来固定点大小,没问题.

有解决方案吗?我读过一些有趣的东西,建议使用 geom_polygon 来人为地创建椭圆.但是这种方法有两个问题:

  • 首先,我不知道如何使用数据来实现该目标,现在我知道了我的点的中心所在的位置,但是后来我又如何说出如何定义所有中心,然后定义一个填充圆形的多边形?

  • 第二,我使用了 scale_size_continuous 相对于其他变量绘制较小或较大的点.如何使用 geom_polygon 来实现呢?

简单:我很乐意覆盖无法确定点大小的相对单位的可能性,或者希望有一些帮助使我理解如何使用功能 geom_polygon .

我尝试在此处加入一个可重现的小示例.这只是一个例子,我的数据存在的问题是我有很多封闭的小值(主要是1,就像可重现示例中的小点一样),因此它们看起来确实不错,但是在导出时它会变得更大并通过过度绘图产生很多问题,这就是我需要固定此比例的原因.

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!

解决方案

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

这篇关于ggplot geom_point随窗口大小而变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆