# Example 1
# make a very simple plot

x <- c(1,3,6,9,12)
y <- c(1.5,2,7,8,15)

plot(x,y)



# Example 2. Draw a plot, set a bunch of parameters.
plot(x,y, xlab="diameter (cm)", ylab="height (m)", main="Tree height versus diameter", ylim=c(0,20), xlim=c(0,20), pch=21, col="blue") 

# fit a line to the points
linea1 <- lm(y ~ x) 

# get information about the fit
summary(linea1) 

# draw the fit line on the plot
abline(linea1, lty=1, col="Blue") 



# Example 3
# add some more points to the graph
x2 <- c(0.5, 3, 5, 8, 12)
y2 <- c(0.8, 1, 2, 4, 6)

points(x2, y2, pch=22, lty=2, col="green")

# fit a line to the points
linea2 <- lm(y2 ~ x2) 

# get information about the fit
summary(linea2) 

# draw the fit line on the plot
abline(linea2,col="Green", lty=2) 

# Create a legend
legend(0, 20, c("Black River","Tussock Land"), 
   col=c("blue","green"), pch=21:22, lty=1:2)

#Example 4. Histogram

# Generate 1000 random numbers with a normal distribution
x<-rnorm(1000, 600,100)
x

# Histogram of x
hist(x)

# Some more options
hist(x, col="lightblue", ylim=c(0,400), 
	xlab="Trees per hectare", ylab="Number of Trees",
	main="Histogram of trees per hectare",
	breaks=c(100,200,300,400,500,600,700,800,900,1000,1100))



#Example 5. Plots after reading files. Let's read some data: R_data_bown_2009.xls
# / open excel / edit / copy

bown<-read.delim("clipboard")
names(bown)

library(car)
scatterplot(h12~d12 | treat, data=bown, 
	boxplots=FALSE, smooth=FALSE, reg.line=FALSE,
	xlab="Diameter (mm)", ylab="Tree height (cm)")


#Example 6. Pie chart based on summary statistics

library(doBy)
summaryBy(ANPPGPP+APRGPP+TBCAGPP~treat, data=bown, FUN=c(mean, sd, length))

# This is the database you have to read

comp	N0P0	N0P1	N1P0	N1P1
ANPP	0.25	0.28	0.34	0.35
APR	0.30	0.32	0.31	0.33
TBCF	0.45	0.39	0.35	0.32

# Now comes the reading

gpp_part<-read.delim("clipboard")
gpp_part
names(gpp_part)

# Now our first pie graph
pie(N0P0)

# Now some nice enhancements for our pie plot

pie(N0P0, labels=comp, main="Tree C partitioning", col=rainbow(length(N0P0)))


#Example 7 Several Pies in the same graph

par(mfrow=c(2,2))
pie(N0P0, labels=comp, main="Treatment N0P0", col=rainbow(length(N0P0)))
pie(N0P1, labels=comp, main="Treatment N0P1", col=rainbow(length(N0P1)))
pie(N1P0, labels=comp, main="Treatment N1P0", col=rainbow(length(N1P0)))
pie(N1P1, labels=comp, main="Treatment N1P1", col=rainbow(length(N1P1)))


#Example 8 Scatterplot matrix

attach(bown)
pairs(~APR+TBCA+GPP)

coplot(TBCA~treat | Clone, data=bown)

#Example 9 Barplots

barplot(N0P0, col="Green", main ="C partitioning", ylab="C fraction (%)", xlab="Component", names.arg=c("ANPP", "APR", "TBCF"))








