[R] changing graphs in qqplot2

Ista Zahn
Mon, 27 Jun 2011 04:00:40 -0000

Hi

On Sun, Jun 26, 2011 at 4:21 PM, Sigrid <s.stenerud at gmail.com>
wrote:
> This is what I have now so far.
>
> p=ggplot(data = test, aes(x = YEAR, y = TOTAL, colour = TREATMENT)) +
> geom_point() + geom_smooth(method = "lm", se=FALSE) +
facet_wrap(~COUNTRY)
>> p +scale_x_continuous(limits=c(1,4))
>
> http://r.789695.n4.nabble.com/file/n3626510/graph.gif
>
> I would also like to:
>
>
> 1.) change the headline for the faced wraps ?high? and ?low?. Is there any
> other way of doing that other than changing the names in the dataset?

Don't think so. Changing the "names" is easy if you have stored
the
variable as a factor -- just change the levels.

>
>
> 2.) show the slope and intercept next to the treatments labels on the right
> side. The geom_text only works inside the graph. I know I can change the
> names of the treatments in the data set to include the intercept and slope,
> but that seems like a detour for what I want to achieve.

See the breaks and labels argument in ?scale_hue. As far as I know
you'll have to calculate the values outside of ggplot though.

>
>
> 3.) avoid the decimals on the x-axis. I would prefer the ticks to be
> (1,2,3,4). How is that done?

See the breaks and labels arguments to
?scale_x_continuous
>
>
> 4.) change the lines. The look of the graph is exactly what I am after
> (except those minor changes mentioned above). ?However, the slopes and
> intercepts are not the same as what I got when I completed an ancova model.
>
>> ?coef(lm(formula = TOTAL~ TREATMENT + YEAR + COUNTRY + TREATMENT:YEAR,
>> data= test))
> ? ?.... r gives the values for the different intercepts and slopes...

Well, those models are not the same. ggplot is calculating separate
lines for high and low COUNTRY, you are merely controling for it. It's
not clear to me what you actually want to do.

>
> a. I?ve tried adding an abline that specifies slope and intercept. But then
> I would have to specify which of the facet wraps that the line belongs to,
> as it turns up in both grids. Is that possible in qqplot2?

Yes, you can put the intercept and slope values in a separate
data.frame. For example,

d2 <- data.frame(vs = c(0, 1), a = c(11, 11), b = -0.25, -0.25)
ggplot(mtcars, aes(mpg, cyl)) + geom_point()  +
geom_abline(aes(intercept = a, slope = b), data = d2) +
facet_wrap(~vs)

>
>> p=ggplot(data = test, aes(x = YEAR, y = TOTAL, colour = TREATMENT)) +
>> geom_point() + facet_wrap(~NITROGEN)
>
> #this is the treatment A, but should only be displayed in the right graph
> (low).
> p + geom_abline(intercept = 81.476,slope=47.2667, colour = "red",
size = 1)
>
> http://r.789695.n4.nabble.com/file/n3626510/graph2.gif
>
> b. Another option I?ve tried is to specify the model in the stat_smooth and
> geom_smooth commandsm=, but in ?both cases I get an error message.
>
>> p + stat_smooth(method = "lm", formula = TOTAL ~ TREATMENT +
YEAR +
>> COUNTRY + TREATMENT:YEAR)+
> + geom_point()
> Error in model.frame.default(formula = formula, data = data, weights >
weight, ?:
> ?variable lengths differ (found for 'COUNTRY')

I think you can only specify transformations to x and y in the formula
argument. At least I've never got anything more complicated to work.
So I recommend either using geom_abline or going back and thinking
about what exactly you want geom_smooth to do (see my comment on part
4 above).

>
> Thank you for all help and creative solutions.

Hope it helps,
Ista
>
> Sigrid
>
> (I am re-posting as I've gotten more information on what I wanted to
know
> under my last post, but my new problems are not reflected by the subject
and
> I feel like some people would be missing out a chance of helping me out. I
> hope it is okay.)
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/changing-graphs-in-qqplot2-tp3626510p3626510.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology