Skip to content
Prev 275113 / 398506 Next

lattice::xyplot/ggplot2: plotting weighted data frames with lmline and smooth

Thanks very much, Dennis.  See below for something I don't understand.
On 10/21/2011 12:15 PM, Dennis Murphy wrote:
This seems to work, but I don't understand *why*, since the weight 
variable is
PearsonLee$frequency, not PearsonLee$weight.

 > PearsonLee$weight
NULL

I get an error if I try to use PearsonLee$frequency as the weights= 
variable.

 > ggplot(PearsonLee, aes(x = parent, y = child)) +
+    geom_point(size = 1.5, position = position_jitter(width = 0.2)) +
+    geom_smooth(method = lm, aes(weights = PearsonLee$frequency),
+                colour = 'green', se = FALSE, size = 1.5) +
+    geom_smooth(aes(weights = PearsonLee$frequency),
+                colour = 'red', se = FALSE, size = 1.5) +
+    facet_grid(chl ~ par)
Error in eval(expr, envir, enclos) : object 'weight' not found

In the form below, it makes sense to me and does work, using 
weight=frequency in the initial aes(),
and weight= in geom_smooth:

ggplot(PearsonLee, aes(x = parent, y = child, weight=frequency)) +
    geom_point(size = 1.5, position = position_jitter(width = 0.2)) +
    geom_smooth(method = lm, aes(weight = PearsonLee$frequency),
                colour = 'green', se = FALSE, size = 1.5) +
    geom_smooth(aes(weight = PearsonLee$frequency),
                colour = 'red', se = FALSE, size = 1.5) +
    facet_grid(chl ~ par)