[R] Overlay boxplot and scatter.smooth line
jlh.membership at gmail.com
Wed Nov 20 17:39:17 CET 2013
Same thing using ggplot...
# your data
x <- c(rep(1,100),rep(2,100),rep(3,100))
y <- c(rnorm(100,1),rnorm(100,2),rnorm(100,3))
df <- data.frame(x=x, y=y)
ggp <- ggplot(df) + labs(x="X", y="Y")
ggp <- ggp + geom_boxplot(aes(x=factor(x), y=y))
ggp <- ggp + geom_smooth(formula=y~x, aes(x=x, y=y), method="loess", se=F)
Setting method="lm" above would plot linear model instead of loess, setting se=T (the default) would plot 95% CL for the model.
Adding the line:
ggp <- ggp + stat_summary(fun.data="mean_cl_normal", aes(x=factor(x),y=y), size=1.2, color="red")
will overlay mean and 95% CL (assuming normal distribution of error) at each level of x.
From: Johannes Radinger [mailto:johannesradinger at gmail.com]
Sent: Tuesday, November 19, 2013 10:01 AM
To: Adams, Jean
Cc: R help
Subject: Re: [R] Overlay boxplot and scatter.smooth line
Thanks...that works great!
On Tue, Nov 19, 2013 at 2:39 PM, Adams, Jean <jvadams at usgs.gov> wrote:
> Use lines() with loess.smooth() instead of scatter.smooth() to add to
> the already existing boxplot. For example,
> lines(loess.smooth(x, y))
> On Tue, Nov 19, 2013 at 4:43 AM, Johannes Radinger <
> johannesradinger at gmail.com> wrote:
>> I'd like to plot a boxplot and use a scatter.smooth line plot as an
>> overlay for this plot.
>> This works except for the problem that the x-axis ticks for both
>> plots are differently spaced:
>> The boxplot ticks are closer and the space between the outer most
>> boxplots and the plot region (box) is larger for boxplots than for
>> the scatter.smooth plot. Is there any plot-option that can be changed
>> to produce the desired plot.
>> Here an example to illustrate what problem I am facing:
>> x <- c(rep(1,100),rep(2,100),rep(3,100))
>> y <- c(rnorm(100,1),rnorm(100,2),rnorm(100,3))
>> scatter.smooth(x, y, xaxt="n", yaxt="n", ann=FALSE)
>> Moreover, I'd like to plot just the smoothed line only and not the
>> Any suggestions?
>> best regards,
>> [[alternative HTML version deleted]]
>> R-help at r-project.org mailing list
>> PLEASE do read the posting guide
>> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help