放置在 ggmap 坐标上的小 ggplot2 图
Small ggplot2 plots placed on coordinates on a ggmap
我想先用ggmap绘制一个以经纬度为轴的特定区域
然后我想在特定位置放置小的 ggplot2 图,给定它们的经度和纬度。这些可以是主题最少的条形图。
我的数据库可能包含以下列:
1. town
2. longitude
3. latitude
4. through 6. value A, B, C
我生成一个图(伪代码)
p <- ggmap(coordinates)
我有我的最小 ggplot2 设计
q<-ggplot2()+geom_bar(....)+ ... x-axis null y axis null minimal template
如何将这两种设计结合起来,使 ggmap 具有在地图的特定坐标上施加的最小 ggplot 图?
这是我使用饼图作为散点图上的点所做的一个。您可以使用相同的概念将条形图放在地图上特定 lat/long 坐标处。
需要进一步更新。使用的一些代码是从另一个答案中缩写出来的,该答案已被删除。如果您通过搜索引擎找到此答案,请留下评论,我会抽空充实它。
已更新:
主要使用您的答案中的改编代码,但我不得不更新几行。
p <- ggmap(Poland) + coord_quickmap(xlim = c(13, 25), ylim = c(48.8, 55.5), expand = F)
此更改可实现更好的投影并消除有关重复比例的警告。
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)))
这里我明确指定了你的 geom_col
的闪避宽度,所以我可以将它与 geom_text
匹配。我使用 round(value, 1)
作为标签美学,它会自动从 subplots = ggplot(...)
调用中继承 x
和 y
美学。我还手动将尺寸设置得非常小,以便标签适合,但随后我增加了每个子群的整体边界框,每个方向从 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")}
我在这里做的唯一改变是 "ghost" geom_col
的美学。当它们设置为 0,0 时,它们根本不会被绘制,因为那不在 x 和 y 的限制范围内。通过使用 Inf,Inf,它们被绘制在最右上角,这足以使它们不可见,但仍然为图例绘制。
我想先用ggmap绘制一个以经纬度为轴的特定区域
然后我想在特定位置放置小的 ggplot2 图,给定它们的经度和纬度。这些可以是主题最少的条形图。
我的数据库可能包含以下列:
1. town
2. longitude
3. latitude
4. through 6. value A, B, C
我生成一个图(伪代码)
p <- ggmap(coordinates)
我有我的最小 ggplot2 设计
q<-ggplot2()+geom_bar(....)+ ... x-axis null y axis null minimal template
如何将这两种设计结合起来,使 ggmap 具有在地图的特定坐标上施加的最小 ggplot 图?
这是我使用饼图作为散点图上的点所做的一个。您可以使用相同的概念将条形图放在地图上特定 lat/long 坐标处。
需要进一步更新。使用的一些代码是从另一个答案中缩写出来的,该答案已被删除。如果您通过搜索引擎找到此答案,请留下评论,我会抽空充实它。
已更新:
主要使用您的答案中的改编代码,但我不得不更新几行。
p <- ggmap(Poland) + coord_quickmap(xlim = c(13, 25), ylim = c(48.8, 55.5), expand = F)
此更改可实现更好的投影并消除有关重复比例的警告。
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)))
这里我明确指定了你的 geom_col
的闪避宽度,所以我可以将它与 geom_text
匹配。我使用 round(value, 1)
作为标签美学,它会自动从 subplots = ggplot(...)
调用中继承 x
和 y
美学。我还手动将尺寸设置得非常小,以便标签适合,但随后我增加了每个子群的整体边界框,每个方向从 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")}
我在这里做的唯一改变是 "ghost" geom_col
的美学。当它们设置为 0,0 时,它们根本不会被绘制,因为那不在 x 和 y 的限制范围内。通过使用 Inf,Inf,它们被绘制在最右上角,这足以使它们不可见,但仍然为图例绘制。