R 美国地图上的特定颜色状态

R 美国地图上的特定颜色状态,r,ggplot2,R,Ggplot2,我正在使用此ggplot2代码创建美国地图: library(ggplot2) all_states <- map_data("state") p <- ggplot() p <- p + geom_polygon( data=all_states, aes(x=long, y=lat, group = group),colour="white", fill="grey30" ) 库(ggplot2) 所有状态库(ggplot2) 图书馆(光栅) 所有状态库(ggplot

我正在使用此ggplot2代码创建美国地图:

library(ggplot2)
all_states <- map_data("state")  
p <- ggplot()
p <- p + geom_polygon( data=all_states, aes(x=long, y=lat, group = group),colour="white", fill="grey30" )
库(ggplot2)
所有状态
库(ggplot2)
图书馆(光栅)
所有状态
库(ggplot2)
图书馆(光栅)

所有状态您也可以手动添加多边形:

library(ggplot2)
library(dplyr)
all_states <- map_data("state")  
p <- ggplot()
p <- p + geom_polygon( data=all_states, aes(x=long, y=lat, group = group),colour="white", fill="grey30" )


ny <- filter(all_states, region == "new york")
tx <- filter(all_states, region == "texas")


p + geom_polygon(data = ny, aes(x=long, y=lat, group = group),fill="red") +
  geom_polygon(data = tx, aes(x=long, y=lat, group = group),fill="blue")
库(ggplot2)
图书馆(dplyr)

所有状态您也可以手动添加多边形:

library(ggplot2)
library(dplyr)
all_states <- map_data("state")  
p <- ggplot()
p <- p + geom_polygon( data=all_states, aes(x=long, y=lat, group = group),colour="white", fill="grey30" )


ny <- filter(all_states, region == "new york")
tx <- filter(all_states, region == "texas")


p + geom_polygon(data = ny, aes(x=long, y=lat, group = group),fill="red") +
  geom_polygon(data = tx, aes(x=long, y=lat, group = group),fill="blue")
库(ggplot2)
图书馆(dplyr)
所有_状态与的答案相似,但更多地反映了数据集的原始结构,减少了需要的短语:

library(ggplot2)
library(dplyr)

all_states <- map_data("state") 
# Add more states to the lists if you want
states_positive  <-c("new york")
states_negative  <- c("texas")

我是StackOverflow的新手,所以我不确定是否应该对原始答案进行这些编辑,但我觉得这些修改足够大,可以自行修改。请说我错了:)

与的答案类似,但更多地反映了数据集的原始结构,减少了所需的短语:

library(ggplot2)
library(dplyr)

all_states <- map_data("state") 
# Add more states to the lists if you want
states_positive  <-c("new york")
states_negative  <- c("texas")


我是StackOverflow的新手,所以我不确定是否应该对原始答案进行这些编辑,但我觉得这些修改足够大,可以自行修改。请说我错了:)

这个问题有用吗。只需添加一列来控制颜色。这个问题有用吗。只需添加一列来控制颜色。我学到了一些新东西,所以我投了赞成票!嗯,我学到了一些新东西,所以我投了赞成票!
# Plot results
ggplot(all_states, aes(x=long, y=lat, group = group)) +
  geom_polygon(fill="grey", colour = "white") +
  geom_polygon(fill="green", data = filter(all_states, region %in% states_positive)) +
  geom_polygon(fill="red", data = filter(all_states, region %in% states_negative))