- GGPlot2 Overview
- GGPlot2 Layers and Examples
- Colors in GGPlot2
- More Examples
- More Information
Spring 2020
The ggplot2 library:
Elements you choose to visualize may be set explicitly, or may be mapped to a variable using aes()
Additional elements you choose to visualize may be set explicitly, or may be mapped to a variable using aes()
library(ggplot2)
rd = data.frame(
Student = c("Bob", "Sue", "Cat", "Lin"),
NumberGrade = c(96, 82, 97, 74),
LetterGrade = factor(c("A","B","A","C")) )
p = ggplot(rd, aes(y=NumberGrade))
ggplot2 interprets plot elements using geom objects:
There are other geomtetry objects: http://docs.ggplot2.org/current/
Aside from geom objects, there are other kinds of layers:
library(ggplot2)
rd = data.frame(Student = c("Bob", "Sue", "Cat", "Lin"),
NumberGrade = c(96, 82, 97, 74),
LetterGrade = factor(c("A","B","A","C")) )
ggplot(rd, aes(x=Student,y=NumberGrade)) + # Build the plot object
geom_point(size=5) + # Encode visually using points
xlab("Student Name") + # Label the X axis
ylab("Numeric Grade") + # Label the Y axis
ggtitle("Course Grade Results") # Give the plot a title
library(ggplot2)
rd = data.frame(Student = c("Bob", "Sue", "Cat", "Lin"),
NumberGrade = c(96, 82, 97, 74),
LetterGrade = factor(c("A","B","A","C")) )
ggplot(rd, aes(x=Student,y=NumberGrade)) +
geom_bar(stat="identity") + # Only line that changed...
xlab("Student Name") +
ylab("Numeric Grade") +
ggtitle("Course Grade Results")
library(ggplot2)
rd = data.frame(Student = c("Bob", "Sue", "Cat", "Lin"),
NumberGrade = c(96, 82, 97, 74),
LetterGrade = factor(c("A","B","A","C")) )
ggplot(rd, aes(x=Student,y=NumberGrade)) +
geom_bar(stat="identity") +
coord_flip() +
xlab("Student Name") +
ylab("Numeric Grade") +
ggtitle("Course Grade Results")
library(ggplot2)
rd = data.frame(Student = c("Bob", "Sue", "Cat", "Lin"),
NumberGrade = c(96, 82, 97, 74),
LetterGrade = factor(c("A","B","A","C")) )
ggplot(rd, aes(x=Student,y=NumberGrade,fill=LetterGrade)) +
geom_bar(stat="identity") +
xlab("Student Name") +
ylab("Numeric Grade") +
ggtitle("Course Grade Results")
library(ggplot2)
myData = data.frame(Furbletude=rnorm(30),
Blehmekness=rnorm(30))
ggplot(myData, aes(x=Furbletude, y=Blehmekness)) +
geom_point(color="lightblue", fill="darkblue", size=4)
library(ggplot2)
myData = data.frame(Furbletude=rnorm(30),
Blehmekness=rnorm(30))
ggplot(myData, aes(x=Furbletude, y=Blehmekness)) +
geom_point(color="darkblue", fill="lightblue", size=4, shape=21)
library(ggplot2)
myData = data.frame(Count=sample(1:10, 30, replace=T),
Awesomeness=sample(c("CoolThings", "SillyThings", "Meh"), 30, replace=T))
ggplot(myData, aes(x=Awesomeness, y=Count)) +
geom_bar(stat="identity", color="white", fill=rgb(0.12, 0.76, 0.9))
library(ggplot2)
myData = data.frame(Count=sample(1:10, 30, replace=T),
Awesomeness=sample(c("CoolThings", "SillyThings", "Meh"), 30, replace=T),
TypeOfThing=sample(c("A", "B", "C"), 30, replace=T))
ggplot(myData, aes(x=Awesomeness, y=Count, fill=TypeOfThing)) +
geom_bar(stat="identity", color="black")
library(ggplot2)
library(RColorBrewer)
myData = data.frame(Count=sample(1:10, 30, replace=T),
Awesomeness=sample(c("CoolThings", "SillyThings", "Meh"), 30, replace=T),
TypeOfThing=sample(c("A", "B", "C"), 30, replace=T))
ggplot(myData, aes(x=Awesomeness, y=Count, fill=TypeOfThing)) +
geom_bar(stat="identity") +
scale_fill_brewer(palette="Set1")
library(ggplot2)
ggplot(mtcars, aes(x=mpg,y=hp)) +
geom_smooth(size=1.5, color="darkgray") +
geom_point(aes(size=gear,color=cyl)) +
xlab("Miles per Gallon") +
ylab("Horse Power")
library(ggplot2)
ggplot(mtcars, aes(x=mpg,y=hp)) +
geom_point(size=4, shape=21, fill="lightblue", color="darkblue") +
xlab("Miles per Gallon") +
ylab("Horse Power") +
theme(text=element_text(size=18, family="Times"))
library(ggplot2)
ggplot(diamonds, aes(carat)) +
geom_histogram(binwidth=0.5, fill="wheat", color="black") +
xlab("Carat") +
ylab("Count") +
ggtitle("Diamond Carat Distribution")
theme(text=element_text(size=18, family="Times"))