0

I am fairly new to R and I am working on analyzing some data in ggplot2.

I have one set of data that has hormone values for a type of animal. The animals came from two sites (Control, New). I analyzed the data using an ANCOVA and plotted the predicted regression lines based on the model. What I would really like to do, is plot dotted confidence interval lines around both lines on my graph. I can't seem to find/figure out how to perform this using the ggplot2 package. I moved to the package initially because my sample sizes are unequal. Any help would be appreciated.

Here is the code I have used thus far:

Data <- read.csv("Data.csv")
library(ggplot2) # package for plotting
library(outliers) # package for rntransform
aov.con <- aov(rntransform(Hormone) ~ Length + Status, data=Data) ### ANCOVA 

pred <- predict(aov.con)

ggplot(Data, aes(Length, Hormone, color=Status)) + 
geom_point() + 
geom_line(aes(y=pred)) + 
theme_bw() + 
theme(panel.border = element_rect(colour = "black", fill=NA, size=1),        
panel.grid.major = element_blank(), 
panel.grid.minor = element_blank(), axis.line = element_line(colour =   
"black"))
DHaskins
  • 1
  • 2
  • 2
    New to R ... and apparently to SO as well. Please make edits to this questions using the SO formatting conventions – IRTFM Nov 24 '15 at 04:13
  • 1
    Please provide the package where `rntransform` is from. I don't understand how unequal sample sizes affects your choice of plotting package. Your example is not [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Axeman Nov 24 '15 at 07:34
  • 2
    As Axeman said, hard to give a real answer without a reproducible example. But I think you need to provide a `confidence`-argument to `predict`, plotting lines is then trivial using group and geom_line or geom_area. – Heroka Nov 24 '15 at 08:03
  • 1
    The general approach is to calculate your confidence intervals outside of ggplot (i.e. what you did with `predict()`). Use ggplot to plot your y-values with `geom_ribbon(aes(ymax= y + ci, ymin = y - ci)` (or `geom_rect()`) drawing your confidence bands. This [example](http://stackoverflow.com/q/14033551/4718512) shows one way to do it. Also, in your example, it looks like you want to reference two separate dataframes: Data and pred, but `geom_line(aes(y=pred))` is looking for column "pred" in dataframe "Data." You would need to use `geom_line(data= pred, aes(y= yvalue))` instead. – oshun Nov 24 '15 at 20:29

0 Answers0