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

Dennis Murphy djmuser at gmail.com
Sat Oct 22 07:32:52 CEST 2011


Hi Michael:

The necessary argument to geom_smooth() is weight, not weights (my
fault, sorry), so try this instead:

ggplot(PearsonLee, aes(x = parent, y = child)) +
   geom_point(size = 1.5, position = position_jitter(width = 0.2)) +
   geom_smooth(method = lm, aes(weight = frequency,
               colour = 'Linear'), se = FALSE, size = 1.5) +
   geom_smooth(aes(weight = frequency,
               colour = 'Loess'), se = FALSE, size = 1.5) +
   facet_grid(chl ~ par) +
   scale_colour_manual(breaks = c('Linear', 'Loess'),
                       values = c('green', 'red')) +
   opts(legend.position = c(0.14, 0.885),
        legend.background = theme_rect(fill = 'white'))

Dennis

On Fri, Oct 21, 2011 at 11:57 AM, Michael Friendly <friendly at yorku.ca> wrote:
> Thanks very much, Dennis.  See below for something I don't understand.
>
> On 10/21/2011 12:15 PM, Dennis Murphy wrote:
>>
>> Hi Michael:
>>
>> Here's one way to get it from ggplot2. To avoid possible overplotting,
>> I jittered the points horizontally by ą 0.2. I also reduced the point
>> size from the default 2 and increased the line thickness to 1.5 for
>> both fitted curves. In ggplot2, the term faceting is synonymous with
>> conditioning (by groups).
>>
>> library('HistData')
>> library('ggplot2')
>> 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$weight),
>>                colour = 'green', se = FALSE, size = 1.5) +
>>    geom_smooth(aes(weights = PearsonLee$weight),
>>                colour = 'red', se = FALSE, size = 1.5) +
>>    facet_grid(chl ~ par)
>
> 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)
>
>
>> # If you prefer a legend, here's one take, pulling the legend inside
>> # to the upper left corner. This requires a bit more 'trickery', but
>> # the tricks are found in the ggplot2 book.
>>
>> 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$weight,
>>                colour = 'Linear'), se = FALSE, size = 1.5) +
>>    geom_smooth(aes(weights = PearsonLee$weight,
>>                colour = 'Loess'), se = FALSE, size = 1.5) +
>>    facet_grid(chl ~ par) +
>>    scale_colour_manual(breaks = c('Linear', 'Loess'),
>>                        values = c('green', 'red')) +
>>    opts(legend.position = c(0.14, 0.885),
>>         legend.background = theme_rect(fill = 'white'))
>>
>>
>> HTH,
>> Dennis
>
>
> --
> Michael Friendly     Email: friendly AT yorku DOT ca
> Professor, Psychology Dept.
> York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
> 4700 Keele Street    Web:   http://www.datavis.ca
> Toronto, ONT  M3J 1P3 CANADA
>
>
>



More information about the R-help mailing list