我想使用给定的数据框和一列
叫frequency
来策划美国的县,
并使用频率为县上色。柱。
为此,我这样做:
data(county.fips) # Load the county.fips dataset for plotting
cnty <- map_data("county") # Load the county data from the maps package
cnty2 <- cnty %>%
mutate(polyname = paste(region, subregion, sep=",")) %>%
left_join(county.fips, by="polyname")
head(cnty2)
long lat group order region subregion polyname fips
-86.5051651 32.3491974 1 1 alabama autauga alabama,autauga 1001
-86.5338211 32.3549271 1 2 alabama autauga alabama,autauga 1001
-86.5452728 32.3663864 1 3 alabama autauga alabama,autauga 1001
-86.5567322 32.3778458 1 4 alabama autauga alabama,autauga 1001
-86.5796585 32.3835716 1 5 alabama autauga alabama,autauga 1001
-86.5911102 32.3778458 1 6 alabama autauga alabama,autauga 1001
我自己的数据表的前几行
看起来像dt
:
query NN freq long lat group order region subregion
53047 55121 7308 -91.4383392 43.9916992 3048 90132 wisconsin trempealeau
53047 55121 7308 -91.4956284 44.0146179 3048 90133 wisconsin trempealeau
53047 55121 7308 -91.5471954 44.0318031 3048 90134 wisconsin trempealeau
53047 55121 7308 -91.5529251 44.1292114 3048 90136 wisconsin trempealeau
53047 55121 7308 -91.5701141 44.1463966 3048 90137 wisconsin trempealeau
polyname
wisconsin,trempealeau
wisconsin,trempealeau
wisconsin,trempealeau
wisconsin,trempealeau
wisconsin,trempealeau
如何通过fips列更改两个县的边框颜色?
假设我希望与53047
和55121
列中的query
和NN
关联的县
红色和黄色边界。
到目前为止,我所做的是:
ggplot(dt, aes(long, lat, group = group)) +
geom_polygon(data = county2, fill="lightgrey") +
geom_polygon(aes(fill = analog_freq), colour = rgb(1, 1, 1, 0.2)) +
coord_quickmap() +
theme(legend.title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank()) +
ggtitle(title_p)
有什么建议吗?
答案 0 :(得分:3)
对于这些情节,我喜欢scale_*_identity()
。看看这个例子是否对您有帮助。
states <-
map_data("state") %>%
group_by(region) %>%
filter(min(long) < -100) %>%
ungroup() %>%
# specify the colors I want to use + line thickness
mutate(
outline = case_when(
region == "idaho" ~ "yellow",
region == "arizona" ~ "red",
TRUE ~ "white"),
size = ifelse(outline == "white", 0.5, 3)
)
ggplot(states, aes(long, lat, group = group)) +
geom_polygon(aes(color = outline, size = size)) +
scale_color_identity() +
scale_size_identity()