Open an R markdown file here (.Rmd) - really cool, easy to send things about and generate docs

using ‘Knit’ (icon on the menu above… ask Al)

install.packages(c(“ggplot2”, “ggthemes”, “ggsci”, “reshape2”, “plotly”,“ggExtra”, “svglite”))

install.packages(“ggplot2”) install.packages(“ggthemes”) install.packages(“ggsci”) install.packages(“reshape2”) install.packages(“plotly”) install.packages(“ggExtra”) install.packages(“svglite”)

install.packages(“devtools”) library(devtools) devtools::install_github(‘baptiste/egg’)

library(ggplot2) library(ggthemes) library(ggsci) library(reshape2) library(plotly) library(ggExtra) library(egg) library(svglite)

data(iris) head(iris)

p <- ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width) ) #aes=aesthetics head(p) str(p)

p # blank plot, no geometry specified yet

p=p+geom_point() # add geometry type p

build layers sequentially, adding to previous

here we overwrite only x and y, but everything else is kept from the previous (labels stay as SEpals!!!)

ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width) ) + geom_point(aes(x=Petal.Length, y=Petal.Width)) # each layer can have their own geom, aes etc… but unless specified the previous will be inherited

p=p+geom_point(color=“red”) p

colours can be defined by factors… see Species, for example:

p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, colour=Species) ) + geom_point() p geom_point() #just like functions we get a help menu here… see inherited=TRUE by default, # but it can be set to F!

to indicate variables as parameters:

sw <- “Sepal.Width” ggplot(iris, aes_string(“Sepal.Length”, sw, colour=“Species”) ) + geom_point()

can do simple math in display parameters:

h<- ggplot(iris, aes(Sepal.Length/Sepal.Width, fill=Species )) +geom_histogram(binwidth=0.1) h

cut_width: cuts data into bins - check

Sepal.Length.Groups <- cut_width( iris$Sepal.Length, 0.5 ) b <- ggplot(data = iris) +
geom_violin( aes( x = Sepal.Length, y = Sepal.Width, group = Sepal.Length.Groups, fill = Sepal.Length.Groups ) ) + labs( fill = “Sepal length intervals”, x = “Sepal length”, y = “Sepal width” ) b

Themes! <- check manual. Themes control the whole look.

?theme p

apply a bw theme…

p + theme_bw()

or create a new theme:

p + theme( panel.background = element_blank(), panel.grid.major = element_line(colour = “darkgrey”), text = element_text(size=12), axis.title.x=element_blank(), axis.title.y=element_blank(), panel.grid.minor.y=element_blank(),
panel.grid.major.y=element_blank()
)

Note you can save your theme and reuse it

theme_for_nature <- theme( panel.background = element_blank(), panel.grid.major = element_line(colour = “darkgrey”), text = element_text(size=12), axis.title.x=element_blank(), axis.title.y=element_blank(), panel.grid.minor.y=element_blank(),
panel.grid.major.y=element_blank()
)

you can apply themes to other geoms…

h + theme_for_nature

there are repositories for themes available online

Here is one based on https://fivethirtyeight.com/ found on the package ggthemes

library(ggthemes) h + theme_fivethirtyeight()

getwd() # “C:/Users/mcnach/Dropbox/_ggplots"

saving plots… extension defines the type

save a png file

ggsave(“IrisDotplot.png”, p) #save a jpeg file ggsave(“IrisDotplot.jpg”, p) #save a pdf ggsave(“IrisDotplot.pdf”, p) #save a svg ggsave(“IrisDotplot.svg”, p)

fine colouring… check colorbrewer2.org - palettes continuous, discrete scales… all sorts of stuff

q <- ggplot(iris, aes(x=Petal.Length, y=Petal.Width, colour=Sepal.Length/Sepal.Width )) + geom_point(alpha=0.5) q

q + scale_color_distiller(palette=“Paired”)

q + scale_color_gradient2(high=“darkred”, low=“blue”, mid=“red”, midpoint=2, space=“Lab”)

?scale_color_gradient2

manual set colour to fill bars

h+ scale_fill_manual(values = c(“magenta”,“cyan”, “darkblue”))

there are also prebuilt palettes, much like themes:

library(ggsci) h + scale_fill_npg() # check details

split graphs

q q + facet_grid(~Species) # check other ‘facet’ functions…

Countries <- c(“Italy”, “Spain”, “France”, “UK”)

IrisDataWithCountries <- iris IrisDataWithCountries$Country <- sample(Countries, nrow(iris), replace = TRUE)

q <- ggplot(IrisDataWithCountries, aes(x = Petal.Length, y = Petal.Width, colour = Sepal.Length/Sepal.Width ) ) + geom_point() q + facet_grid(Country ~ Species)

splitting a histogram:

h + facet_wrap(~Species, ncol=1)

boxplot stuff…

watch x must be factor

coord_flip… does not change geometry… check reverse, still applied to the original geometry

b <- ggplot(iris, aes(x=Species, y=Sepal.Length )) + geom_boxplot() b b <- b + coord_flip() b b + scale_y_reverse()

levels(iris$Species) # order factors with ‘ordered’

iris\(Species <- ordered(iris\)Species, levels=c(“virginica”, “setosa”, “versicolor”))

b <- ggplot(iris, aes(x=Species, y=Sepal.Length )) + geom_boxplot() + coord_flip() b

add layers with different geometries, such as jitter dots

b + geom_jitter() ?geom_jitter b + geom_jitter(width=0.1)

can add layers directly… but can be messy

d <- ggplot(iris) b1 <- geom_boxplot(aes(x=Species, y=Sepal.Length, alpha=0.5)) b2 <- geom_boxplot(aes(x=Species, y=Sepal.Width, alpha=0.5)) b3 <- geom_boxplot(aes(x=Species, y=Petal.Width, alpha=0.5)) b4 <- geom_boxplot(aes(x=Species, y=Petal.Length, alpha=0.5)) d + b1 +b2 + b3 + b4

library(reshape2) iris.long <- melt(iris)

Using Species as id variables

head(iris.long) #Species variable value #1 setosa Sepal.Length 5.1 #2 setosa Sepal.Length 4.9 #3 setosa Sepal.Length 4.7

change just column 2

names(iris.long)[2] = “Flower.Part” #Check the results names(iris.long)

bigbox <- ggplot(iris.long, aes(Species, value, fill=Flower.Part)) + geom_boxplot() bigbox

plotly - INTERACTIVE!!!!!!!!!!!!!

it can be inserted into an html etc… without Shiny! stays interactive!

ggplotly(p) # it displays whatever is in the aes! # BUT it can be ‘hacked’ to display other things as long as we use variable names not yet recognised by ggplot

By default the mouse over text is what is mapped in the aesthetics. You can change this using the tooltip() function within plotly and giving some false aesthetics to enable more data to be accessible by tooltip.

p2<- p+ geom_point(aes(pl=Petal.Length, pw= Petal.Width))

returns: Warning: Ignoring unknown aesthetics: pl, pw

now you can use either the false aesthetic or its name

ggplotly(p2, tooltip=c(“Species”, “Petal.Length”, “pw”) ) # now displays petal data rather than sepal data

Plot.ly graphs can be used locally, or online either through a shiny server

(you need to use renderPlotly and plotlyOutput) or using plot.ly’s servers to get a URL

(details on their web page).

multiplot tools

library(ggExtra) ggMarginal(p)

ggMarginal(p + theme_fivethirtyeight(), type=“histogram”, binwidth=0.1)

arranging together

library(egg) ggarrange(p,q,h) ggsave(“multifigure.pdf”, ggarrange(p,q,h, ncol=2) )

ggarrange(p,q,h, ncol=2) ggsave(“multifigure2.pdf”, ggarrange(p,q,h, ncol=2) )

check ‘ggedit’ - shiny package, but can be ran in RStudio locally, may be useful for

;abelling graphs etc…

to give programmatic access to aes… aes_string: that way we can indicate a variable (in aes) rather than teh full name

sl=“Sepal.Length” sw=“Sepal.Width” ggplot(iris, aes_string(x=sl,y=sw)) #check, incomplete