【问题标题】:how to Plot ggplots hexagon only if number greater than threshold仅当数字大于阈值时如何绘制 ggplots 六边形
【发布时间】: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?

标签: r ggplot2


【解决方案1】:

如 cmets 所示,您可以使用 ggplot_build 提取绘图数据。

获得所需绘图的一种方法是使用cut,如下所述:https://unconj.ca/blog/not-all-population-maps-are-boring.html 对数据进行分箱。

如果你从 4 开始而不是 0,则低于 5 的所有内容都将映射到 NA,这些点将不会被绘制,然后你可以在 scale_fill_viridis 中使用 breaksNA 因子从图例,然后您再次从ggplot_build 获得绘图数据。

这就是我的意思:

df <- read.table("https://www.r-graph-gallery.com/wp-content/uploads/2017/12/Coordinate_Surf_Tweets.csv", sep=",", header=T)
df %>%
  filter(homecontinent=='Europe') %>% 
  ggplot( ) + 
  geom_hex(aes(x=homelon, y=homelat, 
               fill = cut(..count.., c(4, 10, 50, 100, 500, 1000, 2000, Inf))), 
           bins=65) +
  theme_void() +
  xlim(-30, 70) +
  ylim(24, 72) + 
  scale_fill_viridis(option="B",
                     breaks = cut(c(5, 10, 50, 100, 500, 1000, 2000), 
                                  c(4, 10, 50, 100, 500, 1000, 2000, Inf)),
                     labels = c("5-9 ", "10-49 ", "50-99 ", "100-499 ", "500-999 ", "1000-1999", '2000+'), 
                     name="Number of Tweet recorded in 8 months",
                     discrete = TRUE,
                     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")),
  )

最终我得到了这个:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多