Introduction

install.packages("ggplot2")
library("ggplot2")
data(diamonds)
data(iris)
data(mtcars)
View(diamonds)
View(iris)
View(mtcars)
help(diamonds)
help(iris)
help(mtcars)

Introduction to ggplot2

d <- ggplot(data = diamonds, aes(x=carat, y=price)) 
d + geom_point()

i <- ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width)) 
i + geom_point()

m <- ggplot(data = mtcars, aes(x = wt, y = mpg)) 
m + geom_point()

  1. The data being graphed (diamonds dataset)

  2. Mapping of aesthetics to the attributes with the aes() function and assignments “x=carat” (carat on the x-axis) and “y=price” (price on the y-axis)

  3. Lastly, the assigned layer, which defines what type of graph it is. Since we want a scatter plot, the corresponding layer is geom_pont.

d + geom_point(aes(colour=clarity))

i + geom_point(aes(colour=Species))

m + geom_point(aes(colour=factor(cyl)))

d + geom_point(aes(colour=clarity, size=cut))
## Warning: Using size for a discrete variable is not advised.

m + geom_point(aes(colour=factor(cyl), size = qsec))

d + geom_point(aes(colour=clarity, shape=cut))

i + geom_point(aes(colour=Species, shape=Species))

m + geom_point(aes(colour = factor(cyl), shape = factor(cyl)))

d + geom_point() + geom_smooth()
## `geom_smooth()` using method = 'gam'

i + geom_point(aes(colour = Species)) + geom_smooth(method = "lm")

d + geom_point() + geom_smooth(se=FALSE)
## `geom_smooth()` using method = 'gam'

m + geom_point(aes(colour = factor(cyl))) + geom_smooth(method="lm", se=FALSE)

Faceting and Additional Options

d + geom_point(aes(colour=cut)) + facet_wrap(~ clarity)

i + geom_point() + facet_grid(. ~ Species) + geom_smooth(method = "lm")

d + geom_point(aes(colour=cut)) + facet_grid(color ~ clarity)

ggplot(data=mtcars, aes(x=mpg, y=disp)) + 
  geom_point(aes(color = carb)) + 
  facet_grid(cyl ~ gear)

d + geom_point() + ggtitle("My scatter plot")

ggplot(diamonds, aes(x=carat, y=price)) +
  geom_point() +
  ggtitle("My scatter plot") +
  xlab("Weight (carats)")

ggplot(diamonds, aes(x=carat, y=price)) +
  geom_point() +
  ggtitle("My scatter plot") +
  xlab("Weight (carats)") +
  xlim(0, 2)
## Warning: Removed 1889 rows containing missing values (geom_point).

ggplot(diamonds, aes(x=carat, y=price)) +
  geom_point() +
  ggtitle("My scatter plot") +
  xlab("Weight (carats)") +
  ylim(0, 10000)
## Warning: Removed 5222 rows containing missing values (geom_point).

ggplot(diamonds, aes(x=carat, y=price)) +
  geom_point() +
  ggtitle("My scatter plot") +
  xlab("Weight (carats)") +
  scale_y_log10() 

Line Graphs

m + geom_line(aes(colour = as.factor(cyl)))

Histograms and Density Plots

ggplot(diamonds, aes(x=price)) +
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(diamonds, aes(x=price)) +
  geom_histogram(binwidth=2000)

ggplot(data = mtcars, aes(x = mpg, fill=cyl)) +
  geom_histogram(binwidth = 1)

ggplot(diamonds, aes(x=price, fill=clarity)) +
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot(diamonds, aes(x=price)) + 
  geom_density()

ggplot(diamonds, aes(x=price, color=cut)) +
  geom_density()

ggplot(iris, aes(x = Sepal.Length, color = Species, fill=Species)) +
  geom_density(alpha=0.3)

Boxplots

ggplot(diamonds, aes(x=color, y=price)) +
  geom_boxplot()

ggplot(iris, aes(x=Species,y=Sepal.Length)) + 
  geom_boxplot()

Output: Saving Your Plots

p = ggplot(diamonds, aes(x=carat, y=price)) +
  geom_point()
  1. PNG
ggsave(filename="diamonds.png", p)
  1. PDF
ggsave(filename="diamonds.pdf", p)
  1. JPEG
ggsave(filename="diamonds.jpeg", p)
ggplot(diamonds, aes(x=carat, y=price)) +
  geom_point()

ggsave("diamonds.png")