# ggplot workshop #install.packages("ggplot2") #install.packages("dplyr") # libraries ---- library(ggplot2) library(dplyr) # load data ----- my.data <- read.csv(file.choose(), header = TRUE, comment.char = "") # Add a column for the seasons my.data$Season[my.data$Month %in% c(12, 1, 2)] <- "Winter" my.data$Season[my.data$Month %in% c(3, 4, 5)] <- "Spring" my.data$Season[my.data$Month %in% c(6, 7, 8)] <- "Summer" my.data$Season[my.data$Month %in% c(9, 10, 11)] <- "Autumn" # Add a column as numerator my.data$Num <- seq(from=1,to=length(my.data$Minute),by=1) # Introduction ----- ## init ggplot ## ggplot(my.data,aes(x=Month, y=Ta_1.5)) ## ggplot(my.data, aes(x = Month, y = Ta_1.5)) + geom_point() ## ggplot(my.data, aes(x = Month, y = Ta_1.5))+ geom_point()+ geom_smooth() ## ggplot(my.data, aes(x = Month, y = Ta_1.5))+geom_smooth() # histogram---- ## Base graphics histogram hist(my.data$Ta_1.5) ## ggplot histogram hist1 <- ggplot(my.data, aes(x = Ta_1.5)) + geom_histogram() plot(hist1) ### ggplot histogram / define binwidth hist2 <- ggplot(data = my.data, mapping = aes(x = Ta_1.5)) + geom_histogram(binwidth = 2) # the width of the bar plot(hist2) hist3 <- ggplot(data = my.data, mapping = aes(x = Ta_1.5)) + geom_histogram(binwidth = 5) plot(hist3) hist4 <- ggplot(data = my.data, mapping = aes(x = Ta_1.5, fill=Season)) + geom_histogram(binwidth = 2) plot(hist4) # Point graphs---- ## base graphs plot(Ta_1.5 ~ Month, data = subset(my.data, Minute == 10 & Day == 1 & Hour == 14)) points( Ta_1.5 ~ Month, col = "red", data = subset(my.data, Minute == 10 & Day == 15 & Hour == 20) ) ## ggplot ggplot( subset(my.data, Hour %in% c("14", "20") & Minute == 10 & Day == 1), aes(x = Month, y = Ta_1.5, color = factor(Hour)) ) + geom_point() # Boxplots---- box1 <- ggplot(data = my.data, mapping = aes(x = Season, y = Ta_1.5)) + geom_boxplot(aes(fill = Season)) plot(box1) summer.data <- subset(my.data, Month == 6 | Month == 7 | Month == 8) box2 <- ggplot(data = summer.data, mapping = aes(x = factor(Month), y = Ta_1.5)) + geom_boxplot(aes(fill = factor(Month))) plot(box2) # Line graphs---- ## line1 <- ggplot(data = my.data, mapping = aes(x = Num, y = Ta_1.5)) + geom_line(colour="red") plot(line1) ## line2 <- ggplot(data=my.data,mapping = aes(x=Num, y=Ta_1.5,col=Season))+ geom_line() plot(line2) ## line3 <- ggplot(my.data, aes(Num)) + geom_line(aes(y = Ta_1.5, colour = "Air Temp_1.5")) + geom_line(aes(y = RH_1.5, colour = "RH_1.5")) plot(line3) ## line4 <- line3 + scale_color_discrete(name = "Parameter", labels = c("Air Temperature 1.5m", "RH 1.5m")) plot(line4) # Density plots----- ## dens1 <- ggplot(data = my.data, mapping = aes(x = Ta_1.5, fill = Season)) + geom_density() plot(dens1) ## dens2 <- ggplot(data = my.data, mapping = aes(x = Ta_1.5, fill = Season)) + geom_density(alpha=0.5) plot(dens2) # The labels---- ## dens3 <- ggplot(data = my.data, mapping = aes(x = Ta_1.5, fill = Season)) + geom_density(alpha = 0.5) + labs(title = "Density Plot", x = "Temperature at 1.5m", y = "Probability Density") plot(dens3) # Theme----- dens4 <- dens3 + theme( plot.title = element_text(size = 30, face = "bold"), axis.text.x = element_text(size = 15), axis.text.y = element_text(size = 15), axis.title.x = element_text(size = 25), axis.title.y = element_text(size = 25) ) plot(dens4) # Facets----- ## f_dens1 <- dens3+facet_wrap(~Season) plot(f_dens1) ## f_dens2 <- dens3+facet_wrap(~Season,ncol=4) plot(f_dens2) ## f_dens3 <- dens3+facet_wrap(Minute~Season) plot(f_dens3) ## f_dens4 <- dens3+facet_wrap(Minute~Season,ncol = 4) plot(f_dens4) # graphics devices------ windows()# graphics device hist6 <- ggplot(data = my.data, mapping = aes(x = Ta_1.5, fill = Season)) + geom_histogram(binwidth = 2, alpha = 0.5) # transparency of the the graph plot(hist6) ####links----- # http://tutorials.iq.harvard.edu/R/Rgraphics/Rgraphics.html#geometric_objects_and_aesthetics