Tagged with lattice

ggplot Fit Line and Lattice Fit Line in R

Let's add a fit line to a scatterplot!

Fit Line in Base Graphics

Here's how to do it in base graphics:

ols <- lm(Temp ~ Solar.R,
    data = airquality)

summary(ols)

plot(Temp ~ Solar.R,
    data = airquality)
abline(ols

Fit line in base graphics in R

Fit Line in ggplot

And here's how to do it in ggplot:

library(ggplot2)
ggplot(data = airquality,
        aes(Solar.R, Temp)) + 
    geom_point(pch = 19) + 
    geom_abline(intercept = ols$coefficients[1],
        slope = ols$coefficients[2])

You can access the info from your regression results through ols$coefficients.

Edit: Thanks to an anonymous commenter, I have learned that you can simplify this by using geom_smooth. This way you don't have to specify the intercept and slope of the fit line.

ggplot(data = airquality,
        aes(Solar.R, Temp)) + 
    geom_point(pch = 19) + 
    geom_smooth(method = lm,
        se = FALSE)

Fit line in ggplot in R

Fit Line in Lattice

In lattice, it's even easier. You don't even need to run a regression; you can just add to the type option.

library(lattice)

xyplot(Temp ~ Solar.R,
    data = airquality,
    type = c("p", "r"))

Fit Line in Lattice in R

The code is available in a gist.

References

Tagged , , , , , ,

Sorting Within Lattice Graphics in R

Default

By default, lattice sorts the observations by the axis values, starting at the bottom left.

For example,

library(lattice)
colors = c("#1B9E77", "#D95F02", "#7570B3")
dotplot(rownames(mtcars) ~ mpg,
    data = mtcars,
    col = colors[1],
    pch = 1)

produces:

Default lattice dotplot

(Note: The rownames(cars) bit is just because of how this data set is stored. For your data, you might just type the variable name (model, for example) instead.)

Graphing one variable, sorting by another

If we want to show the same data, but we want to sort by another variable (or the same variable, for that matter), we can just add reorder(yvar, sortvar). For example, to sort by the number of cylinders, we could:

dotplot(reorder(rownames(mtcars), cyl) ~ mpg,
    data = mtcars,
    col = colors[1],

Sorted by number of cylinders

Graphing two variables

To better show how this works, let's graph cyl alongside mpg, so we can see how it is sorting:

dotplot(reorder(rownames(mtcars), cyl) ~ mpg + cyl,
    data = mtcars,
    col = colors,
    pch = c(1, 0))
    pch = 1)

Graph of mpg and cyl, sorted by cyl

Reverse order

We can also sort in reverse order, by adding a "-" before the variable name:

dotplot(reorder(rownames(mtcars), -cyl) ~ mpg + cyl,
    data = mtcars,
    col = colors,
    pch = c(1, 0))

Graph of mpg and cyl, sorted by cyl, reversed

Adding a legend

We can also add a legend:

dotplot(reorder(rownames(mtcars), -cyl) ~ mpg + cyl,
    data = mtcars,
    xlab = "",
    col = colors,
    pch = c(1, 0),
    key = list(points = list(col = colors[1:2], pch = c(1, 0)),
        text = list(c("Miles per gallon", "Number of cylinders")),
        space = "bottom"))

With legend

Other lattice types

The same technique will work with other lattice graphs, such as barchart, bwplot, and stripplot.

Full code available as a gist.

Tagged , , , , , , ,