使用facet wrap在R中映射不同的状态 [英] Mapping different states in R using facet wrap

查看:169
本文介绍了使用facet wrap在R中映射不同的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在ggplot2中使用facet_wrap将不同的状态映射到多方面情节的每个方面。我的代码在这里列出,其中'dataset'是一个数据框,其中包含一个名为'state'的变量,其中包含我想由facet映射的不同状态的名称。

  library(maps)
library(ggplot2)

states <-c( 'Oklahoma','Arizona','Washington','North
Dakota','Michigan','Florida')

map< - ggplot(data = datatest)+ geom_polygon data = map_data
('state',region = datatest $ state),aes
(x = long,y = lat,group = group),color ='black',fill ='white') +
facet_wrap(〜state,ncol = 3)

我无法获得每个方面都是一个不同状态的地图。相反,每个方面都是整个美国的地图,其中列出了六个列出的州。如果有人知道如何让这个工作在每个方面都是不同的状态,我将不胜感激。我很确定它与调用map_data时定义的区域有关,但我尝试过的任何操作都没有成功。这可能无法完成,但我想我会看看有没有人有想法。谢谢!

解决方案

状态不存在于 map_data 。在那里,你正在寻找的列被称为 region 。此外,至少在你的例子中,没有数据是从 datatest 数据绘制的。所以,你可以忽略它。



这段代码应该可以工作。请注意,我添加了 scales =free,因为我假设您希望每个状态都填充相应的方面。



< pre $ ggplot(map_data('state',region = states)
,aes(x = long,y = lat,group = group))+
geom_polygon color ='black',fill ='white')+
facet_wrap(〜region
,scales =free
,ncol = 3)

提供



请注意,宽高比将使用 facet_wrap 关闭,因为这些方面无法处理 coord_map 控件。为了使情节更好,我建议分别制作每个状态图,然后使用 cowplot 中的 plot_grid 将它们拼接在一起。请注意, cowplot 会加载一个默认主题,所以您需要重置默认值(使用 theme_set )或者显式设置(如我在这里所做的):

  sepStates < -  
lapply(states,function( thisState){
ggplot(map_data('state',region = thisState)
,aes(x = long,y = lat,group = group))+
geom_polygon(color ='black ',fill ='white')+
facet_wrap(〜region)+
coord_map()+
theme_void()
})


library(cowplot)
plot_grid(plotlist = sepStates)

给出



如果您想包含来自其他来源的数据,则需要确保它是兼容的。尤其是,您需要确保您想要基于的分栏在两个方面都被称为相同的内容。



让我们假设您拥有以下数据想添加到图中:

  datatest < -  
structure(list(zip = c(85246 ,85118,85340,34958,33022,
32716,49815,48069,48551,58076,58213,58524 b $ b73185,74073,73148,98668,98271,98290),city = c(Chandler,
Gold Canyon,Litchfield Park Jensen Beach,好莱坞,
Altamonte Springs,Channing,Pleasant Ridge,Flint,Wahpeton,
Ardoch,Braddock, ,Sperry,Oklahoma City,
Vancouver,Marysville,Snohomish),state = c(AZ,AZ,
AZ,FL FL,FL,MI,MI,MI,ND,ND,ND,OK,
,OK,OK, WA,WA,WA),纬度= c(33.276539,33.34,
33.50835,27.242402,26.013368,28.744752,46.186913,42.472235,
42.978995,4 6.271839,48.204374,46.596608,35.551409,36.306323,
35.551409,45.801586,48.093129,47.930902),经度= c(-112.18717,
-111.42,-112.40523,-80.224613,-80.144217,-81.22328, - 88.04546,
-83.14051,-83.713124,-96.608142,-97.30774,-100.09497,-97.407537,
-96.02081,-97.407537,-122.5203​​47,-122.21614,-122.03976)),.Name = c zip,
city,state,latitude,longitude),row.names = c(NA,
-18L),class = c(tbl_df, tbl,data.frame))

看起来像这样:

  zip city state纬度经度
< chr> < CHR> < CHR> < DBL> < DBL>
1 85246 Chandler AZ 33.27654 -112.18717
2 85118 Gold Canyon AZ 33.34000 -111.42000
3 85340 Litchfield Park AZ 33.50835 -112.40523
4 34958 Jensen Beach FL 27.24240 -80.22461
5 33022 Hollywood FL 26.01337 -80.14422
6 32716 Altamonte Springs FL 28.74475 -81.22328
7 49815钱宁MI 46.18691 -88.04546
8 48069愉悦岭MI 42.47223 -83.14051
9 48551弗林特MI 42.97899 -83.71312
10 58076 Wahpeton ND 46.27184 -96.60814
11 58213 Ardoch ND 48.20437 -97.30774
12 58524 Braddock ND 46.59661 -100.09497
13 73185 Oklahoma City OK 35.55141 -97.40754
14 74073 Sperry OK 36.30632 -96.02081
15 73148 Oklahoma City OK 35.55141 -97.40754
16 98668 Vancouver WA 45.80159 -122.5203​​5
17 98271 Marysville WA 48.09313 -122.21614
18 98290Snohomish WA 47.93090 -122.03976

如果你想面向这个状态,你需要把它变成与地图数据中相同的格式(即全名和小写),并调用列相同的东西( region 而不是状态)。另外,如果你使列名完全相同,这也是最简单的。在这里,我添加了列以匹配从 map_data 绘制的三个列,并添加了一个 region 列,允许分面:

  stateList<  -  
setNames(tolower(state.name),state.abb)

datatest $ lat < - datatest $ latitude
datatest $ long < - datatest $ longitude
datatest $ group < - NA
datatest $ region < - stateList [datatest $ state]

现在,您可以添加 geom_point() line to the plot,and face facet correctly:

  ggplot(map_data('state', region = states)
,aes(x = long,y = lat,group = group))+
geom_polygon(color ='black',fill ='white')+
geom_point data = datatest)+
facet_wrap(〜region
,scales =free
,ncol = 3)

给出





或者,你可以将它添加到 cowplot 方法中(请注意,我现在只是在标题中跳过这一切)。



<$ p $ (状态,函数(thisState){
ggplot(map_data('state',region = thisState)
,aes(x = long = y = lat,group = group))+
geom_polygon(color ='black',fill ='white')+
geom_point(data = datatest [datatest $ region == tolower(thisState) ,))+
ggtitle(thisState)+
coord_map()+
theme_void()+
主题(plot.title = element_text(hjust = 0.5))
})

plot_grid(plotlist = sepStates)

给出




I am trying to use facet_wrap in ggplot2 to map a different state into each facet of a multi-faceted plot. My code is listed here, where 'dataset' is a data frame containing a variable named 'state', which contains the names of the different states I want mapped by facet.

library(maps)
library(ggplot2)

states <- c('Oklahoma','Arizona','Washington','North     
Dakota','Michigan','Florida')

map <- ggplot(data=datatest) + geom_polygon(data=map_data
('state',region=datatest$state),aes
(x=long,y=lat,group=group),colour='black',fill='white') + 
facet_wrap(~state,ncol=3)

I'm not able to get each facet to be a map of a different state. Instead, each facet is a map of the whole United States with the six listed states appearing on it. If anyone knows of a way to get this to work where each facet is a different state by itself, I would greatly appreciate it. I'm pretty sure it has to do with how the 'region' is defined in the call to map_data, but nothing I've tried is working. It's possible this can't be done, but I thought I'd see if anyone has an idea. Thank you!

解决方案

The column state is not present in the return from map_data. There, the column you are looking for is called region. Further, at least in your example, there is no data being plotted from the datatest data. So, you can omit it.

This code should work. Note that I added scales = "free" because I am assuming that you want each state to fill it's corresponding facet.

ggplot(map_data('state',region=states)
       , aes(x=long,y=lat,group=group)) +
  geom_polygon(colour='black',fill='white') + 
  facet_wrap(~region
             , scales = "free"
             , ncol=3)

Gives

Note that the aspect ratios are going to be off using facet_wrap because the facets can't handle the coord_map controls. To make the plot better, I would suggest making each state map separately and then using plot_grid from cowplot to stitch them together. Note that cowplot loads a default theme, so you will want to either reset your default (with theme_set) or explicitly set a theme for the plots (as I do here):

sepStates <-
  lapply(states, function(thisState){
    ggplot(map_data('state',region=thisState)
           , aes(x=long,y=lat,group=group)) +
      geom_polygon(colour='black',fill='white') +
      facet_wrap(~region) +
      coord_map() +
      theme_void()
  })


library(cowplot)
plot_grid(plotlist = sepStates)

gives

If you want to include data from another source, you will need to make sure that it is compatible. In particular, you need to make sure that the column you want to be facetting based on is called the same thing in both.

Let's imagine you have the following data that you want to add to the plot:

datatest <-
structure(list(zip = c("85246", "85118", "85340", "34958", "33022", 
"32716", "49815", "48069", "48551", "58076", "58213", "58524", 
"73185", "74073", "73148", "98668", "98271", "98290"), city = c("Chandler", 
"Gold Canyon", "Litchfield Park", "Jensen Beach", "Hollywood", 
"Altamonte Springs", "Channing", "Pleasant Ridge", "Flint", "Wahpeton", 
"Ardoch", "Braddock", "Oklahoma City", "Sperry", "Oklahoma City", 
"Vancouver", "Marysville", "Snohomish"), state = c("AZ", "AZ", 
"AZ", "FL", "FL", "FL", "MI", "MI", "MI", "ND", "ND", "ND", "OK", 
"OK", "OK", "WA", "WA", "WA"), latitude = c(33.276539, 33.34, 
33.50835, 27.242402, 26.013368, 28.744752, 46.186913, 42.472235, 
42.978995, 46.271839, 48.204374, 46.596608, 35.551409, 36.306323, 
35.551409, 45.801586, 48.093129, 47.930902), longitude = c(-112.18717, 
-111.42, -112.40523, -80.224613, -80.144217, -81.22328, -88.04546, 
-83.14051, -83.713124, -96.608142, -97.30774, -100.09497, -97.407537, 
-96.02081, -97.407537, -122.520347, -122.21614, -122.03976)), .Names = c("zip", 
"city", "state", "latitude", "longitude"), row.names = c(NA, 
-18L), class = c("tbl_df", "tbl", "data.frame"))

which looks like this:

     zip              city state latitude  longitude
   <chr>             <chr> <chr>    <dbl>      <dbl>
 1 85246          Chandler    AZ 33.27654 -112.18717
 2 85118       Gold Canyon    AZ 33.34000 -111.42000
 3 85340   Litchfield Park    AZ 33.50835 -112.40523
 4 34958      Jensen Beach    FL 27.24240  -80.22461
 5 33022         Hollywood    FL 26.01337  -80.14422
 6 32716 Altamonte Springs    FL 28.74475  -81.22328
 7 49815          Channing    MI 46.18691  -88.04546
 8 48069    Pleasant Ridge    MI 42.47223  -83.14051
 9 48551             Flint    MI 42.97899  -83.71312
10 58076          Wahpeton    ND 46.27184  -96.60814
11 58213            Ardoch    ND 48.20437  -97.30774
12 58524          Braddock    ND 46.59661 -100.09497
13 73185     Oklahoma City    OK 35.55141  -97.40754
14 74073            Sperry    OK 36.30632  -96.02081
15 73148     Oklahoma City    OK 35.55141  -97.40754
16 98668         Vancouver    WA 45.80159 -122.52035
17 98271        Marysville    WA 48.09313 -122.21614
18 98290         Snohomish    WA 47.93090 -122.03976

If you want to facet on the state, you need to make it into the same format (i.e., full name and lower case) as in the map data and call the column the same thing (region instead of state). In addition, it is easest if you make the column names all the same as well. Here, I am adding columns to match the three that are being plotted from the map_data and adding a region column that will allow the facetting:

stateList <-
  setNames(tolower(state.name), state.abb)

datatest$lat <- datatest$latitude
datatest$long <- datatest$longitude
datatest$group <- NA
datatest$region <- stateList[datatest$state]

Now, you can add a geom_point() line to the plot, and it will facet correctly:

ggplot(map_data('state',region=states)
       , aes(x=long,y=lat,group=group)) +
  geom_polygon(colour='black',fill='white') +
  geom_point(data = datatest) +
  facet_wrap(~region
             , scales = "free"
             , ncol=3)

Gives

Or, you can add it to the cowplot approach (note that I am just titling now and skipping the facetting).

sepStates <-
  lapply(states, function(thisState){
    ggplot(map_data('state',region=thisState)
           , aes(x=long,y=lat,group=group)) +
      geom_polygon(colour='black',fill='white') +
      geom_point(data = datatest[datatest$region == tolower(thisState), ]) +
      ggtitle(thisState) +
      coord_map() +
      theme_void() +
      theme(plot.title = element_text(hjust = 0.5))
  })

plot_grid(plotlist = sepStates)

gives

这篇关于使用facet wrap在R中映射不同的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆