lundi 26 février 2018

Plotting zero values

I want create a bivariate map plotting two variables: production and possession. In order to give part of the data the correct colour I want to add a column with color codes "A", "B", "C" for one variable and for the other 1, 2, 3. Then later concatinating the two. Just so that the data is coded like the following example:

enter image description here

Here's my example df and failing code:

library(dplyr)

example_df <- structure(list(production = c(0.74, 1.34, 2.5), possession = c(5, 
23.8, 124.89)), .Names = c("production", "possession"), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"))

example_df %>%
  mutate(colour_class_nr = case_when(.$production %in% 0.628:0.608 ~ "1",
                                     .$production %in% 0.609:1.502 ~ "2",
                                     .$production %in% 1.503:3.061 ~ "3",
                                     TRUE ~ "none"),
         colour_class_letter = case_when(.$possession %in% 0.276:9.6 ~ "A",
                                         .$possession %in% 9.7:52 ~ "B",
                                         .$possession %in% 52.1:155.3 ~ "C",
                                         TRUE ~ "none"))

With these results...:

# A tibble: 3 x 4
  production possession colour_class_nr colour_class_letter
       <dbl>      <dbl> <chr>           <chr>              
1      0.740       5.00 4               none               
2      1.34       23.8  4               none               
3      2.50      125    4               none  

But this IS the desired output:

# A tibble: 3 x 4
  production possession colour_class_nr colour_class_letter
       <dbl>      <dbl>           <dbl> <chr>              
1      0.740       5.00 2                A               
2      1.34       23.8  2                B               
3      2.50      125    3                C 

I'm new with case_when() incombination with mutate, hope someone can help.

Aucun commentaire:

Enregistrer un commentaire