【发布时间】:2019-06-03 04:01:10
【问题描述】:
我想用 ggplot 的漂亮框架创建一个情节。这是一个六边形的密度图。我使用了来自https://www.r-graph-gallery.com/329-hexbin-map-for-distribution/的示例代码
图形很好,但如果达到阈值,我想要这些六边形。例如:如果数字大于 4,则绘制所有值。
是否有机会保存基础聚合数据?我想用它们来进一步测试模式相似性。因此,我想删除具有四个或更少观察值的点。
通常可以通过以下方式提取数据
object <- Function_that_produces_object
object$Data_I_Want_have
我查看了文档,但是写了如何增加 Letters 的大小,而不是显示级别的数量和范围。
包
library(tidyverse)
library(viridis)
library(ggplot2)
# Get the GPS coordinates of a set of 200k tweets:
data=read.table("https://www.r-graph-gallery.com/wp-content/uploads/2017/12/Coordinate_Surf_Tweets.csv", sep=",", header=T)
# Get the world polygon
library(mapdata)
world <- map_data("world")
data %>%
filter(homecontinent=='Europe') %>%
ggplot( aes(x=homelon, y=homelat)) +
geom_hex(bins=65) +
theme_void() +
xlim(-30, 70) +
ylim(24, 72) +
scale_fill_viridis(option="B",
trans = "log",
name="Number of Tweet recorded in 8 months",
guide = guide_legend( keyheight = unit(3, units = "mm"), keywidth=unit(12, units = "mm"), label.position = "bottom", title.position = 'top', nrow=1)
) +
ggtitle( "Where people tweet about #Surf" ) +
theme(
legend.position = c(0.5, 0.09),
text = element_text(color = "#22211d"),
plot.background = element_rect(fill = "#f5f5f2", color = NA),
panel.background = element_rect(fill = "#f5f5f2", color = NA),
legend.background = element_rect(fill = "#f5f5f2", color = NA),
plot.title = element_text(size= 22, hjust=0.1, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
)
【问题讨论】:
-
好的,我一直认为 ggplot_build 只适用于直方图。谢谢。所以我们需要以某种方式说服 ggplot 只使用大于 4 的那些。
-
一种选择是您可以使用
layer_data函数(它将返回一个data.frame),然后过滤该data.frame并将其传递回ggplot -
我现在使用 ggplot_build(ggplot_data)$
data[[1]][layer_data(ggplot_data)$count>4,] 只获取大于 4 的计数。但是如何再次将它们传回ggplot?