gpt4 book ai didi

r - 如何仅在数字大于阈值时绘制 ggplots 六边形

转载 作者:行者123 更新时间:2023-12-05 05:12:59 27 4
gpt4 key购买 nike

我想用 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 提取绘制的数据。

获得所需情节的一种方法是使用 cut,就像这里提到的:https://unconj.ca/blog/not-all-population-maps-are-boring.html对数据进行分箱。

如果您从 4 而不是 0 开始,则 5 以下的所有内容都将映射到 NA,不会绘制这些点,然后您可以在 中使用 breaks code>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

关于r - 如何仅在数字大于阈值时绘制 ggplots 六边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54092169/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com