小ggplot2图放置在ggmap的坐标上 [英] Small ggplot2 plots placed on coordinates on a ggmap

查看:110
本文介绍了小ggplot2图放置在ggmap的坐标上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想首先使用ggmap绘制一个以经度和纬度为轴的特定区域。

然后,我想在特定位置放置小ggplot2图,考虑到它们的经度和纬度。这些可以是最简单的主题。



我的数据库可能包含以下列:

  1。镇
2.经度
3.纬度
4.通过6.价值A,B,C

我生成一个图(伪代码)

  p < -  ggmap(coordinates)

我有我最小的ggplot2设计

  q <-ggplot2()+ geom_bar(....)+ ... x轴空y轴null最小模板

如何将这两种设计结合起来,以便在地图的特定坐标上加上一个ggplot小图形?

解决方案

下面是我使用饼图作为散点图上的点。您可以使用相同的概念将barcharts放置在特定纬度/经度坐标的地图上。



主要使用您的改编码从你的答案,但我不得不更新几行。

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



  df.grobs<  -  df%>%$ b更好地预测并消除了重复刻度的警告。 $ b do(subplots = ggplot(。,aes(1,value,fill = component))+ 
geom_col(position = position_dodge(width = 1),
alpha = 0.75,color =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 (小区),
x = lon-0.5,y = lat-0.5,
xmax = lon + 0.5,ymax = lat + 0.5))
pre>

在这里,我明确指定了您的 geom_col 的闪避宽度,以便我可以将它与 geom_text 。我使用 round(value,1)作为标签美学,它自动继承 x y 美学来自 subplots = ggplot(...)调用。我也手动将尺寸设置为很小,因此标签会适合,但是随后我将每个子方块的整体边界框从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),
color =white)}

我在这里做的唯一改变是为了幽灵美学 geom_col 。当它们被设置为0,0时,它们根本不被绘制,因为它不在x和y范围内。通过使用Inf,Inf,他们被绘制在最右上角,这足以让他们看不见,但仍然为图例绘制。


I would like to first use ggmap to plot a specific area with longitude and latitude as axes.

Then I would like to put small ggplot2 plots on the specific locations, given their longitude and latitude. 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

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?

解决方案

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?

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. I used round(value, 1) for the label aesthetic, and it automatically inherits the x and y aesthetics from the subplots = ggplot(...) call. 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.

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. When they were set to 0,0 they weren't plotted at all since that wasn't within the x and y limits. 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.

这篇关于小ggplot2图放置在ggmap的坐标上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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