Reputation: 2539
I have some difficulties understanding the following error. While using ggplot. The example below illustrate the point
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
The folliwing command works fine:
ggplot(dt,aes(Year,A)) + geom_path()
but
ggplot(dt,aes(Year,C)) + geom_path()
produces
#Error: geom_path requires the following missing aesthetics: y
I have tried with dt[!is.na(c)]
but got the same results:
Upvotes: 2
Views: 1251
Reputation: 37879
If you check the structure of your data.table you ll see that column C is shown as a list of lists:
> 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
and that's what's causing the problem. If you unlist
it, it works fine:
ggplot(dt,aes(Year,unlist(C))) + geom_path()
Warning message:
Removed 1 rows containing missing values (geom_path).
#missing values are auto removed
Upvotes: 3