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
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!
sw <- “Sepal.Width” ggplot(iris, aes_string(“Sepal.Length”, sw, colour=“Species”) ) + geom_point()
h<- ggplot(iris, aes(Sepal.Length/Sepal.Width, fill=Species )) +geom_histogram(binwidth=0.1) h
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
?theme p
p + theme_bw()
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()
)
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()
)
h + theme_for_nature
library(ggthemes) h + theme_fivethirtyeight()
getwd() # “C:/Users/mcnach/Dropbox/_ggplots"
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)
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
h+ scale_fill_manual(values = c(“magenta”,“cyan”, “darkblue”))
library(ggsci) h + scale_fill_npg() # check details
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)
h + facet_wrap(~Species, ncol=1)
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
b + geom_jitter() ?geom_jitter b + geom_jitter(width=0.1)
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)
head(iris.long) #Species variable value #1 setosa Sepal.Length 5.1 #2 setosa Sepal.Length 4.9 #3 setosa Sepal.Length 4.7
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
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
p2<- p+ geom_point(aes(pl=Petal.Length, pw= Petal.Width))
ggplotly(p2, tooltip=c(“Species”, “Petal.Length”, “pw”) ) # now displays petal data rather than sepal data
library(ggExtra) ggMarginal(p)
ggMarginal(p + theme_fivethirtyeight(), type=“histogram”, binwidth=0.1)
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) )
sl=“Sepal.Length” sw=“Sepal.Width” ggplot(iris, aes_string(x=sl,y=sw)) #check, incomplete