1

I am starting to work with R, this has to be a basic question but it doesn't seem obvious how to do it easily. If I have the following data set :

x y

0,1
0,2
1,2
1,4

and so on, so there are multiple y values for each x value. This how can I easily do a plot showing the data and the means and the CI intervals.

I can do it, as I have hodged together a solution off by hacking bits of code together, but there has to be a simpler solution.

This is what I am doing with this very simple data file

cardboard,r1,r2,r3,r4,r5,r6

0,233,130,110,140,160

101,293,340,313,260,366,38

and this mess of code :

er <- read.csv(file="ianevans.csv",head=TRUE,sep=",")

er[,2:7] <- min(er[1,2:7],na.rm=TRUE)/er[,2:7]*100

er$sharpness[1]=mean(as.vector(er[1,2:7], "numeric"),na.rm=TRUE)
er$sharpness[2]=mean(as.vector(er[2,2:7], "numeric"),na.rm=TRUE)

er$se[1]=sd(as.vector(er[1,2:7], "numeric"),na.rm=TRUE)/sqrt(6-1)
er$se[2]=sd(as.vector(er[2,2:7], "numeric"),na.rm=TRUE)/sqrt(6-1)

p <- ggplot(er,aes(x=cardboard,y=sharpness))
p1 <- p  + geom_point(aes(y=sharpness,color="red",size=5) )  

p2 <- p1 +
 scale_shape_discrete(solid=F) +
 geom_point(aes(y=r1),color="blue",shape="o",size=3)  +  
 geom_point(aes(y=r2),color="blue",shape="o",size=3)  +  
 geom_point(aes(y=r3),color="blue",shape="o",size=3)  +
 geom_point(aes(y=r4),color="blue",shape="o",size=3)  +
 geom_point(aes(y=r5),color="blue",shape="o",size=3)  +
 geom_point(aes(y=r6),color="blue",shape="o",size=3)  

p3 <- p2 +
 geom_errorbar(aes(ymax=sharpness+se,ymin=sharpness-se),width=5)

Again this works, more or less, but as you can see there are a bunch of hard coded numbers and there has to be a better way to both have the data file set up and do the plots and easily be able to deal with different amounts of y data for each x data for example. The way some of the data manipulation is done is also awkward as it should be possible do to it without individual references for the means, sd's, .

IRTFM
  • 258,963
  • 21
  • 364
  • 487
Cliff Stamp
  • 531
  • 5
  • 11
  • A [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your data would help. But generally, you can create functions to do some of this. `se <- function(vec) { sd(vec, na.rm=T)/sqrt(len(vec) )}` . If you get your data to a [tidy form](http://vita.had.co.nz/papers/tidy-data.pdf) it will facilitate this. – jraab Jan 27 '15 at 00:11
  • Does this help - `ggplot(er, aes(cardboard, sharpness)) + stat_summary(fun.data = mean_cl_normal, geom = "pointrange") `. Or you may want `?mean_sdl()` – user20650 Jan 27 '15 at 01:27

1 Answers1

0

Here is my attempt to simplify your code. It is based on dplyr and ggplot2:

library(dplyr)
library(ggplot2)

er <- structure(list(cardboard = c("100", "101"), r1 = c(30L, 293L), 
                     r2 = c(233L, 340L), r3 = c(130L, 313L), r4 = c(110L, 260L
                     ), r5 = c(140L, 366L), r6 = c(160L, 38L)), .Names = c("cardboard", 
                                                                           "r1", "r2", "r3", "r4", "r5", "r6"), row.names = c(NA, -2L), class = "data.frame")

d2 <- er %>% 
    melt(id.vars='cardboard',na.rm=T) %>%    # convert to tidy format
    group_by(cardboard) %>%                  # group by cardboard
    mutate(v2=min(value)/value) %>%          # calculate v2
    summarise(sharpness=mean(v2),se=sd(v2))  # calculate mean & sd
#  cardboard sharpness        se
#1       100 0.3390063 0.3273334
#2       101 0.2688070 0.3585103

ggplot(d2) + 
    aes(x=cardboard,y=sharpness,ymax=sharpness+se,ymin=sharpness-se) + 
    geom_pointrange()
Marat Talipov
  • 13,064
  • 5
  • 34
  • 53
  • Thanks, I have managed to make this a lot more sensible, appreciate it and I have moved onto a more complex plot. This is the data : https://drive.google.com/file/d/0B3dKJopPCAfCY0Y2MEhaaEdUZGs/view . I can also get most of the plots with this ggplot(er,aes(x=cardboard,y=sharpness))+geom_point(aes(color=condition,shape=condition)) . But I can't figure out how to get this data on the plot as error bars : ddply(er,c("cardboard","condition"),summarise,n=length(sharpness),mean=mean(sharpness),sd=sd(sharpness),se=sd/sqrt(n)) – Cliff Stamp Jan 31 '15 at 03:43