plot():主要用于绘制折线图,箱型图,直方图等
runif():
默认生成0-1之间的符合正态分布的随机数
生成了10个0-100之间的随机数字
plot(rnorm(50)): rnorm(50)是生成50个随机正态分布的数字作为图形的Y轴,根据此索引找到X,绘制散点图
boxplot(iris S e p a l . L e n g t h i r i s Sepal.Length~iris Sepal.Length irisSpecies, col = c(“lightblue”, “lightyellow”, “lightpink”))
iris S e p a l . L e n g t h 表示从 i r i s 数据集中提取花瓣长度这一变量, i r i s Sepal.Length 表示从iris数据集中提取花瓣长度这一变量,iris Sepal.Length表示从iris数据集中提取花瓣长度这一变量,irisSpecies 表示从iris数据集中提取鸢尾花的物种这一变量。~ 符号表示将前一个变量(花瓣长度)按照后一个变量(鸢尾花的物种)进行分组
https://mp.weixin.qq.com/s/G-LXN9P2HVLv9v0cvyFJMA
session:指的是新打开一个R的工作界面/project。翻译为“会话”
dir()
list.files()
ls()当前环境下啥也没有
dir()和list.files()是当前路径下所有东西
1.mpg
mpg是ggplot内置的一个数据框格式
2.基础作图:
ggplot(data= )+mapping(aes())
ggplot(data,aes(x=displ,y=hwy))+
geom_point()
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, size = class))
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
(将车型映射给点的透明度)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, shape = class))但是形状只有6种,因此SUV为空
手动设置图形属性
点的边界线宽度为3
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, stroke=3),shape = 21)
单因素分面
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2)
nrow指定分面后显示几行
ncol指定分面后显示几列
注意~分面依据必须是离散型变量
两因素分面,不需要制定行列
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl)
不想在行/列分面,可以用. 代替
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(. ~ cyl)
颜色映射3个
ggplot(data = mpg) +
geom_smooth(
mapping = aes(x = displ, y = hwy, color = drv),
show.legend = FALSE #不显示图例
)
ggplot(data = mpg) +
geom_smooth(
mapping = aes(x = displ, y = hwy, linetype = drv),
)线形
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) + #映射点的颜色
geom_smooth(data = filter(mpg, class == “subcompact”),se=FALSE)
#映射平滑线,数据源是mpg里面的subcompact的所有行, se=false不使用标准误差即灰色背景
每个几何对象函数都有一个默认的统计变换,每个统计变换函数都又一个默认的几何对象
geom_bar-------统计变换--------stat_count--------几何对象-------直方图
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut)) #两个画出来一样的
demo <- tribble(
~cut, ~freq,
“Fair”, 1610,
“Good”, 4906,
“Very Good”, 12082,
“Premium”, 13791,
“Ideal”, 21551
) #新建表格并赋值给demo
ggplot(data = demo) +
geom_bar(mapping = aes(x = cut, y = freq), stat = “identity”) #覆盖默认的统计变换,使用identity。
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = …prop…, group = 1)) group=1
所有的看做一个整体和为1,纵坐标显示比例
fill是填充色,color是边框色
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut,fill=clarity))
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = “fill”)
ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) +
geom_bar(alpha = 1/5, position = “identity”)
ggplot(data = diamonds, mapping = aes(x = cut, colour = clarity)) +
geom_bar(fill = NA, position = “identity”)
position是位置调整功能
并列直方图:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = “dodge”)
把重叠点分开
ggplot(data = mpg) +
geom_jitter(mapping = aes(x = displ, y = hwy))
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip() #翻转坐标系
ggplot(nz, aes(long, lat, group = group)) +
geom_polygon(fill = “white”, colour = “black”) #geom_polygon是多边形的意思
ggplot(nz, aes(long, lat, group = group)) +
geom_polygon(fill = “white”, colour = “black”) +
coord_quickmap() #为地图设置长宽比
极坐标
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) + #图形长宽比例为1,正方形
labs(x = NULL, y = NULL) #不显示x,y轴
bar + coord_flip() #翻转坐标
bar + coord_polar() #变成极坐标
ggplot(mpg, aes(x = factor(1), fill = drv)) +
geom_bar(width = 1) +
coord_polar(theta = “y”) #theta 参数来确定在哪个轴上应用极坐标转换
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = “fill”) +
coord_polar(theta = “y”) #position = “fill” 表示将每个柱状图堆叠,并将它们的高度归一化为1
ggplot(data = ) +
mapping = aes(),
stat = ,
position =
) +
引用https://mp.weixin.qq.com/s/jOIJLpCN4qTznPH0b-qJdw