ggplot2
ggplot2 copied to clipboard
ggplot2 can't determine that Haven labelled integers are discrete
Hi! I'm trying to plot some data from a Haven labelled dataset using ggplot2. I'm grouping by "sex", a labelled int column. However, I get the following:
ggplot() + geom_line(data=dec_by_age_gender, mapping=aes(x=age_5yr,y=avg_dec,group=sex)) + scale_color_manual(values=c("red","green"))
Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/integer. Defaulting to continuous.
> ggplot() + geom_line(data=dec_by_age_gender, mapping=aes(x=age_5yr,y=avg_dec,group=sex)) + scale_color_manual(values=c("red","green"))
Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/integer. Defaulting to continuous.
> head(dec_by_age_gender)
# A tibble: 6 × 3
age_5yr sex avg_dec
<dbl> <int+lbl> <dbl>
1 20 1 [Male] 0.00357
2 20 2 [Female] 0.00320
3 25 1 [Male] 0.00421
4 25 2 [Female] 0.00406
5 30 1 [Male] 0.00473
6 30 2 [Female] 0.00453
> class(dec_by_age_gender)
[1] "tbl_df" "tbl" "data.frame"
> class(dec_by_age_gender$sex)
[1] "haven_labelled" "vctrs_vctr" "integer"
>
Since "sex" has the class "integer", shouldn't ggplot2 know to pick a discrete color scale for it?
ggplot2 handles the basic integer vector like R in general does: as a numeric... It is also not clear to me why you would expect an integer to be mapped to a discrete colour scale...
All that being said, your initial question was why a default scale was not able to be picked - My guess is that the vctrs class doesn't dispatch to numeric like standard integers does
Minimal reprex:
library(ggplot2)
scale_type(haven::labelled(1:2))
#> Don't know how to automatically pick scale for object of type haven_labelled/vctrs_vctr/integer. Defaulting to continuous.
#> [1] "continuous"
Created on 2022-03-15 by the reprex package (v2.0.1)
I suspect we just need a scale_type.integer()
here because of the way .numeric
works (which IIRC only applies to un-classed base types).
@Hadley can we agree that the scale type of integers should be continuous? And that any haven specific behaviour should be taken care of separately
Agreed.