STAT545-UBC / Discussion

Public discussion
38 stars 20 forks source link

Further troubles with faceting in a function #467

Open Adam-Ziada opened 7 years ago

Adam-Ziada commented 7 years ago

Alright, so there is still an issue.

facet2D <- function(data, c1, c2, f1, f2) {
    ggplot(data = data, aes(x = c1, y = c2)) +
        geom_point(alpha = 1) +
        facet_wrap(c(paste(f1), paste(f2))) 
}

facet2D(data = my_cars, my_cars$wt, my_cars$mpg, "am", "cyl")

image

gives a distinctly different graph than

ggplot2::ggplot(my_cars, ggplot2::aes(wt, mpg)) +
  ggplot2::geom_point(alpha = 1) +
  ggplot2::facet_wrap(c(paste("am"), paste("cyl")))

image

The issue seems to be with facet_wrap again, as if you remove it both the graphs look identical, but when you include it thing seem to go wrong. I use the following definition for my_cars.

my_cars <- dplyr::mutate(mtcars, cyl = as.factor(mtcars$cyl),
                         gear = as.factor(mtcars$gear),
                         carb = as.factor(mtcars$carb),
                         am = as.factor(mtcars$am),
                         vs = as.factor(mtcars$vs))
samhinshaw commented 7 years ago

See #465