首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >以美国的形状排列ggplot方面

以美国的形状排列ggplot方面
EN

Stack Overflow用户
提问于 2017-02-24 01:40:27
回答 3查看 1.1K关注 0票数 20

我有一个每个美国州的一个方面的ggplot。我想把这些方面安排成美国的形状,边界参差不齐(ordered like the second map,但没有夏威夷或阿拉斯加)。

为此,我创建了一个按美国州排序的州级别因子变量,在地图上从左到右读取。这个因素还包含我想要删除的空白面的“空格符”。我遵循了this post的建议(请参阅对提供的答案的编辑),但names(g$grobs)为空,因此我无法实现他们的答案。你知道我能做什么吗?

下面是我的代码:

代码语言:javascript
复制
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"))

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("e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "e10", "Maine",
                                                    "e11", "e12", "e13", "e14", "e15", "e16", "e17", "e18", "e19", "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", "e20",
                                                    "California", "Utah", "Colorado", "Nebraska", "Missouri", "Kentucky", "West Virginia", "Virginia", "Maryland", "Washington DC", "e21",
                                                    "e22", "Arizona", "New Mexico", "Kansas", "Arkansas", "Tennessee", "North Carolina", "South Carolina", "Delaware", "e23", "e24",
                                                    "e25", "e26", "e27", "Oklahoma", "Louisiana", "Mississippi", "Alabama", "Georgia", "e28", "e29",
                                                    "e30", "e31", "e32", "e33", "Texas", "e34", "e35", "e36", "e37", "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) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  ylab("Count of police deaths") +
  xlab("Year")

#the order of these facets is what I want. From here, I'd like to display the facets e1, e2, ..., e37 as completely blank by removing their facet strips and panels.
plot1

#A SO post (next line) provides a potential solution, but it doesn't work for me
#https://stackoverflow.com/questions/30372368/adding-empty-graphs-to-facet-wrap-in-ggplot2 
g <- ggplotGrob(plot1)
names(g$grobs) #this is NULL so I can't implement the SO answer.
g$layout$name
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-02-24 02:25:04

一个hack-ish选项是为空面创建唯一的空白条带标签,以便它们可以用作占位符,但不会创建任何可见的条带标签。使用状态缩写而不是全名也可能更好,但我在这里没有这样做。下面是一个例子:

代码语言:javascript
复制
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)")

票数 19
EN

Stack Overflow用户

发布于 2017-10-19 01:06:45

https://github.com/hafen/geofacetgeofacet包应该可以很好地工作

票数 8
EN

Stack Overflow用户

发布于 2017-05-04 13:08:46

有一个名为glyphs in the GGally package的函数可以实现这些功能。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42422669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档