很早之前就发现了这个教程(Top 50 ggplot2 Visualizations - The Master List (With Full R Code)),自己作图的时候经常会参考这个教程中的例子,接下来的这段时间自己争取每天都重复其中的一个例子。如果你也想学习R语言ggplot2绘图,欢迎和我一起重复这篇教程中的50幅美图。相信我们在坚持重复完这50个教程之后,我们的R语言技能可以得到显著提升。如果你在重复这些教程中,欢迎添加我的微信,我们可以一起讨论在重复过程中遇到的问题。
散点图是数据分析中非常常用的一种形式(The most frequently used plot for data analysis is undoubtedly the scatterplot);如果你想初步了解两个变量之间的关系,第一选择一定是散点图(Whenever you want to understand the nature of relationship between two variables, invariably the first choice is the scatterplot);ggplot2中用来画散点图的函数是geom_point(),同时可以用geom_smooth()函数添加拟合曲线。
ggplot2是R语言中最为常用的一个绘图包。
如果第一次使用需要通过 install.packages("ggplot2") 来安装;执行命令就可以自动下载安装;以后每次使用需要通过library(ggplot2)来加载。
options(scipen=999) 关闭科学计数法(学英语:turn-off scientific notation)theme_set(theme_bw())可以先忽略data("midwest", package = "ggplot2")加载内置数据集R语言里最为常用的数据格式是
数据框是ggplot2的输入格式,我们自己的数据通常会整理在excel中,读入R语言后就是以数据框格式存贮。
library(ggplot2)
options(scipen=999)
data("midwest",package="ggplot2")
ggplot(midwest,aes(x=area,y=poptotal))+
geom_point()+
geom_smooth()

ggplot(midwest, aes(x=area, y=poptotal)) +
geom_point(aes(col=state, size=popdensity)) +
geom_smooth(method="loess", se=F) +
xlim(c(0, 0.1)) +
ylim(c(0, 500000)) +
labs(subtitle="Area Vs Population",
y="Population",
x="Area",
title="Scatterplot",
caption = "Source: midwest")+
theme_bw()

image.png
如果在重复这幅图的时候遇到任何问题,欢迎在留言讨论