简体   繁体   English

放置在 ggmap 坐标上的小 ggplot2 图

[英]Small ggplot2 plots placed on coordinates on a ggmap

I would like to first use ggmap to plot a specific area with longitude and latitude as axes.我想首先使用 ggmap 以经度和纬度为轴绘制特定区域。

Then I would like to put small ggplot2 plots on the specific locations, given their longitude and latitude.然后我想在特定位置放置小的 ggplot2 图,给定它们的经度和纬度。 These can be barplots with minimal theme.这些可以是具有最小主题的条形图。

My database may have the columns:我的数据库可能有以下列:

1. town
2. longitude
3. latitude
4. through 6. value A, B, C

I generate a plot (pseudocode)我生成一个情节(伪代码)

p <- ggmap(coordinates) 

and I have my minimal ggplot2 design我有我最小的 ggplot2 设计

q<-ggplot2()+geom_bar(....)+ ... x-axis null y axis null minimal template

How to combine the two designs to have a ggmap with small minimal ggplot plots imposed on specific coordinates of the map?如何将这两种设计结合起来以获得一个 ggmap,并在地图的特定坐标上施加小的最小 ggplot 图?

Here's one I did using pie charts as points on a scatterplot.这是我使用饼图作为散点图上的点所做的一个。 You can use the same concept to put barcharts on a map at specific lat/long coordinates.您可以使用相同的概念将条形图放在地图上特定纬度/经度坐标处。

R::ggplot2::geom_points: how to swap points with pie charts? R::ggplot2::geom_points:如何用饼图交换点?

Needs further update.需要进一步更新。 Some of the code used was abbreviated from another answer, which has since been deleted.使用的一些代码是从另一个答案缩写而来的,此后已被删除。 If you find this answer via a search engine, drop a comment and I'll get around to fleshing it back out.如果您通过搜索引擎找到这个答案,请发表评论,我会尽快充实它。


Updated:更新:

在此处输入图片说明

Using mostly your adapted code from your answer, but I had to update a few lines.主要使用您的答案中的改编代码,但我不得不更新几行。

p <- ggmap(Poland) + coord_quickmap(xlim = c(13, 25), ylim = c(48.8, 55.5), expand = F)

This change makes a better projection and eliminates the warnings about duplicated scales.此更改使投影更好,并消除了有关重复比例的警告。

df.grobs <- df %>% 
  do(subplots = ggplot(., aes(1, value, fill = component)) + 
       geom_col(position = position_dodge(width = 1), 
                alpha = 0.75, colour = "white") +
       geom_text(aes(label = round(value, 1), group = component), 
                 position = position_dodge(width = 1),
                 size = 3) +
       theme_void()+ guides(fill = F)) %>% 
  mutate(subgrobs = list(annotation_custom(ggplotGrob(subplots), 
                                           x = lon-0.5, y = lat-0.5, 
                                           xmax = lon+0.5, ymax = lat+0.5))) 

Here I explicitly specified the dodge width for your geom_col so I could match it with geom_text .在这里,我明确指定了geom_col的闪避宽度,以便我可以将它与geom_text匹配。 I used round(value, 1) for the label aesthetic, and it automatically inherits the x and y aesthetics from the subplots = ggplot(...) call.我使用round(value, 1)作为标签美学,它会自动从subplots = ggplot(...)调用继承xy美学。 I also manually set the size to be quite small, so the labels would fit, but then I increased the overall bounding box for each subgrob, from 0.35 to 0.5 in each direction.我还手动将尺寸设置得非常小,以便标签适合,但随后我将每个子格的整体边界框从每个方向的 0.35 增加到 0.5。

df.grobs %>%
  {p + 
    .$subgrobs + 
    geom_text(data=df, aes(label = name), vjust = 3.5, nudge_x = 0.065, size=2) + 
    geom_col(data = df,
             aes(Inf, Inf, fill = component), 
             colour = "white")}

The only change I made here was for the aesthetics of the "ghost" geom_col .我在这里所做的唯一更改是为了“幽灵” geom_col When they were set to 0,0 they weren't plotted at all since that wasn't within the x and y limits.当它们设置为 0,0 时,它们根本没有被绘制,因为它不在 x 和 y 范围内。 By using Inf,Inf they're plotted at the far upper right corner, which is enough to make them invisible, but still plotted for the legend.通过使用 Inf,Inf 将它们绘制在最右上角,这足以使它们不可见,但仍为图例绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM