小编cor*_*e r的帖子

安排美国形状的ggplot facets

我有一个ggplot,每个美国州都有一个方面.我希望将这些方面排列成美国的形状,边框参差不齐(如第二张地图所示,但没有夏威夷或阿拉斯加).

为此,我创建了一个由美国州订购的州级因子变量,在地图上从左向右读取.这个因素还包含我想删除的空白面的"空间持有者".我按照这篇文章的建议(参见编辑提供的答案)但是names(g$grobs)为NULL,所以我无法实现他们的答案.任何想法我能做什么?

这是我的代码:

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", …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

18
推荐指数
2
解决办法
776
查看次数

如何保留所有刻度线,但删除x轴上的大多数网格线?

假设我想使用ggplot创建一个类似下面指定的图,但是我想保留x轴上的所有刻度线(对于每个整数),但只显示5,10,15,20和5的网格线25.如何修改代码以删除无关的网格线?

网格线太多了!

ggplot(cars, aes(x = speed, y = dist)) + 
geom_point() + 
scale_x_continuous(breaks = seq(1, 25, 1), 
                   limits = c(1, 25),
                   labels = seq(1, 25, 1)) + 
theme(panel.grid.minor.x = element_blank())
Run Code Online (Sandbox Code Playgroud)

r ggplot2

4
推荐指数
1
解决办法
1409
查看次数

标签 统计

ggplot2 ×2

r ×2