#################################################
# E03: ggplot2                                  #
# 吳漢銘 國立政治大學統計學系                   #
# https://hmwu.idv.tw                           #
#################################################


# 4/77
# install.packages("ggplot2")
library(ggplot2)
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point(size = 3)



# 11/77
ggplot(iris, aes(x = 1:150, y = Sepal.Length, color = Species)) + geom_point()

iris.index.plot <- function(x){
  ggplot(iris, aes(x = 1:nrow(iris), y = iris[,x], color = Species)) + 
    geom_point() +
    labs(x = names(iris)[x], y = "values") 
}
index.list <- lapply(1:4, iris.index.plot)
library(grid)
library(gridExtra)
marrangeGrob(index.list, nrow = 2, ncol = 2, top = "")


# select numerical variables
mydata <- infert
dim(mydata)
head(mydata)
str(mydata)
id <- sapply(mydata, is.numeric) 
id
mydata.numeric <- mydata[, id]
dim(mydata.numeric)
head(mydata.numeric)

# is.integer, is.double, # is.logical, is.character, # is.POSIXt, is.POSIXlt, 
# is.POSIXct



# 12/77
library(ggplot2)
p <- ggplot(iris, aes(x = Species, y = Sepal.Length)) + 
  geom_boxplot()
p

p + coord_flip() # Rotate the box plot

# Notched box plot
ggplot(iris, aes(x = Species, y = Sepal.Length)) + 
  geom_boxplot(notch = TRUE)

ggplot(iris, aes(x = Species, y = Sepal.Length)) + 
  geom_boxplot(outlier.colour = "red", outlier.shape = 8,
               outlier.size = 3)



# 13/77
# stat_summary(): add mean points to a box plot 
p <- ggplot(iris, aes(x = Species, y = Sepal.Length)) + 
  geom_boxplot()

p

p + stat_summary(fun = mean, geom = "point", shape = 2, size = 4, col = "red")

p + geom_dotplot(binaxis = "y", stackdir = "center")



# 14/77
p <- ggplot(iris, aes(x = Species, y = Sepal.Length, color = Species)) + 
  geom_boxplot()
p

# Use custom color palettes
p + scale_color_manual(values = c("orange", "purple", "darkgreen"))

# Use brewer color palettes
p + scale_color_brewer(palette = "Set2")


# turn off legends
ggplot(iris, aes(x = Species, y = Sepal.Length, color = Species)) + 
  geom_boxplot(show.legend = FALSE)

# remove the legend after the plot is created 
p + theme(legend.position = "none")

library(RColorBrewer)
display.brewer.all()



# 15/77
p <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + 
  geom_boxplot()
p

p + scale_fill_manual(values = c("orange", "purple", "darkgreen"))

p + scale_fill_brewer(palette = "Set2")



# 16/77
p <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + 
  geom_boxplot()
p
p + theme(legend.position = "top")
p + theme(legend.position = "bottom")
p + theme(legend.position = "none")



# 17/77
levels(iris$Species)

p <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + 
  geom_boxplot()
p
p + scale_x_discrete(limits = c("setosa", "versicolor"))
p + scale_x_discrete(limits = c("versicolor", "setosa", "virginica"))



# 18/77
set.seed(12345)
select.id <- sample(1:150, 50)
if.selected <- 1:150 %in% select.id
iris.sel <- cbind(iris, if.selected)
head(iris.sel)

p <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = if.selected)) + 
  geom_boxplot() +
  labs(title = "iris data with selected IDs")
p



# 19/77
ggplot(iris, aes(x = Species, y = Sepal.Length)) + 
  geom_violin()

p <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + 
  geom_violin(trim = FALSE)
p

p + stat_summary(fun.data = mean_sdl, fun.args = list(mult = 1), 
                 geom = "pointrange", color = "purple")



# 20/77
ggplot(iris, aes(x = Species, y = Petal.Length)) + 
  geom_dotplot(binaxis = "y")

ggplot(iris, aes(x = Species, y = Petal.Length)) + 
  geom_dotplot(binaxis = "y", stackdir = "center", 
               stackratio = 1.5, dotsize = 0.5)

ggplot(iris, aes(x = Species, y = Petal.Length)) + 
  geom_boxplot()+
  geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 0.5)

ggplot(iris, aes(x = Species, y = Petal.Length)) + 
  geom_violin(trim = FALSE)+
  geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 0.5, colour = "red")




# 21/77
p <- ggplot(iris, aes(x = Species, y = Petal.Length)) + 
  geom_dotplot(binaxis = "y", stackdir = "center", dotsize = 0.5)
p
p + stat_summary(fun = mean, geom = "point", shape = 17,
                 size = 3, color = "red")

my_summary <- function(x, a = 1) {
  m <- mean(x)
  ymin <- m - a * sd(x)
  ymax <- m + a * sd(x)
  c(y = m, ymin = ymin, ymax = ymax)
}
p + stat_summary(fun.data = my_summary, color = "red")

p + stat_summary(fun.data = my_summary, color = "blue", fun.args = list(a = 2))



# 22/77
# install.packages("gridExtra")
library(gridExtra)
h1 <- ggplot(data = iris, aes(x = Sepal.Length)) + geom_histogram()
h2 <- ggplot(data = iris, aes(x = Sepal.Length)) + geom_histogram(binwidth = 1)
h3 <- ggplot(data = iris, aes(x = Sepal.Length)) + geom_histogram(color = "black", fill = "blue", bins = 10) 
h4 <- ggplot(data = iris, aes(x = Sepal.Length, color = Species)) + geom_histogram(binwidth = 1)
grid.arrange(h1, h2, h3, h4, nrow = 1, ncol = 4)



# 23/77
p <- ggplot(data = iris, aes(x = Sepal.Length)) 
p <- p + geom_histogram()
p + facet_grid(Species~.)   #row



# 24/77
library(gridExtra)
sl <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) + 
  geom_histogram(binwidth = 0.1)
sw <- ggplot(iris, aes(x = Sepal.Width, fill = Species)) + 
  geom_histogram(binwidth = 0.1)
pl <- ggplot(iris, aes(x = Petal.Length, fill = Species)) + 
  geom_histogram(binwidth = 0.1)
pw <- ggplot(iris, aes(x = Petal.Width, fill = Species)) + 
  geom_histogram(binwidth = 0.1)
grid.arrange(sl, sw, pl, pw, nrow = 2)



# 25/77
iris.hist <- function(x){
  ggplot(iris, aes(x = iris[,x], fill = Species)) + 
    geom_histogram(binwidth = 0.1) +
    xlab(names(iris)[x])
}

hist.list <- lapply(1:4, iris.hist)
library(grid)
marrangeGrob(hist.list, nrow = 2, ncol = 2, top = "")



# 26/77
p1 <- ggplot(iris, aes(x = Sepal.Length)) +     
  geom_density()

p2 <- ggplot(iris, aes(x = Sepal.Length, color = Species)) +
  geom_density()

p3 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density()

# Add mean line
p1 + geom_vline(aes(xintercept = mean(Sepal.Length)),
                color = "blue", linetype = "dashed", size = 1)

mu <- tapply(iris$Sepal.Length, iris$Species, mean)
mu.df <- data.frame(Sp = names(mu), grp.mean = mu)
head(mu.df)
p2 + geom_vline(data = mu.df, aes(xintercept = grp.mean, color = Sp),
                linetype = "dashed")



# 27/77
# Change line color, line type and fill color
ggplot(iris, aes(x = Sepal.Length))+
  geom_density(color = "darkblue", fill = "lightblue", linetype = "dashed")

# Use semi-transparent fill
ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
  geom_density(alpha = 0.4)



# 28/77
# Histogram with density plot
ggplot(iris, aes(x = Sepal.Length)) + 
  geom_histogram(aes(y = ..density..), colour = "black", fill = "lightblue") +
  geom_density(alpha = 0.2, fill = "red") 

# Color by groups
phd <- ggplot(iris, aes(x = Sepal.Length, color = Species, fill = Species)) + 
  geom_histogram(aes(y = ..density..), alpha = 0.5, position = "identity") +
  geom_density(alpha = .2)
phd

# facets: Split the plot in multiple panels
phd + facet_grid(Species ~ .)



# 29/77
head(mtcars)



# 30/77
mtcars$cyl <- as.factor(mtcars$cyl)
head(mtcars)

p <- ggplot(mtcars, aes(x = cyl)) + 
  geom_bar() + 
  labs(x = "汽缸數(cyl)", y = "車輛數")

p + coord_flip()

cyl.df <- data.frame(table(mtcars$cyl))
cyl.df
names(cyl.df) <- c("cyl", "count")
cyl.df

ggplot(cyl.df, aes(x = cyl, y = count)) +
  geom_bar(stat = "identity") +
  labs(x = "汽缸數(cyl)", y = "車輛數")



# 31/77
ggplot(cyl.df, aes(x = cyl, y = count)) +
  geom_bar(stat = "identity", width = 0.5, color = "black", fill = "steelblue") 
p <- ggplot(cyl.df, aes(x = cyl, y = count, fill = cyl)) +
  geom_bar(stat = "identity", width = 0.5) 
p
p + geom_text(aes(label = count), vjust = -0.3, size = 4)
p + geom_text(aes(label = count), vjust = 1.6, color = "white", size = 4)
p + scale_fill_manual(values = c("#999000", "#E69F99", "#56B4E9"))
p + scale_fill_brewer(palette = "Dark2")
p + scale_fill_grey()



# 32/77
iris.mean <- aggregate(iris[,1:4], by = list(Species = iris$Species), FUN = mean) 
iris.mean
mydata <- cbind(stack(iris.mean[,-1]), Species = iris.mean$Species)
mydata
ggplot(mydata, aes(x = ind, y = values, fill = Species)) + 
  geom_bar(stat = "identity")



# 33/77
ggplot(mydata, aes(x = ind, y = values, fill = Species)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_text(aes(label = values), vjust = 1.4, color = "white",
            position = position_dodge(0.9)) + 
  labs(x = "", y = "mean")



# 34/77
library(plyr)
mydata.sorted <- arrange(mydata, ind, Species) 
mydata.sorted
mydata.sorted$Species <- factor(mydata.sorted$Species, levels = rev(levels(mydata$Species)))
mydata.cumsum <- ddply(mydata.sorted, "ind",
                       transform, label.ypos = cumsum(values))
mydata.cumsum
ggplot(mydata.cumsum, aes(x = ind, y = values, fill = Species)) +
  geom_bar(stat = "identity") +
  geom_text(aes(y = label.ypos, label = values), vjust = 1.6, color = "white")



# 35/77
head(airquality)
airquality$Month <- factor(airquality$Month)
ggplot(airquality, aes(x = Day, y = Temp, group = Month, color = Month)) +
  geom_line(aes(linetype = Month)) +
  geom_point()

linetype <- c("blank", "solid", "dashed", "dotted", "dotdash", "longdash", "twodash")
df <- data.frame(linetype, no = 1:length(linetype))
ggplot(df, aes(x = 1:7, y = no, group = linetype)) +
  geom_line()+
  geom_point()



# 36/77
sales <- data.frame(
  date = seq(Sys.Date(), length.out = 100, by = "1 day")[sample(100, 50)],
  price = floor(rnorm(50, mean = 100, sd = 20))
)
sales <- sales[order(sales$date), ]
head(sales)

lp <- ggplot(data = sales, aes(x = date, y = price)) + geom_line()
lp

lp + scale_x_date(date_labels = ("%m/%d"))



# 37/77
lp + scale_x_date(date_breaks =  "1 week") +
  theme(axis.text.x = element_text(angle = 45))

range(sales$date)
# "2020-08-10" "2020-11-07"
amin <- as.Date("2020-09-01")
amax <- as.Date("2020-10-31")
lp + scale_x_date(limits = c(amin, amax))



# 38/77
mydata <- as.data.frame(matrix(rnorm(100), ncol = 4))
library(reshape2)
head(mydata, 3)
#id variable for position in matrix 
mydata$id <- 1:nrow(mydata) 
#reshape to long format
mydata.lf <- melt(mydata, id.var = "id")
head(mydata.lf)
tail(mydata.lf)
ggplot(mydata.lf, aes(x = id, y = value, group = variable, colour = variable)) +
  geom_point()+
  geom_line(aes(lty = variable)) 




# 39/77
pp <- ggplot(airquality, aes(x = Temp, y = Wind)) +
  geom_point()
pp
pp + geom_hline(yintercept = mean(airquality$Wind), linetype = "dashed", color = "red", size = 2) +
  geom_vline(xintercept = mean(airquality$Temp), linetype = "dotted", color = "blue", size = 2)

beta <- lm(Wind ~ Temp, data = airquality)$coefficients
pp + geom_abline(intercept = beta[1], slope = beta[2], color = "red", size = 2) +
  ggtitle(paste0("y = ", round(beta[1], 2), " + ",  round(beta[2], 2), " x"))



# 40/77
pp + geom_segment(aes(x = 70, y = 5, xend = 90, yend = 12), color = "red") +
  geom_segment(aes(x = 65, y = 17, xend = 61.5, yend = 19.5), color = "blue",
               arrow = arrow(length = unit(0.5, "cm"))) +
  geom_point(aes(x = 60, y = 5), color = "darkgreen", shape = 13, size = 4)

xy.df <- data.frame(x1 = airquality$Temp[1:3], y1 = airquality$Wind[1:3], 
                    x2 = airquality$Temp[4:6], y2 = airquality$Wind[4:6])
pp + geom_segment(data = xy.df, aes(x = x1, y = y1, xend = x2, yend = y2), color = 2:4, size = 2)

?geom_curve



# 41/77
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, 
                        shape = Species, color = Species)) + 
  geom_point()

p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, shape = Species, color = Species))
p <- p + geom_point()
p
p + geom_line(aes(y = Sepal.Width))



# 42/77
p <- ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point()
p
p + coord_fixed(ratio = 1)
p + coord_fixed(ratio = 0.5)
p + coord_fixed(ratio = 5)



# 43/77
# Change x and y axis limits
gp <- ggplot(airquality, aes(x = Temp, y = Wind)) +
  geom_point()
gp
gp + xlim(50, 100) + ylim(0, 25)
gp + expand_limits(x = c(50, 100), y = c(0, 25))


# Axis transformations
# trans: "log2", "log10", "sqrt"
gp + scale_x_continuous(trans = "log2") + scale_y_continuous(trans = "log2") +
  labs(x = "log2(Temp)", y = "log2(Wind)")
gp + coord_trans(x = "log2", y = "log2")
gp + scale_y_sqrt() # square root
gp + scale_y_reverse() # Reverse coordinates



# 44/77
gp + scale_y_continuous(labels = percent)
gp + scale_y_continuous(labels = dollar)
gp + scale_y_continuous(labels = scientific)



# 45/77
p <- ggplot(iris, aes(x = Species, y = Sepal.Length)) + 
  geom_boxplot() +
  ggtitle("鳶尾花資料集 (Iris Dataset) 盒形圖") +
  xlab("品種 (Species)") + 
  ylab("花萼長度 (Sepal.Length)")
p
p + theme(plot.title = element_text(color = "red", size = 20, face = "bold.italic"),
          axis.title.x = element_text(color = "blue", size = 14, face = "bold"),
          axis.title.y = element_text(color = "darkgreen", size = 14, face = "bold"))

# Hide the main title and axis titles
p + theme(plot.title = element_blank(),
          axis.title.x = element_blank(),
          axis.title.y = element_blank())



# 46/77
mtcars$cyl <- as.factor(mtcars$cyl)
head(mtcars)

ggplot(mtcars, aes(x = wt, y = mpg)) + 
  geom_point() +
  labs(x = "車體重量(wt)", y = "耗油量(mpg)")

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point(size = 2, color = "blue", shape = 3) +
  labs(x = "車體重量(wt)", y = "耗油量(mpg)")


ggplot(mtcars, aes(x = wt, y = mpg)) + 
  geom_point(aes(size = qsec), color = "darkgreen") +
  labs(x = "車體重量(wt)", y = "耗油量(mpg)", size = "1/4英里加速秒數(qsec)",
       title = "Motor Trend Car Road Tests 資料集 (mtcars)")



# 47/77
# mtcars$cyl <- as.factor(mtcars$cyl)
ggplot(mtcars, aes(x = wt, y = mpg, shape = cyl)) +
  geom_point() +
  labs(x = "車體重量(wt)", y = "耗油量(mpg)", shape = "汽缸數(cyl)")

ggplot(mtcars, aes(x = wt, y = mpg, shape = cyl, color = cyl)) +
  geom_point(size = 3) +
  labs(x = "車體重量(wt)", y = "耗油量(mpg)", shape = "汽缸數(cyl)", color = "汽缸數(cyl)")

mtcars$am <- as.factor(mtcars$am)
ggplot(mtcars, aes(x = wt, y = mpg, shape = am, color = cyl, size = hp)) +
  geom_point() +
  labs(x = "車體重量(wt)", y = "耗油量(mpg)", shape = "手自排(am)", color = "汽缸數(cyl)", size = "馬力(hp)")



# 48/77
p <- ggplot(data = mtcars, aes(x = wt, y = mpg, label = rownames(mtcars))) +
  geom_point() +
  geom_text(size = 3) + 
  labs(x = "車體重量(wt)", y = "耗油量(mpg)")
p
p + geom_label()



# 49/77
set.seed(123)
id <- sample(1:nrow(mtcars), 10)
mtcars.subset <- mtcars[id, ]

sp <- ggplot(mtcars.subset, aes(x = wt, y = mpg, label = rownames(mtcars.subset))) +
  geom_point()
sp + geom_text()
sp + geom_text(size = 3)
sp + geom_text(hjust = 0, vjust = 0)

# 1(normal), 2(bold), 3(italic), 4(bold.italic)
sp + geom_text(aes(fontface = 3)) 
sp + geom_label()



# 50/77
sp + geom_text(aes(color = factor(cyl)))
sp + geom_text(aes(size = cyl))
sp + geom_text(aes(size = cyl)) + scale_size(range = c(3, 6))



# 51/77
sp + geom_text(x = 3, y = 25, label = "Scatter plot")
sp + annotate(geom = "text", x = 3, y = 25, label = "Scatter plot", color = "red")



# 52/77
# ggrepel: Avoid overlapping of text labels
# install.packages("ggrepel")
require("ggrepel")

sp2 <- ggplot(mtcars.subset, aes(x = wt, y = mpg, label = rownames(mtcars.subset))) +
  geom_point(color = "red") 

sp2 + geom_text(size = 3.5)

sp2 + geom_text_repel(size = 3.5) 

sp2 + geom_label_repel(aes(fill = factor(cyl)), color = "white", size = 3.5) +
  theme(legend.position = "bottom")

label.sub <- subset(mtcars.subset, wt > 3 & mpg < 20)
sp2 + geom_label_repel(data = label.sub, 
                       aes(label = rownames(label.sub), fill = factor(cyl)), 
                       color = "white", size = 3.5) +
  theme(legend.position = "bottom")



# 53/77
carb.df <- data.frame(table(mtcars$carb))
names(carb.df) <- c("carb", "Freq")
carb.df

bar.pt <- ggplot(carb.df, aes(x = "", y = Freq, fill = carb)) +
  geom_bar(width = 1, stat = "identity") +
  labs(x = "", fill = "化油器數(carb)") 
bar.pt

pie <- bar.pt + coord_polar("y", start = 0)
pie

pie + scale_fill_brewer(palette = "Set2") 
pie + scale_fill_grey() + theme_minimal()

mtcars$carb <- factor(mtcars$carb)
ggplot(mtcars, aes(x = factor(1), fill = carb))+
  geom_bar(width = 1)+
  coord_polar("y")



# 54/77
cyl.df <- data.frame(table(mtcars$cyl))
names(cyl.df) <- c("cyl", "Freq")
cyl.df$Prop <- prop.table(cyl.df$Freq)
cyl.df

p.bar <- ggplot(cyl.df, aes(x = cyl, y = Freq, fill = cyl)) +
  geom_bar(width = 1, stat = "identity") +
  labs(x = "", title = "mtcars$cyl", fill = "cyl") 

p.bar.tmp <- ggplot(cyl.df, aes(x = "", y = Freq, fill = cyl)) +
  geom_bar(width = 1, stat = "identity") +
  labs(x = "", title = "mtcars$cyl", fill = "cyl") 

p.pie <- p.bar.tmp + coord_polar("y", start = 0) +
  theme_void() + 
  geom_text(aes(label = paste0(round(Prop*100), "%")), 
            position = position_stack(vjust = 0.5))

library(gridExtra)
grid.arrange(p.bar, p.pie, nrow = 2)



# 55/77
ggplot(airquality, aes(sample = Wind)) + 
  stat_qq() +
  labs(title = "QQplot for airquality$Wind")

ggplot(airquality, aes(sample = Wind, shape = Month, color = Month)) + 
  stat_qq() +
  labs(title = "QQplot for airquality$Wind of each Month")



# 56/77
ggplot(airquality, aes(x = Wind)) + 
  stat_ecdf(geom = "point")

ggplot(airquality, aes(x = Wind)) + 
  stat_ecdf(geom = "step") +
  labs(title = "Empirical Cumulative Density Function",
       y = "F(Wind)", x = "Wind")



# 57/77
library(tidyr)
xdata <- iris[, 1:4]
n <- nrow(xdata)
p <- ncol(xdata)
iris.df <- data.frame(x = rep(1:p, each = n), y = rep(1:n, p), 
                      value = gather(xdata)$value)
str(iris.df)
ggplot(iris.df, aes(x = x, y = y, fill = value)) +
  geom_raster() +
  scale_fill_gradient(low = "white", high = "black", na.value = NA) +
  scale_y_reverse() +
  labs(x = "", y = "", title = "heatmap for iris data")

image(t(iris[, 1:4])[, nrow(iris[, 1:4]):1])



# 58/77
# print(): print a ggplot to a file

myplot <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point()
pdf("myplot.pdf") # or png("myplot.png")
print(myplot)
dev.off()

# ggsave: save the last ggplot
ggplot(mtcars, aes(wt, mpg)) + geom_point()
ggsave("myplot.png") 
# ggsave: save a ggplot object
ggsave(file = "myplot2.pdf", 
       plot = myplot, 
       device = "pdf", 
       scale = 1.5)



# 59/77
ggplot(iris, aes(x = Species, y = Sepal.Length)) + 
  geom_boxplot(fill = "lightblue", color = "darkred")

ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + 
  geom_point(color = "blue")


# 60/77
bp <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species)) + geom_boxplot()
bp
sp <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point(size = 3)
sp

bp + scale_fill_hue(l = 50, c = 40)
sp + scale_color_hue(l = 30, c = 40)



# 61/77
bp + scale_fill_manual(values = c("coral", "deeppink", "slateblue2"))
sp + scale_color_manual(values = c("coral", "deeppink", "slateblue2"))

# Use RColorBrewer palettes
bp + scale_fill_brewer(palette = "Dark2")
sp + scale_color_brewer(palette = "Dark2")



# 62/77
# Use gray colors, theme_classic(): turn bg white
bp + scale_fill_grey() + theme_classic()
sp + scale_color_grey() + theme_classic()

bp + scale_fill_grey(start = 0.8, end = 0.2) + theme_classic()
sp + scale_color_grey(start = 0.8, end = 0.2) + theme_classic()



# 63/77
# Continuous colors
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + 
  geom_point(size = 3)
sc <- ggplot(iris, aes(x = Sepal.Length, 
                       y = Sepal.Width, 
                       color = Petal.Length)) + 
  geom_point(size = 3)
sc

# Sequential color scheme
sc + scale_color_gradient(low = "blue", high = "red")

# Diverging color scheme
mid.value <- mean(iris$Petal.Length)
sc + scale_color_gradient2(midpoint = mid.value, low = "blue", mid = "white", high = "red")

# Gradient between n colors
library(fields)
sc + scale_color_gradientn(colours = tim.colors(10))



# 64/77
library(gridExtra)
p <- ggplot(iris, aes(x = Species, y = Sepal.Length)) + geom_boxplot()
p1 <- p + theme_gray() + labs(title = "gray") 
p2 <- p + theme_bw() + labs(title = "bw") 
p3 <- p + theme_linedraw() + labs(title = "linedraw") 
p4 <- p + theme_light() + labs(title = "light")
p5 <- p + theme_dark() + labs(title = "dark") 
p6 <- p + theme_minimal() + labs(title = "minimal")
p7 <- p + theme_classic() + labs(title = "classic")
grid.arrange(p, p1, p2, p3, p4, p5, p6, p7, nrow = 2, ncol = 4)



# 65/77
# install.packages("ggthemes") 
library(ggthemes)
sp <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point()
sp1 <- sp + theme_tufte() + labs(title = "tufte") # a minimalist theme
sp2 <- sp + theme_economist() + labs(title = "economist") 
sp3 <- sp + theme_stata() + labs(title = "stata") 
sp4 <- sp + theme_hc() + labs(title = "hc") # Highcharts JS
sp5 <- sp + theme_wsj() + labs(title = "wsj") # Wall Street Journal
grid.arrange(sp, sp1, sp2, sp3, sp4, sp5, nrow = 2, ncol = 3)



# 66/77
ggplot(mtcars, aes(x = wt, y = mpg, color = as.factor(carb), shape = as.factor(gear))) + 
  geom_point(size = 3) +
  facet_grid(cyl ~ am, labeller = label_both) +
  labs(x = "車體重量(wt)", y = "耗油量(mpg)", color = "化油器數", shape = "變速箱數")



# 67/77
set.seed(12345)
df.A <- data.frame(x = rnorm(10), y = rnorm(10))
df.B <- data.frame(x = rnorm(10), y = rnorm(10))
ggplot(df.A, aes(x, y)) +
  geom_point() +
  geom_point(data = df.B, color = "red", shape = 2, size = 5) 


set.seed(12345)

df.A <- data.frame(xa = rnorm(10), ya=rnorm(10))
df.B <- data.frame(xb = rnorm(10), yb=rnorm(10))

ggplot(df.A, aes(x = xa, y = ya)) +
  geom_point() +
  geom_point(data = df.B, aes(x = xb, y = yb), 
             color = "red", shape = 2, size = 5) 

ggplot() +
  geom_point(data = df.A, aes(x = xa, y = ya)) +
  geom_point(data = df.B, aes(x = xb, y = yb), 
             color = "red", shape = 2, size = 5) 



# 68/77
test.df <- data.frame(Day = as.Date(c("2021-07-20", "2021-07-21", 
                                      "2021-07-22", "2021-07-23", 
                                      "2021-07-24")),
                      Number = c(2, 5, 4, 3, 4), 
                      Percentage = c(0.70, 0.50, 0.95, 0.75, 0.3)
)

ggplot(test.df) + 
  geom_bar(aes(x = Day, y = Number), stat = "identity") +
  geom_line(aes(x = Day, y = Percentage), 
            size = 2, color = "blue")

ggplot(test.df) + 
  geom_bar(aes(x = Day, y = Number), stat = "identity") +
  geom_line(aes(x = Day, y = Percentage * 5), 
            size = 2, color = "blue") +
  scale_y_continuous(sec.axis = sec_axis(~./5, 
                                         name = "Percentage")) 

