国家和省份地图上的ggplot数据

时间:2016-08-26 17:20:28

标签: r ggplot2 spatial

我需要将我的离岸数据绘制在美国地图上。 ggplot answer by jlhoward很有帮助,但我试图将自己的数据覆盖在其上并遇到问题。我的数据位于data.frame中,如下所示:

> dput(dat)
 structure(list(YEAR = 1982:2014, Longitude = c(-70.412294, -72.364029, 
-71.718199, -71.026232, -70.057038, -71.256635, -71.33416, -71.921617, 
-70.764959, -69.996114, -70.735911, -70.804216, -70.971903, -70.380952, 
-70.840294, -70.246243, -69.998864, -70.415875, -70.803285, -71.590647, 
-70.911025, -70.893613, -72.290058, -72.178312, -71.545455, -72.59155, 
-72.386746, -72.427943, -72.799752, -72.897847, -71.893173, -72.749717, 
-69.732889), Latitude = c(40.723863, 39.711704, 40.196502, 40.38192, 
40.618407, 40.461734, 40.63319, 40.255586, 40.566896, 40.636625, 
40.658859, 40.600427, 40.496296, 40.886167, 40.597873, 40.74334, 
40.996396, 40.88574, 41.015681, 40.751503, 40.917864, 40.756501, 
40.370116, 40.232457, 40.761132, 39.940429, 40.312277, 40.165928, 
39.83173, 39.814042, 40.530672, 39.967331, 40.730932), biomass = c(0.338144811453591, 
0.279218697044777, 0.237201626514534, 0.336660946471182, 0.342330121351236, 
0.369994373320721, 0.324471285554554, 0.329548963755295, 0.385566422852207, 
0.371231941937523, 0.363874003449712, 0.394639716203538, 0.38023963566932, 
0.399016421268399, 0.386505432090588, 0.42981456156909, 0.343536916969732, 
0.39892372171312, 0.355308306725635, 0.336114098583543, 0.41596425093632, 
0.329576428474592, 0.306697644479785, 0.262190121610882, 0.379213749266384, 
0.362876021807967, 0.409606747502569, 0.358005533873342, 0.308136419268288, 
0.370401008590535, 0.400519345003107, 0.381512816639217, 0.401243046609029
), code = c("late", "early", "average", "average", "average", 
"average", "late", "average", "late", "average", "average", "average", 
"late", "average", "late", "late", "average", "average", "average", 
"average", "average", "average", "average", "average", "average", 
"average", "average", "average", "average", "average", "early", 
"early", "average"), alpha = c(1, 1, 0.5, 0.5, 0.5, 0.5, 1, 0.5, 
1, 0.5, 0.5, 0.5, 1, 0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 1, 0.5)), .Names = c("YEAR", 
"Longitude", "Latitude", "biomass", "code", "alpha"), row.names = c(NA, 
-33L), class = "data.frame")

我用于绘图的代码是。

library(raster)
library(maps)
library(ggmap)
library(ggplot2)
library(scales)
states    <- c('Maine', 'New Hampshire', 'Vermont', 'Massachusetts', 'Rhode Island', 'Connecticut','New York', 'Pennsylvania','West Virginia', 'Delaware', 'New Jersey', 'Maryland', 'Virginia', 'North Carolina')
provinces <- c("Nova Scotia", 'New Brunswick')
us <- getData("GADM",country="USA",level=1)
canada <- getData("GADM",country="CAN",level=1)
us.states <- us[us$NAME_1 %in% states,]
ca.provinces <- canada[canada$NAME_1 %in% provinces,]
xlim <- c(-77, -65)
ylim <- c(35,45)

ggplot(us.states, aes(x=long, y=lat,group=group))+
  geom_path()+
  geom_path(data=ca.provinces)+
  labs(title='title') +
  coord_map(xlim=xlim, ylim=ylim, project='mercator') +
  geom_point(aes(x=Longitude,y=Latitude,size=biomass),
           color='black',fill='gray',
           data=dat[dat$code=='average' & dat$YEAR>1982,],
           shape=21,alpha=1) + 
   scale_size_continuous(name='Biomass') +
   geom_point(aes(x=Longitude,y=Latitude,size=biomass),
           color='black',fill='red',
           data=dat[dat$code=='early',],shape=21,alpha=1) +   
   geom_point(aes(x=Longitude,y=Latitude,size=biomass),
           color='black',fill='cyan',data=dat[dat$code=='late',],
           shape=21,alpha=1)+    
   geom_point(aes(x=Longitude,y=Latitude,size=biomass),
           color='black',fill='gray',
           data=dat[dat$code=='average' & dat$YEAR==1982,],
           shape=21,alpha=1) + 
   # to fix the color in the scale
   labs(x = "Longitude", y = "Latitude", size = 20) +
   theme(axis.text = element_text(size = rel(1.25)),
           axis.title = element_text(size = rel(1.25)),
           plot.title = element_text(size = rel(2))) +
   theme_bw()

我收到以下错误消息。

Error in eval(expr, envir, enclos) : object 'group' not found

有没有人对这里出了什么问题有所了解?如果我注释掉coord_map(...)下面的代码,一切运行正常。

1 个答案:

答案 0 :(得分:1)

因为您在group = group的主要调用中指定了aes(),所以ggplot希望每个geom包含一个名为&#34; group&#34;的列。在geom_point来电中,您指定的数据集没有&#34; group&#34;柱。这是您的错误的主要来源(另一个是您的代码包含一块似乎已被错误粘贴的无实体代码)。

您可以告诉geom_point的每次通话重置&#34;组&#34;通过指定group = 1来变量。

此代码:

ggplot(us.states, aes(x=long, y=lat,group=group))+
    geom_path()+
    geom_path(data=ca.provinces)+
    labs(title='title') +
    coord_map(xlim=xlim, ylim=ylim, project='mercator') +
    geom_point(aes(x=Longitude,y=Latitude,size=biomass), color='black',fill='gray', data=dat[dat$code=='average' & dat$YEAR>1982,], shape=21,alpha=1, group = 1) + 
    scale_size_continuous(name='Biomass') +   
    geom_point(aes(x=Longitude,y=Latitude,size=biomass), group = 1, color='black',fill='cyan',data=dat[dat$code=='late',],shape=21,alpha=1) +    
    geom_point(aes(x=Longitude,y=Latitude,size=biomass),group = 1, color='black',fill='gray', data=dat[dat$code=='average' & dat$YEAR==1982,],shape=21,alpha=1) + 
    # to fix the color in the scale
    labs(x = "Longitude", y = "Latitude", size = 20) +
    theme(axis.text = element_text(size = rel(1.25)),
          axis.title = element_text(size = rel(1.25)),
          plot.title = element_text(size = rel(2))) +
    theme_bw()

制作此图片:

enter image description here

编辑:备用

下面的图表版本以多种方式清理代码:

  1. &#34; dat&#34;数据集现在是主要的数据集。加拿大和美国地图数据仅用于绘制边界线,因此您可以通过这种方式节省更多内容。确保所有&#34;晚期&#34;将点数绘制在其他点之上,我事先在数据集上使用dplyr::arrange(这会将所有&#34;后期&#34;点按字母顺序排在最后,因此它们将在最后绘制。)
  2. 只需拨打geom_point即可绘制点数。填充和大小定义为美学,为您节省多个geom_point来电的冗余。
  3. 绘图元素按重要性顺序排列:主要调用,然后是geoms,然后是坐标规范,然后是缩放,然后是标签和主题。
  4. 总的来说,这使得绘图代码更容易理解。

    library(dplyr)
    dat <- arrange(dat, code)
    
    ggplot(data = subset(dat, YEAR > 1982), aes(x = Longitude, y = Latitude)) +
        geom_path(data = us.states, aes(x = long, y = lat, group = group)) +
        geom_path(data=ca.provinces, aes(x = long, y = lat, group = group)) +
        geom_point(aes(fill = code, size = biomass), color='black', shape = 21) + 
        coord_map(xlim = xlim, ylim = ylim, project = 'mercator') +
        scale_fill_manual(values = c(early = 'gray', average = 'gray', late = 'cyan')) +
        scale_size_continuous(name = 'Biomass') +
        labs(title = 'Title', x = 'Longitude', y = 'Latitude') +
        theme(axis.text = element_text(size = rel(1.25)),
              axis.title = element_text(size = rel(1.25)),
              plot.title = element_text(size = rel(2))) +
        theme_bw()
    

    enter image description here