EX1
ggplot() +
stat_bin(data = subset(dta, female=="Male"), binwidth = bw, # 繪製男生的長條圖,因為往左所以呈現負的
aes(math, color = "Male", fill = "Male", y = - ..density.. )) +
stat_bin(data = subset(dta, female == "Female"), binwidth = bw, # 繪製女生的長條圖,往右所以為正
aes(math, color = "Female", fill = "Female", y = ..density.. )) +
scale_color_manual(values = c("black", "black"), # 調整外框的顏色與標籤部分,以及呈現的狀況
guide = guide_legend(title = NULL, direction = "horizontal",
title.position = "top", reverse = TRUE,
label.position = "bottom", label.hjust = .5, label.vjust = .5,
label.theme = element_text(angle = 90) ) ) +
scale_fill_manual(values = c("White", "gray80"), # 調整填滿長條圖的顏色
guide = guide_legend(title = NULL, reverse = TRUE,
direction = "horizontal", title.position = "top",
label.position = "bottom", label.hjust = .5, label.vjust = .5,
label.theme = element_text(angle = 90))) +
scale_x_continuous(limits = c(30, 80), breaks=seq(30, 80, by = 5)) + # 應該是對刻度以及標籤的調整
labs(x = "Mathematic score", y = "Density") +
coord_flip() +
theme(legend.position=c(.9, .85))

# 畫出來基本上呈現橫向金字塔型,區別男女
EX2
m0 <- lm(math ~ read + write + science + socst + race + ses + female, data = dta)
dta_m0 <- ggpredict(m0, terms = c("race", "female", "ses"))
p1 <- plot(dta_m0)+labs(y = "Mean math score", x = "Race")
p2 <- ggplot(dta, aes(race, math, color = female))+
stat_summary(fun.data = "mean_se",
geom = "pointrange",
position = pd)+
facet_grid(~ses)
grid.arrange(p1, p2, ncol = 1)
## Warning: Removed 3 rows containing missing values (geom_pointrange).

# 基本上本題透過資料型態進行繪製,與範例中的透過預測模式進行繪圖的基準不同,模式當中已經涵蓋其他變項
EX3
dta3 <- read.csv("data/kdt.csv", header = TRUE, sep = "\t")
ggplot(dta3,aes(Test, Accuracy,fill=Format))+
coord_cartesian(ylim=c(85,100))+
geom_bar(stat="identity",position='dodge', colour="black")+
geom_errorbar(aes(ymin = Accuracy-SE , ymax= Accuracy+SE), width = .2,position=position_dodge(.9))+
scale_fill_manual(values=c("#CCCCCC","#555555"))
