R 以美国的形状排列平面
我有一个GGP图,每个美国州都有一个方面。我想把这些方面安排成美国的形状,边界不规则(,但没有夏威夷或阿拉斯加) 为此,我创建了一个州级别的因子变量,该变量由美国各州排序,在地图上从左到右读取。该因子还包含我想要删除的空白面的“空格保持器”。我遵循了的建议(请参见对提供答案的编辑),但是R 以美国的形状排列平面,r,ggplot2,R,Ggplot2,我有一个GGP图,每个美国州都有一个方面。我想把这些方面安排成美国的形状,边界不规则(,但没有夏威夷或阿拉斯加) 为此,我创建了一个州级别的因子变量,该变量由美国各州排序,在地图上从左到右读取。该因子还包含我想要删除的空白面的“空格保持器”。我遵循了的建议(请参见对提供答案的编辑),但是names(g$grobs)为空,因此我无法实现他们的答案。你知道我能做什么吗 这是我的密码: 库(ggplot2) 图书馆(五三八) 图书馆(dplyr) 图书馆(gridExtra) 数据(“警察死亡”)
names(g$grobs)
为空,因此我无法实现他们的答案。你知道我能做什么吗
这是我的密码:
库(ggplot2)
图书馆(五三八)
图书馆(dplyr)
图书馆(gridExtra)
数据(“警察死亡”)
警察死亡人数百分比排列(州,年份)%%>%分组(州,年份)%%>%count()
警察死亡人数百分比安排(州,-年)%>%
过滤器(年份%c(1970:2015)和状态%c(“AK”、“HI”、“US”、“GU”、“MP”、“PR”、“RR”、“TR”、“VI”))
police_deatments_count$state.name一个骇客选项是为空面创建唯一的空白条标签,以便它们可以用作占位符,但不创建任何可见的条标签。使用州缩写而不是全名可能更好,但我在这里没有这样做。下面是一个例子:
library(ggplot2)
library(fivethirtyeight)
library(dplyr)
library(gridExtra)
data("police_deaths")
police_deaths_count <- police_deaths %>% arrange(state, -year) %>% group_by(state, year) %>% count()
police_deaths_count <- police_deaths_count %>% arrange(state, -year) %>%
filter(year %in% c(1970:2015) & !state %in% c("AK", "HI", "US", "GU", "MP", "PR", "RR", "TR", "VI"))
# Create unique blank strip labels for empty facets
bl = sapply(1:37, function(n) paste(rep(" ",n),collapse=""))
police_deaths_count$state.name <- state.name[match(police_deaths_count$state, state.abb)]
police_deaths_count$state.name[police_deaths_count$state == "DC"] <- "Washington DC"
police_deaths_count$state.reorder <- factor(police_deaths_count$state.name,
levels = c(bl[1:10], "Maine",
bl[11:19], "Vermont", "New Hampshire",
"Washington", "Idaho", "Montana", "North Dakota", "Minnesota", "Illinois", "Wisconsin", "Michigan", "New York", "Massachusetts", "Rhode Island",
"Oregon", "Nevada", "Wyoming", "South Dakota", "Iowa", "Indiana", "Ohio", "Pennsylvania", "New Jersey", "Connecticut", bl[20],
"California", "Utah", "Colorado", "Nebraska", "Missouri", "Kentucky", "West Virginia", "Virginia", "Maryland", "Washington DC", bl[21],
bl[22], "Arizona", "New Mexico", "Kansas", "Arkansas", "Tennessee", "North Carolina", "South Carolina", "Delaware", bl[23:24],
bl[25:27], "Oklahoma", "Louisiana", "Mississippi", "Alabama", "Georgia", bl[28:29],
bl[30:33], "Texas", bl[34:37], "Florida"))
police_deaths_count2 <- police_deaths_count %>% filter(!(state=="NY" & year==2001))
plot1 <- ggplot(subset(police_deaths_count2, is.na(state.name)==F), #take away 9-11 peak to see trends without it
aes(y = n, x = year)) +
geom_line() +
facet_wrap( ~ state.reorder, ncol = 11, drop = F, strip.position="bottom") +
theme_classic() +
theme(axis.text.x = element_blank(),
strip.background=element_blank(),
axis.line=element_blank(),
axis.ticks=element_blank()) +
ylab("Count of police deaths") +
xlab("Year (1970 - 2015)")
库(ggplot2)
图书馆(五三八)
图书馆(dplyr)
图书馆(gridExtra)
数据(“警察死亡”)
警察死亡人数百分比排列(州,年份)%%>%分组(州,年份)%%>%count()
警察死亡人数百分比安排(州,-年)%>%
过滤器(年份%c(1970:2015)和状态%c(“AK”、“HI”、“US”、“GU”、“MP”、“PR”、“RR”、“TR”、“VI”))
#为空镶嵌面创建唯一的空白条标签
bl=sapply(1:37,函数(n)粘贴(rep(“,n),collapse=”“))
police_death_count$state.name在GGally包中有一个名为glyphs的函数,可以实现这些功能。GeoFact
包应该可以很好地工作:这太棒了!谢谢。仅供参考,粘贴(rep(“,n),collapse=”“)
==strrep(“,n)
。回答很酷。谢谢你@JonathanCarroll。很高兴了解strrep
。谢谢您的指点,Dianne。我还没有用过GGALY,但肯定会看一看:)看看这个软件包()这个问题的灵感来源于我问了这个问题后推出的GeoFact
,但我现在自己也用了:)它确实用过,但对于那些偶然发现你的问题的人来说,更新这些问题总是很有用的。:)
library(ggplot2)
library(fivethirtyeight)
library(dplyr)
library(gridExtra)
data("police_deaths")
police_deaths_count <- police_deaths %>% arrange(state, -year) %>% group_by(state, year) %>% count()
police_deaths_count <- police_deaths_count %>% arrange(state, -year) %>%
filter(year %in% c(1970:2015) & !state %in% c("AK", "HI", "US", "GU", "MP", "PR", "RR", "TR", "VI"))
# Create unique blank strip labels for empty facets
bl = sapply(1:37, function(n) paste(rep(" ",n),collapse=""))
police_deaths_count$state.name <- state.name[match(police_deaths_count$state, state.abb)]
police_deaths_count$state.name[police_deaths_count$state == "DC"] <- "Washington DC"
police_deaths_count$state.reorder <- factor(police_deaths_count$state.name,
levels = c(bl[1:10], "Maine",
bl[11:19], "Vermont", "New Hampshire",
"Washington", "Idaho", "Montana", "North Dakota", "Minnesota", "Illinois", "Wisconsin", "Michigan", "New York", "Massachusetts", "Rhode Island",
"Oregon", "Nevada", "Wyoming", "South Dakota", "Iowa", "Indiana", "Ohio", "Pennsylvania", "New Jersey", "Connecticut", bl[20],
"California", "Utah", "Colorado", "Nebraska", "Missouri", "Kentucky", "West Virginia", "Virginia", "Maryland", "Washington DC", bl[21],
bl[22], "Arizona", "New Mexico", "Kansas", "Arkansas", "Tennessee", "North Carolina", "South Carolina", "Delaware", bl[23:24],
bl[25:27], "Oklahoma", "Louisiana", "Mississippi", "Alabama", "Georgia", bl[28:29],
bl[30:33], "Texas", bl[34:37], "Florida"))
police_deaths_count2 <- police_deaths_count %>% filter(!(state=="NY" & year==2001))
plot1 <- ggplot(subset(police_deaths_count2, is.na(state.name)==F), #take away 9-11 peak to see trends without it
aes(y = n, x = year)) +
geom_line() +
facet_wrap( ~ state.reorder, ncol = 11, drop = F, strip.position="bottom") +
theme_classic() +
theme(axis.text.x = element_blank(),
strip.background=element_blank(),
axis.line=element_blank(),
axis.ticks=element_blank()) +
ylab("Count of police deaths") +
xlab("Year (1970 - 2015)")