ggplot2学习day1
2020-05-15 本文已影响0人
Silmarillion123
使用书籍:R数据可视化手册
快速索引
1.散点图
qplot(mydata$Confirmed,mydata$Deaths)
ggplot(mydata,aes(x=Confirmed,y=Deaths))+geom_point()
推荐使用后者
![](https://img.haomeiwen.com/i23233571/12654217ab925754.png)
2.折线图
ggplot(mydata,aes(x=Confirmed,y=Deaths))+geom_line()
ggplot(mydata,aes(x=Confirmed,y=Deaths))+geom_line()+geom_point()#后者加了数据点
图分别为一下所示
![](https://img.haomeiwen.com/i23233571/0d27603c21ca548a.png)
![](https://img.haomeiwen.com/i23233571/21848797441ce89e.png)
3.条形图
barplot(table(mydata$Country))
统计一列数据的频数
![](https://img.haomeiwen.com/i23233571/e0cd726b4bb23e53.png)
barplot(newdata$Deaths,names.arg=newdata$Province.State)
第一个参数设定高度,第二个参数对应标签
![](https://img.haomeiwen.com/i23233571/54ddd9e6adf5203b.png)
ggplot(a,aes(x=time,y=demand))+geom_bar(stat="identity")
![](https://img.haomeiwen.com/i23233571/075615e6d26fb306.png)
ggplot(a,aes(x=factor(time),y=demand))+geom_bar(stat="identity")
绘图函数里的stat参数表示对样本点做统计的方式,默认为identity,表示一个x对应一个y
![](https://img.haomeiwen.com/i23233571/015b1bd1aeae13c8.png)
以上两者的区别为是否将横坐标作为因子来看待
4.直方图
qplot(newdata1$Deaths)
![](https://img.haomeiwen.com/i23233571/f8248bc801931afb.png)
统计某一数据重复的次数
ggplot(newdata1,aes(x=Deaths))+geom_histogram(binwidth = 0.5)
![](https://img.haomeiwen.com/i23233571/24dfb21a74147f3d.png)
洋气版的直方图画法,可以通过binwidth这个变量控制直方图的宽度
5.箱型图
ggplot(newdata1,aes(x=Province.State,y=Confirmed))+geom_boxplot()
![](https://img.haomeiwen.com/i23233571/2c9740e4faa122aa.png)
6.绘制函数
myfun<-function(x){
1/(1+exp(-x+10))
}
curve(myfun(x),from = 0,to=20)
curve(1-myfun(x),add=TRUE,col="red")
![](https://img.haomeiwen.com/i23233571/cc5801a332c66ec9.png)
完结,明天更条形图和直线图进阶
撒花