# 1 install.packages("RColorBrewer") library(RColorBrewer) display.brewer.all(n=NULL, type="all", select=NULL, exact.n=TRUE, colorblindFriendly=FALSE)# Displaying the Color Brewer scales. # colorblindFriendly set to T selects only those easy to read by color-blind persons # n affects the number of colors in each scale to display. Try setting some numbers in n. #2 library(readxl) library(ggplot2) lettuce<-read_excel("lettuce.xlsx") # Importing the data summary(lettuce) # Barplots with leaf color on x-axis and color-coded taste classes ggplot(lettuce, aes(x=leaf.col, fill=as.character(taste)))+ # Definition of aesthetics; note the missing y-axis geom_bar()+ # Barplot plotting - stacked barplot is the default here theme_classic()+ #Setting the classic theme scale_fill_brewer(palette="RdYlBu", name="Taste class")+ # Setting the Brewer palette (and legend title) # This one is a diverging palette labs(x="Leaf color", y="Frequency")#Defining the axis labels ggplot(lettuce, aes(x=leaf.col, fill=as.character(taste)))+ geom_bar(position="dodge")+# Dodged barplot theme_classic()+ scale_fill_brewer(palette="RdYlBu", name="Taste class")+ # Setting the Brewer palette (and legend title) labs(x="Leaf color", y="Frequency")#Defining the axis labels # Barplots with taste classes on x-axis and color-coded leaf color ggplot(lettuce, aes(fill=leaf.col, x=as.character(taste)))+ geom_bar()+ # Stacked barplot theme_classic()+ scale_fill_manual(values=c("darkgreen", "red"), name="Leaf color")+#Manual setting of the colors # is efficient for just two colors labs(x="Taste class", y="Frequency")#Defining the axis labels ggplot(lettuce, aes(fill=leaf.col, x=as.character(taste)))+ geom_bar(position="dodge")+ # Dodged barplot theme_classic()+ scale_fill_manual(values=c("darkgreen", "red"), name="Leaf color")+ labs(x="Taste class", y="Frequency")#Defining the axis labels # 3 ggplot(beer, aes(x=subtype, y=ABV))+ stat_summary(fun.data=mean_se, geom="pointrange")+#computation an plotting the mean +- SE intervals theme_classic()+ labs(x="Beer subtype", y="Alcohol by volume (%)")#Defining the axis labels ggplot(beer, aes(x=subtype, y=IBU))+ stat_summary(fun.data=mean_se, geom="pointrange")+#computation an plotting the mean +- SE intervals theme_classic()+ labs(x="Beer subtype", y="IBU")#Defining the axis labels # Arrangement of the two plots in a single two-panel plot library(ggpubr) # this package contains the ggarrange function p1<-ggplot(beer, aes(x=subtype, y=ABV))+ # saving the plot in the p1 object stat_summary(fun.data=mean_se, geom="pointrange")+ theme_classic()+ labs(x=NULL, y="Alcohol by volume (%)")#Defining the axis labels. Note, the skipped x-axis label p2<-ggplot(beer, aes(x=subtype, y=IBU))+ # saving the plot in the p2 object stat_summary(fun.data=mean_se, geom="pointrange")+ theme_classic()+ labs(x="Beer subtype", y="IBU")#Defining the axis labels # 4 ggarrange(p1, p2, ncol=1, align='hv') # Combining the plots together as panels # the ncol/nrow parameter defines how the plots are arranged # the align='hv' ensures that the plot areas are aligned and of the same size despite # differences in x-axis label presence. Try plotting the same plot without the align paramter to see the difference # Note the difference between # facetted plots (= multipanel plots with the SAME variables on x and y axes and facets fedined by a factor) # and # arrangament of two (generic) plots of even different types. # 5 p1<-ggplot(beer, aes(x=subtype, y=ABV))+ # saving the plot in the p1 object stat_summary(fun.data=mean_se, geom="pointrange")+ theme_classic()+ labs(x=NULL, y="Alcohol by volume (%)")#Defining the axis labels. Note, the skipped x-axis label p2<-ggplot(beer, aes(x=subtype, y=ABV))+ # saving the plot in the p2 object geom_boxplot(fill="gold")+ theme_classic()+ labs(x="Beer subtype", y="IBU")#Defining the axis labels ggarrange(p1, p2, ncol=1, align='hv') # Combining the plots together as panels # 6 neobiota<-read_excel("neobiota-090-097-s002.xlsx", sheet=2, range="A3:H135", na="NA") # a ggplot(neobiota, aes(x=parasite, y=host_n))+ geom_boxplot(fill="grey")+ facet_wrap(~host)+ theme_bw()+ labs(x="Hemiparasite treatment", y="Number of host shoots") ggplot(neobiota, aes(x=parasite, y=host_n))+ geom_boxplot(fill="grey")+ facet_wrap(~host)+ scale_y_log10()+# log10-scaling of the y axis theme_bw()+ labs(x="Hemiparasite treatment", y="Number of host shoots") # b ggplot(neobiota[neobiota$parasite!="Control",], aes(x=host_g, y=parasite_g, shape=host))+ geom_point()+ facet_wrap(~parasite)+ # scale_y_log10()+ theme_bw()+ labs(x="Host biomass (g)", y="Hemiparasite biomass (g)")+ scale_shape_manual(values=c(1, 16), name="Host species")# The shapes are handled similarly to colors. # numeric coding of the shapes can be obtained in the help for the base R function points, i.e. ?points # Go to the 'pch' values section in the help.