# R语言绘图简单教程

## WEEK2

### Historgram

#### Basic

ggplot(titanic_survival, aes(x = age)) + geom_histogram()

aes里面是x轴，是前面数据包里面的一个子项。

（用的titanic基本数据

#### Binwidth

ggplot(titanic_survival, aes(x = age)) + geom_histogram(binwidth = 5)

#### Label and Title

ggplot(titanic_survival, aes(x = age)) + geom_histogram(binwidth = 5) + xlab("Age") + ylab("Count") + ggtitle("Age of Titanic Survivals") + theme(plot.title = element_text(hjust = 0.5))

#### Color

ggplot(titanic_survival, aes(x = age)) + geom_histogram(binwidth = 5, color = "blue", fill = "white") + xlab("Age") + ylab("Count") + ggtitle("Age of Titanic Survivals") + theme(plot.title = element_text(hjust = 0.5))

ggplot(titanic_survival2, aes(x = age)) + geom_histogram(binwidth = 5, color = "blue", fill = "white") + xlab("Age") + ylab("Count") + ggtitle("Age of Titanic Survivals") + theme(plot.title = element_text(hjust = 0.5)) + geom_vline(aes(xintercept = mean(titanic_survival$age)), color = "yellow") + geom_text(aes(label = ifelse(age > 100, as.character(v1),'')), y = 8) 果然是万能的dp逃过了一劫 #### update2 Gradual Change Color 学了个很秀的渐变画图方法。。先从这里开始吧 ggplot(titanic_survival, aes(x = age)) + geom_histogram(binwidth = 5, color = "blue", fill = "white") + xlab("Age") + ylab("Count") + ggtitle("Age of Titanic Survivals") + theme(plot.title = element_text(hjust = 0.5)) + geom_vline(aes(xintercept = mean(titanic_survival$age)), color = "yellow")

ggplot(titanic_survival, aes(x = age)) + geom_histogram(binwidth = 5, aes(fill = cut(age, 100))) + xlab("Age") + ylab("Count") + ggtitle("Age of Titanic Survivals") + theme(plot.title = element_text(hjust = 0.5)) + geom_vline(aes(xintercept = mean(titanic_survival$age)), color = "yellow") 额。。ggplot给每个颜色都上了个图例。。用show.legend = F取消一下 ggplot(titanic_survival, aes(x = age)) + geom_histogram(binwidth = 5, aes(fill = cut(age, 100)), show.legend = F) + xlab("Age") + ylab("Count") + ggtitle("Age of Titanic Survivals") + theme(plot.title = element_text(hjust = 0.5)) + geom_vline(aes(xintercept = mean(titanic_survival$age)), color = "yellow")

1.加上alpha = x改变一下透明度。注意x是百分比，而且alpha最好在涂色之前加，之后可能会出现诡异的错误

ggplot(titanic_survival, aes(x = age)) + geom_histogram(binwidth = 5, alpha = 0.85, aes(fill = cut(age, 100)), show.legend = F) + xlab("Age") + ylab("Count") + ggtitle("Age of Titanic Survivals") + theme(plot.title = element_text(hjust = 0.5)) + geom_vline(aes(xintercept = mean(titanic_survival$age)), color = "yellow") 2.限制颜色出现的区间 感觉五彩的过于不正经，感觉限制到2个配色还好。scale_fill_discrete(h = c(x, y))，xy区间就多试试吧，也没啥好方法。。 ggplot(titanic_survival, aes(x = age)) + geom_histogram(binwidth = 5, alpha = 0.85, aes(fill = cut(age, 100)), show.legend = F) + scale_fill_discrete(h = c(200, 380)) + xlab("Age") + ylab("Count") + ggtitle("Age of Titanic Survivals") + theme(plot.title = element_text(hjust = 0.5)) + geom_vline(aes(xintercept = mean(titanic_survival$age)), color = "yellow")

3.改变浓度与亮度

scale_fill_discrete(h = c(200, 380), c = 120, l = 70) 浓度和亮度（chroma and luminance不知道翻译的对不对）分别对应c和l，个人感觉没啥区别。。但我也没啥美术功底

ggplot(titanic_survival, aes(x = age)) + geom_histogram(binwidth = 5, alpha = 0.85, aes(fill = cut(age, 100)), show.legend = F) + scale_fill_discrete(h = c(200, 380), c = 120, l = 70) + xlab("Age") + ylab("Count") + ggtitle("Age of Titanic Survivals") + theme(plot.title = element_text(hjust = 0.5)) + geom_vline(aes(xintercept = mean(titanic_survival\$age)), color = "yellow")

### Boxplot

#### Basic

boxplot一般都是对比了，我就直接双变量了。。

ggplot(titanic_survival, aes(x = sex, y = age)) + geom_boxplot()

#### Color

ggplot(titanic_survival, aes(x = sex, y = age)) + geom_boxplot(fill = c("red", "blue"))

#### Label and Title

ggplot(titanic_survival, aes(x = sex, y = age)) + geom_boxplot(fill = c("red", "blue")) + xlab("Gender") + ylab("Age") + ggtitle("Boxplot") + theme(plot.title = element_text(hjust = 0.5))

#### Outliers

ggplot(titanic_survival, aes(x = sex, y = age)) + geom_boxplot(fill = c("red", "blue"), outlier.colour="red", outlier.shape=8) + xlab("Gender") + ylab("Age") + ggtitle("Boxplot") + theme(plot.title = element_text(hjust = 0.5))

### Scatterplot

#### Basic

ggplot(auto.mpg, aes(x = weight, y = mpg)) + geom_point()

ggplot(auto.mpg, aes(x = weight, y = mpg)) + geom_point() + xlab("Weigth") + ylab("MPG") + ggtitle("Relationship Between MPG and Weight") + theme(plot.title = element_text(hjust = 0.5))

#### Color Size Shape

ggplot(auto.mpg, aes(x = weight, y = mpg)) + geom_point(aes(color = factor(cylinders))) + xlab("Weigth") + ylab("MPG") + ggtitle("Relationship Between MPG and Weight") + theme(plot.title = element_text(hjust = 0.5))

ggplot(auto.mpg, aes(x = weight, y = mpg)) + geom_point(aes(color = factor(cylinders))) + xlab("Weigth") + ylab("MPG") + ggtitle("Relationship Between MPG and Weight") + theme(plot.title = element_text(hjust = 0.5)) + scale_colour_discrete(name = "Cylinders", breaks = c('3', '4', '5', '6', '8'), labels = c("Three", "Four", '5', '6', '8'))

ggplot(auto.mpg, aes(x = weight, y = mpg)) + geom_point(aes(color = factor(cylinders))) + xlab("Weigth") + ylab("MPG") + ggtitle("Relationship Between MPG and Weight") + theme(plot.title = element_text(hjust = 0.5)) +scale_color_manual(name = "Cylinders", breaks = c('3', '4', '5', '6', '8'), values = c("purple", "red", 'yellow', 'blue', 'green'))

（好像更丑了。。。

#### Smooth

ggplot(auto.mpg, aes(x = weight, y = mpg)) + geom_point(aes(color = factor(cylinders))) + xlab("Weigth") + ylab("MPG") + ggtitle("Relationship Between MPG and Weight") + theme(plot.title = element_text(hjust = 0.5)) +scale_color_manual(name = "Cylinders", breaks = c('3', '4', '5', '6', '8'), values = c("purple", "red", 'yellow', 'blue', 'green')) + geom_smooth(color = "orange")

ggplot(auto.mpg, aes(x = weight, y = mpg)) + geom_point(aes(color = factor(cylinders))) + xlab("Weigth") + ylab("MPG") + ggtitle("Relationship Between MPG and Weight") + theme(plot.title = element_text(hjust = 0.5)) +scale_color_manual(name = "Cylinders", breaks = c('3', '4', '5', '6', '8'), values = c("purple", "red", 'yellow', 'blue', 'green')) + geom_smooth(color = "orange", method = lm)

#### Wrap

ggplot(auto.mpg, aes(x = weight, y = mpg)) + geom_point(aes(color = factor(cylinders))) + xlab("Weigth") + ylab("MPG") + ggtitle("Relationship Between MPG and Weight") + theme(plot.title = element_text(hjust = 0.5)) +scale_color_manual(name = "Cylinders", breaks = c('3', '4', '5', '6', '8'), values = c("purple", "red", 'yellow', 'blue', 'green')) + geom_smooth(color = "orange", method = lm) + facet_wrap(~cylinders, ncol = 3)

emmm….看个正常点的吧，某次作业里的

×