0

I made this function to plot some graphics, and it works well except for the if condition of the third line

my_dsaurus_function <- function(datasets){

  df1 <- filter(datasaurus_dozen, dataset == datasets)

  if(datasets == c("away", "h_lines", "bullseye")){

    ggplot(df1) +
      geom_point(aes(x, y)) +
      facet_grid(. ~ dataset)

  }else{

    ggplot(df1) +
      geom_point(aes(x, y, colour = dataset, fill = dataset))

}}

these errors always comes up:

Warning messages:
1: In dataset == datasets :
  longitud de objeto mayor no es múltiplo de la longitud de uno menor
2: In datasets == c("away", "h_lines", "bullseye") :
  longitud de objeto mayor no es múltiplo de la longitud de uno menor
3: In if (datasets == c("away", "h_lines", "bullseye")) { :
  la condición tiene longitud > 1 y sólo el primer elemento será usado

it says: the condition has length > 1 and only the first element will be used the length of the bigger object isn´t a multiple for the length of a smaller one

how do I eliminate these errors?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    use `%in%` instead of `==` – Edo Dec 08 '19 at 14:12
  • unless `datasets` is a vector of length greater than 1.. In that case even the way you use `filter` doesn't look correct – Edo Dec 08 '19 at 14:14
  • 1
    you should provide a reproducible example, so we can run your code and help you to find the best solution. Give an example of how your data should look like and how `datasets` looks like – Edo Dec 08 '19 at 14:16
  • Perhaps useful, along with @Edo's comment: https://stackoverflow.com/q/42637099 – r2evans Dec 08 '19 at 14:27

0 Answers0