In R4DS Section 3.6, the authors present the following code:
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth(data = filter(mpg, class == "subcompact"), se = FALSE)
which causes the following error
Error in class == "subcompact" :
comparison (1) is possible only for atomic and list types
I assume it worked when the authors wrote it, as they have a nice plot illustrating the results.
What is happening and how do I fix it? (R 3.3.2 on OS X) Thanks
The filter()
function comes from the dplyr
package. Be sure you've loaded it before running those lines. Otherwise, you're running a comparison with class()
, the built-in function, rather than mpg$class
.
You probably had another package with a function (filter) loaded and masking dplyr filter
quick and dirty fix:
dplyr::filter()
instead of
filter()
use library(dplyr)
library(dplyr)
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth(
data = filter(mpg, class == "subcompact"),
se = FALSE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With