[R] mean on a plot
Joshua Wiley
jwiley.psych at gmail.com
Sun Nov 7 21:51:18 CET 2010
Hi Romzero,
This gets the job done, though it is not the most elegant solution
ever (for those I'd take a look at the ggplot2 or lattice packages).
## Read in data
la <- read.table(textConnection("
Days Weight
0 178.00
0 250.00
0 242.00
0 239.00
0 223.00
0 188.00
0 237.00
0 212.00
0 273.00
0 191.00
0 173.00
0 233.00
0 227.00
0 253.00
0 232.00
4 177.00
4 249.00
4 241.00
4 238.00
4 222.00
4 188.00
4 236.00
4 211.00
4 272.00
4 190.00
4 172.00
4 232.00
4 226.00
4 252.00
4 231.00"), header = TRUE)
closeAllConnections()
## calculate mean by day
tmp <- c(with(la, by(Weight, Days, mean)))
## your plot
plot(Weight ~ Days, data = la)
## add in the daily means
points(x = as.numeric(names(tmp)), y = tmp, col = "blue", cex = 2, pch = 16)
Cheers,
Josh
On Sun, Nov 7, 2010 at 10:53 AM, romzero <romzero at yahoo.it> wrote:
>
> Hi, i need to draw a plot with means of values, reading a table from
> datafile.
>
> Example of datafile:
>
> Days Weight
> 0 178.00
> 0 250.00
> 0 242.00
> 0 239.00
> 0 223.00
> 0 188.00
> 0 237.00
> 0 212.00
> 0 273.00
> 0 191.00
> 0 173.00
> 0 233.00
> 0 227.00
> 0 253.00
> 0 232.00
> 4 177.00
> 4 249.00
> 4 241.00
> 4 238.00
> 4 222.00
> 4 188.00
> 4 236.00
> 4 211.00
> 4 272.00
> 4 190.00
> 4 172.00
> 4 232.00
> 4 226.00
> 4 252.00
> 4 231.00
>
> if i use "plot(Weight~Days, data=la)" the result is this
> http://r.789695.n4.nabble.com/file/n3031068/weightm.jpg
>
> how can i draw a plot with means for each days?
>
> tnx for help :)
>
> --
> View this message in context: http://r.789695.n4.nabble.com/mean-on-a-plot-tp3031068p3031068.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.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
More information about the R-help
mailing list