On 6/13/2011 2:13 AM,
lars.gamfeldt-ZF5OzBzzxaDCagz1f/kl...@public.gmane.org wrote:
> Hi,
>
> I have problems creating a rangeplot for my data. I must admit I am
> quite new to ggplot so my apologies for this being a very basic
> question.
>
> What I would like to accomplish is a figure with the mean value as a
> point and the 95% confidence intervals as ranges.
I am assuming you mean the 95% confidence interval of the mean; if that
isn't what you mean, then we need you to be more clear
> My data looks like this, with 3 columns:
>
> System (2 levels: terrestrial, aquatic)
> variable (2 levels: LRR1, LRR2)
> data (the response data)
It is better to give actual example (even mock) data than this. For
example:
DF <- data.frame(System = rep(c("terrestrial", "aquatic"), times=2),
variable = rep(c("LRR1", "LRR2"), each=2),
data = rnorm(400))
is a data.frame with the structure you describe (I think). In
particular, giving a data.frame means we definitely have the data
(structure) you are working with.
> So, I would like to get a figure with LRR1 and LRR2 (these are log
> response ratios) on the x-axis, data on the y-axis with points as
> means and ranges as the 95% confidence intervals for LRR1 and LRR2
> respectively, and code it by colour=System.
>
> I have indeed tried many different alternatives, and looked in the
> book, but am not very successful. Any advice would be very much
> appreciated.
It is nice to show what you have tried and how it doesn't work.
From your description, there are two basic approaches: summarize the
data you have to get the values you want to plot and then plot those, or
use ggplot's abilities to create summaries to do that step for you. The
first is more flexible; the second can be more compact when it works.
Note that smean.cl.boot is a function in the Hmisc package.
DF.summary <- ddply(DF, .(System, variable), function(x) {
data.frame(t(smean.cl.boot(x$data)))})
ggplot(DF.summary, aes(x=variable, colour=System)) +
geom_pointrange(aes(y=Mean, ymin=Lower, ymax=Upper))
In order to see the two lines separated, you have to manually dodge them
(lines get plotted over each other by default, even when dodged)
ggplot(DF.summary, aes(x=variable, colour=System)) +
geom_pointrange(aes(y=Mean, ymin=Lower, ymax=Upper),
position=position_dodge(width=0.1))
If you want to use the built summary abilities:
ggplot(DF, aes(x=variable, colour=System, y=data)) +
stat_summary(geom="pointrange", fun.data = "mean_cl_boot")
ggplot(DF, aes(x=variable, colour=System, y=data)) +
stat_summary(geom="pointrange", fun.data = "mean_cl_boot",
position=position_dodge(width=0.1))
The second one of these has the same dodging as discussed with the first
approach.
> Best wishes, Lars
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University