##Seminar packages library(ggplot2) library(MASS) ##The `Sitka` dataset data(Sitka) str(Sitka) ##The `ggplot()` function and aesthetics ggplot(data = Sitka, aes(x=Time, y=size)) ##Layers and default aesthetics ggplot(data = Sitka, aes(x=Time, y=size)) + geom_point() ggplot(data = Sitka, aes(x=Time, y=size)) + geom_point() + geom_smooth() ggplot(data = Sitka, aes(x=Time, y=size)) + geom_point(aes(color=treat)) + geom_smooth() ##Aesthetics ggplot(data = Sitka, aes(x=Time, y=size)) + geom_point(aes(shape=treat)) + geom_smooth() ##Mapping vs setting ggplot(data = Sitka, aes(x=Time, y=size)) + geom_point(color="green") ##Histograms ggplot(Sitka, aes(x=size)) + geom_histogram() ggplot(Sitka, aes(x=size)) + geom_histogram() + geom_histogram(bins=20) ##Density plots ggplot(Sitka, aes(x=size)) + geom_density() ggplot(Sitka, aes(x=size)) + geom_density(aes(color=treat)) ##Boxplots ggplot(Sitka, aes(x=treat, y=size)) + geom_boxplot() ##Bar plots ggplot(Sitka, aes(x=treat)) + geom_bar() ggplot(Sitka, aes(x=treat, fill=factor(Time))) + geom_bar() ##Scatter plots ggplot(Sitka, aes(x=Time, y=size)) + geom_point() ggplot(Sitka, aes(x=Time, y=size, color=treat)) + geom_point() ##Line graphs ggplot(Sitka, aes(x=Time, y=size, group=tree)) + geom_line() ggplot(Sitka, aes(x=Time, y=size, group=tree, color=treat)) + geom_line() ggplot(Sitka, aes(x=Time, y=size, group=tree, linetype=treat)) + geom_line() ##Stats ggplot(Sitka, aes(x=Time, y=size)) + stat_summary() ##Scales ggplot(Sitka, aes(x=Time, y=size, color=treat)) + geom_point() + scale_color_manual(values=c("orange","purple")) ## Scale functions for the axes ggplot(Sitka, aes(x=Time, y=size)) + geom_point() + scale_x_continuous(breaks=c(150,180,210,240), labels=c(5,6,7,8), name="time(months)") ##Faceting ggplot(Sitka, aes(x=Time, y=size)) + geom_point() + facet_grid(treat~.) ##Adjust theme elements with `theme()` and element functions ggplot(Sitka, aes(Time, size)) + geom_point() + theme(axis.ticks=element_line(color="white")) ##Saving plots to files ggsave("mygraph.png") # Practicing to learn the grammar of graphics ## Rabbit graph 1 ggplot(Rabbit, aes(x=Dose, y=BPchange)) + geom_line() ## Rabbit graph 2 ggplot(Rabbit, aes(x=Dose, y=BPchange, linetype=Animal)) + geom_line() ## Rabbit graph 3 ggplot(Rabbit, aes(x=Dose, y=BPchange, linetype=Animal)) + geom_line() + facet_wrap(~Treatment) ## Rabbit graph 4 ggplot(Rabbit, aes(x=Dose, y=BPchange, linetype=Animal, shape=Animal)) + geom_line() + facet_wrap(~Treatment) + geom_point() ## Rabbit graph 5 ggplot(Rabbit, aes(x=Dose, y=BPchange, shape=Animal, linetype=Animal)) + geom_line() + facet_wrap(~Treatment) + geom_point() + scale_shape_manual(values=c(0, 3, 8, 16, 17)) ## Rabbit graph 6 ggplot(Rabbit, aes(x=Dose, y=BPchange, shape=Animal, linetype=Animal)) + geom_point() + geom_line() + facet_wrap(~Treatment) + scale_shape_manual(values=c(0, 3, 8, 16, 17)) + labs(x="Dose(mcg)", y="Change in blood pressure") ## Rabbit graph 7 ggplot(Rabbit, aes(x=Dose, y=BPchange, shape=Animal, linetype=Animal)) + geom_point() + geom_line() + facet_wrap(~Treatment) + scale_shape_manual(values=c(0, 3, 8, 16, 17)) + labs(x="Dose(mcg)", y="Change in blood pressure") + theme(panel.background = element_rect(fill="white"), panel.grid=element_line(color="gray90")) ## Rabbit graph 8 ggplot(Rabbit, aes(x=Dose, y=BPchange, shape=Animal, linetype=Animal)) + geom_point() + geom_line() + facet_wrap(~Treatment) + scale_shape_manual(values=c(0, 3, 8, 16, 17)) + labs(x="Dose(mcg)", y="Change in blood pressure") + theme(panel.background = element_rect(fill="white"), panel.grid=element_line(color="gray90"), axis.title=element_text(face="bold"), strip.text=element_text(face="bold"), legend.title=element_text(face="bold")) ## New dataset `birthwt` {`MASS`} data(birthwt) str(birthwt) ## Convert categorical variables to factors before graphing birthwt$low <- factor(birthwt$low, levels=0:1, labels=c("low", "not low")) birthwt$race <- factor(birthwt$race, levels=1:3, labels=c("white", "black", "other")) birthwt$ht <- factor(birthwt$ht, levels=0:1, labels=c("non-hypert", "hypert")) birthwt$smoke <- factor(birthwt$smoke, levels=0:1, labels=c("did not smoke", "smoked")) str(birthwt) ggplot(birthwt, aes(x=age, y=bwt, shape=smoke, alpha=lwt)) + geom_point() ##Overlapping bars in bar graphs ggplot(birthwt, aes(x=low, fill=smoke)) + geom_bar(position="dodge") ggplot(birthwt, aes(x=low, fill=smoke)) + geom_bar(position="fill") ggplot(birthwt, aes(x=age, y=bwt)) + geom_point() ## Error bars and confidence bands m2 <- glm(low ~ smoke + age + ptl + ui, family="binomial", data=birthwt) ci <- confint(m2) odds_ratios <- data.frame(coef = exp(coef(m2)[-1]), lower = exp(ci[-1,1]), upper= exp(ci[-1,2])) odds_ratios$term <- row.names(odds_ratios) ggplot(odds_ratios, aes(x=term, y=coef)) + geom_point() + geom_errorbar(aes(ymin=lower, ymax=upper)) ggplot(odds_ratios, aes(x=term, y=coef)) + geom_point() + geom_errorbar(aes(ymin=lower, ymax=upper)) + geom_hline(yintercept = 1, color="red") ## ColorBrewer ggplot(birthwt, aes(x=low, fill=race)) + geom_bar() + scale_fill_hue(h.start=50) ggplot(birthwt, aes(x=low, fill=race)) + geom_bar() + scale_fill_brewer(type="qual", palette="Pastel1") # Exercise 1 hsb <- read.csv("https://stats.idre.ucla.edu/stat/data/hsbdemo.csv") ggplot(hsb, aes(x=honors, y=math, fill=female)) + geom_boxplot() + scale_fill_manual(values=c("blue", "gold")) # Exercise 2 ggplot(hsb, aes(x=female, fill=prog)) + geom_bar(position="dodge") + facet_grid(schtyp ~ ses) # Exercise 3 ggplot(hsb, aes(x=read, y=write, color=math)) + geom_point() + geom_smooth(color="red") + labs(x="Reading Score", y="Writing Score", color="Math Score") + theme(title=element_text(family="mono", color="red"), panel.background=element_blank())