[R] bar and line plot

Jim Lemon jim at bitwrit.com.au
Sat Sep 27 13:48:58 CEST 2008


Felipe Carrillo wrote:
>  Hello All:
>     Using the below dataset how can I make a barplot with
>     Date(X) and NumEggs(Y) by Site. Then plot Temp(lineplot)
>     It seems really simple, but I am having a hard time trying to
>     do it by Site. Thanks
>   
Hi Felipe,
This might do what you want:

fdc<-read.table("fdc.dat",header=TRUE)
fdc$Date<-as.Date(fdc$Date,format="%Y-%m-%d")
library(plotrix)
par(mar=c(5,4,4,4))
barp(fdc$NumEggs,names.arg=fdc$Date,xlab="Date",ylab="Number of eggs",
 main="Number of eggs by date",col=fdc$Site)
# a low trick to align the x-axis for the temperature line
startDate<-as.Date("2008-04-21",format="%Y-%m-%d")
lines(fdc$Date-startDate,fdc$Temp/5)
axis(4,at=10:12,labels=c(50,55,60))
mtext("Temperature",side=4,at=5,line=1)
legend(2,14,levels(fdc$Site),fill=1:3)

Jim



More information about the R-help mailing list