Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

geom_smooth with facet_grid and different fitting functions

First of all, apologies for the example, but I couldn't find a better data set to demonstrate the problem. Hopefully, it will suffice. Say I'm trying to make a facet grid of transmission (automatic vs. manual) and number of gears from the mtcars data set that plots mpg against displacement, like this:

# Load library
library(ggplot2)

# Load data
data(mtcars)

# Plot data
p <- ggplot(mtcars,aes(x = disp, y = mpg)) + geom_point() + facet_grid(gear ~ am)
p <- p + geom_smooth()
print(p)

which gives,

enter image description here

Notice, I've added a trend line using geom_smooth and it has defaulted to use a loess curve. I can fit user-defined functions rather than a loess curve using nls for the method and then stating a formula, which is great. But is it possible to fit different user-specified curves for each facet? For example, a linear regression to the top left panel and decaying exponential for the bottom right. Is this possible? Or am I using a hammer to drive in screws?

EDIT: A solution for custom (i.e., user-defined) fitting functions is given here.

like image 788
Lyngbakr Avatar asked Jul 04 '17 18:07

Lyngbakr


People also ask

What is the difference between Facet_wrap and Facet_grid?

While facet_grid shows the labels at the margins of the facet plot, facet_wrap creates a label for each plot panel.

What is the function of Facet_grid () in Ggplot ()?

facet_grid() forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data.

What does the se argument of Geom_smooth () do?

It then computes the confidence interval based on a set number of standard-error intervals around the predicted value (for the typical 95% CI, this is predicted ± 1.96 * se ). The Details section of geom_smooth says: Calculation is performed by the (currently undocumented) 'predictdf()' generic and its methods.

What is Geom_smooth LM?

To add a regression line on a scatter plot, the function geom_smooth() is used in combination with the argument method = lm . lm stands for linear model.


1 Answers

Following the suggestions given here, a possibile solution is:

# Load library
library(ggplot2)

# Load data
data(mtcars)

# Vector of smoothing methods for each plot panel
meths <- c("loess","lm","lm","lm","lm","lm","lm")

# Smoothing function with different behaviour in the different plot panels
mysmooth <- function(formula,data,...){
   meth <- eval(parse(text=meths[unique(data$PANEL)]))
   x <- match.call()
   x[[1]] <- meth
   eval.parent(x)
}

# Plot data
p <- ggplot(mtcars,aes(x = disp, y = mpg)) + geom_point() + facet_grid(gear ~ am)
p <- p + geom_smooth(method="mysmooth")
print(p)

enter image description here

like image 160
Marco Sandri Avatar answered Sep 29 '22 18:09

Marco Sandri