有关ggplot2中缺失美学的更多信息

时间:2015-03-13 11:59:37

标签: r ggplot2

我在理解以下错误时遇到一些困难。在使用ggplot时。下面的例子说明了要点

dt <- dput(setnames(a[1:6],3:6,c("A","B","C","D"))[])
structure(list(DistName = c("alpha", "alpha", "alpha", 
"beta", "beta", "beta"), Year = c(2000L, 2005L, 2010L, 
2000L, 2005L, 2010L), A = c(0.0364325563237498, 0.0401968159729988, 
0.0357395587861466, 0.0317236054181487, 0.0328213742235379, 0.0294694430578336
), B = c(0.0425655168939766, 0.0361845895261913, 0.0405488035286116, 
0.0379746835443038, 0.0305346050541756, 0.0329726164158882), 
    C = list(NA, -0.14990837263128, 0.120609741869846, NA, -0.195922066906708, 
        0.0798442081496364), D = list(NA, 0.103321315578263, 
        -0.110885827122383, NA, 0.0346041627652209, -0.10212647230659)), 
.Names = c("Name", "Year", 
"A", "B", "C", "D"), class = c("data.table", "data.frame"
), row.names = c(NA, -6L))

##     Name Year          A          B          C          D
## 1: alpha 2000 0.03643256 0.04256552         NA         NA
## 2: alpha 2005 0.04019682 0.03618459 -0.1499084  0.1033213
## 3: alpha 2010 0.03573956 0.04054880  0.1206097 -0.1108858
## 4:  beta 2000 0.03172361 0.03797468         NA         NA
## 5:  beta 2005 0.03282137 0.03053461 -0.1959221 0.03460416
## 6:  beta 2010 0.02946944 0.03297262 0.07984421 -0.1021265

folliwing命令工作正常:

ggplot(dt,aes(Year,A)) + geom_path()

ggplot(dt,aes(Year,C)) + geom_path()

产生

#Error: geom_path requires the following missing aesthetics: y

我尝试了dt[!is.na(c)],但得到了相同的结果:

1 个答案:

答案 0 :(得分:3)

如果检查data.table的结构,您会看到列C显示为列表列表:

> str(dt)
Classes ‘data.table’ and 'data.frame':  6 obs. of  6 variables:
 $ Name: chr  "alpha" "alpha" "alpha" "beta" ...
 $ Year: int  2000 2005 2010 2000 2005 2010
 $ A   : num  0.0364 0.0402 0.0357 0.0317 0.0328 ...
 $ B   : num  0.0426 0.0362 0.0405 0.038 0.0305 ...
 $ C   :List of 6
  ..$ : logi NA
  ..$ : num -0.15
  ..$ : num 0.121
  ..$ : logi NA
  ..$ : num -0.196
  ..$ : num 0.0798
 $ D   :List of 6
  ..$ : logi NA
  ..$ : num 0.103
  ..$ : num -0.111
  ..$ : logi NA
  ..$ : num 0.0346
  ..$ : num -0.102

这就是导致问题的原因。如果你unlist它,它可以正常工作:

ggplot(dt,aes(Year,unlist(C))) + geom_path()
Warning message:
Removed 1 rows containing missing values (geom_path). 
#missing values are auto removed

enter image description here