gpt4 book ai didi

r - 等值线 : Control polygon fills

转载 作者:行者123 更新时间:2023-12-04 12:21:49 25 4
gpt4 key购买 nike

我已经上传了一个 shapefile here .

#First, read it in
library(rgdal)
pols <- readOGR(dsn="stack", layer="shapefile")

plot.df <- broom::tidy(pols, by="nr") %>% mutate(id = as.numeric(id))

#some values I'd like to plot
plot.df$value[plot.df$id==0] <- 4
plot.df$value[plot.df$id==1] <- 3
plot.df$value[plot.df$id==2] <- 3

library(ggplot2)
ggplot(plot.df, aes(x=long, y=lat))+
geom_polygon(aes(fill=factor(value), group=group))+
coord_fixed()+
scale_fill_manual(values = c("red", "green", "blue", "orange"),
breaks = c("1", "2", "3", "4"))

此代码应生成以下图: enter image description here

问题是,我应该出现另一个多边形(绿色)。

我想出了一个解决方法:

ggplot(plot.df, aes(x=long, y=lat))+
geom_polygon(aes(fill=factor(value), group=group))+
geom_polygon(data=subset(plot.df, id==0), aes(fill=factor(value), group=group))+
coord_fixed()+
scale_fill_manual(values = c("red", "green", "blue", "orange"),
breaks = c("1", "2", "3", "4"))

enter image description here

不过,我确信有更优雅的解决方案。按照建议使用 id 作为分组变量 here导致扭曲的多边形。

还有人有其他想法吗?

最佳答案

多边形重叠,可以设置alpha

ggplot(plot.df, aes(x = long, y = lat)) +
geom_polygon(aes(fill = factor(value), group = group), alpha = .4)+
coord_fixed() +
scale_fill_manual(values = c("red", "green", "blue", "orange"), breaks = c("1", "2", "3", "4")) +
theme(legend.position = 'none')

enter image description here

关于r - 等值线 : Control polygon fills,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47796174/

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