仅当数字大于阈值时如何绘制ggplots六角形

时间:2019-01-08 12:49:58

标签: r ggplot2

我想用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")),
  )

1 个答案:

答案 0 :(得分:1)

如注释中所示,您可以使用ggplot_build提取绘制的数据。

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

如果您以4而不是0开头,则低于5的所有内容都将映射到NA,这些点将不会绘制,然后可以使用breaks中的scale_fill_viridis进行删除图例中的NA因子,然后再次从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")),
  )

最终我得到了:

enter image description here