关于geom_map的错误:“单位错误(x,default.units):'x'和'units'必须长度> 0”

时间:2016-11-25 09:41:05

标签: r animation ggplot2

library(dplyr)
library(plotly)
library(wesanderson)
library(animation)
library(ggthemes)
library(scales)
states_map <-map_data("state")

StateCodes <- c("AL","AK","CA","AL","AK","CA")
year <- c(2010,2010,2010,2011,2011,2011)
n <- c(1.1,1.2,1.3,2.1,2.2,2.3)
data <- data.frame(StateCodes,year,n)

saveGIF({
  for (i in 2010:2011) {

    m<-ggplot(subset(data,year==i), aes(map_id = StateCodes)) +
      geom_map(aes(fill = (n)), map = states_map, color ="black") +
      expand_limits(x = states_map$long, y = states_map$lat) +
      theme_few()+
      theme(legend.position = "bottom",
            axis.ticks = element_blank(), 
            axis.title = element_blank(), 
            axis.text =  element_blank()) +
      scale_fill_gradient(low="white", high="blue") +
      guides(fill = guide_colorbar(barwidth = 10, barheight = .5)) + 
      ggtitle(paste("Value in Year",i))
    print(m)


  } 

}, interval = 1, movie.name = "value_usa.gif", ani.width = 800, ani.height = 600 )

运行上面的代码后,我收到错误Error in unit(x, default.units) : 'x' and 'units' must have length > 0。有许多资源被敦促使用as.factor(n),但也没有用。为了便于理解,我尽可能地减少了上面的例子。我做了一个类似的项目,工作得很好。可以找到here

1 个答案:

答案 0 :(得分:0)

在两行上更改大写:

Statecodes <- c("AL","AK","CA","AL","AK","CA")

...

m<-ggplot(subset(data,year==i), aes(map_id = Statecodes)) +