# Load library ggplot2
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.1
head(iris)
#ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))+ geom_point()
myplot <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width))
myplot + geom_point()
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(size = 3)
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(size = 3)
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point(aes(shape = Species), size = 3)
# Box-plot illustrating birth weight by race
library(MASS) # for loading birthwt data
ggplot(birthwt, aes(factor(race), bwt)) + geom_boxplot()
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point() + facet_grid(Species ~ .)
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point() + facet_grid( . ~ Species)
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point() +
facet_wrap( ~ Species) # notice lack of .
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_point() + facet_grid(Species ~ .) + scale_color_manual(values = c("red", "green", "blue"))
scale_fill_discrete(); scale_colour_discrete()
scale_fill_hue(); scale_color_hue()
scale_fill_manual(); scale_color_manual()
scale_fill_brewer(); scale_color_brewer()
scale_linetype(); scale_shape_manual()
h <- ggplot(faithful, aes(x = waiting))
h + geom_histogram(binwidth = 8, fill = "steelblue",
colour = "black")
# Read the climate data:
climate <- read.csv("C:/Users/Chiranjit Dutta/Dropbox/Chiranjit Dutta/R Tutorial Summer 2022/R Tutorial 2022/Lecture_materials/Data/climate.csv", header = T)
ggplot(climate, aes(Year, Anomaly10y)) + geom_line()
We can also plot confidence regions
ggplot(climate, aes(Year, Anomaly10y)) + geom_ribbon(aes(ymin = Anomaly10y - Unc10y, ymax = Anomaly10y + Unc10y),fill = "blue", alpha = .1) + geom_line(color = "steelblue")
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.2.1
df <- gather(iris, variable, value, -Species)
ggplot(df, aes(Species, value, fill = variable)) +
geom_bar(stat = "identity")
ggplot(df, aes(Species, value, fill = variable)) + geom_bar(stat = "identity", position = "dodge")
ggplot(df, aes(Species, value, fill = variable)) + geom_bar(stat = "identity", position="dodge", color="black")
ggplot(faithful, aes(waiting)) + geom_density()
ggplot(faithful, aes(waiting)) + geom_density(fill = "blue", alpha = 0.1)
Themes are a great way to define custom plots.
+theme()
# see ?theme() for more options
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point(size = 1.2, shape = 16) +
facet_wrap( ~ Species) +
theme(legend.key = element_rect(fill = NA),
legend.position = "bottom",
strip.background = element_rect(fill = NA),
axis.title.y = element_text(angle = 0))
The US economics time series datasets are used from package ggplot2. This is a data frame with 478 rows and 6 variables.
head(economics)
ggplot(economics, aes(x=date)) +
geom_line(aes(y = psavert), color = "darkred") +
geom_line(aes(y = uempmed), color="steelblue", linetype="twodash") +
ggtitle("Multiple time series plot on a single graph") # putting the title on the graph
ggsave("˜/path/to/figure/filename.png")
ggsave(plot1, file = "˜/path/to/figure/filename.png")
ggsave(file = "/path/to/figure/filename.png", width = 6, height =4)
ggsave(file = "/path/to/figure/filename.eps")
ggsave(file = "/path/to/figure/filename.jpg")
ggsave(file = "/path/to/figure/filename.pdf")