使用书籍:R数据可视化手册
快速索引
1.散点图
qplot(mydata$Confirmed,mydata$Deaths)
ggplot(mydata,aes(x=Confirmed,y=Deaths))+geom_point()
推荐使用后者

2.折线图
ggplot(mydata,aes(x=Confirmed,y=Deaths))+geom_line()
ggplot(mydata,aes(x=Confirmed,y=Deaths))+geom_line()+geom_point()#后者加了数据点
图分别为一下所示


3.条形图
barplot(table(mydata$Country))
统计一列数据的频数

barplot(newdata$Deaths,names.arg=newdata$Province.State)
第一个参数设定高度,第二个参数对应标签

ggplot(a,aes(x=time,y=demand))+geom_bar(stat="identity")

ggplot(a,aes(x=factor(time),y=demand))+geom_bar(stat="identity")
绘图函数里的stat参数表示对样本点做统计的方式,默认为identity,表示一个x对应一个y

以上两者的区别为是否将横坐标作为因子来看待
4.直方图
qplot(newdata1$Deaths)

统计某一数据重复的次数
ggplot(newdata1,aes(x=Deaths))+geom_histogram(binwidth = 0.5)

洋气版的直方图画法,可以通过binwidth这个变量控制直方图的宽度
5.箱型图
ggplot(newdata1,aes(x=Province.State,y=Confirmed))+geom_boxplot()

6.绘制函数
myfun<-function(x){
1/(1+exp(-x+10))
}
curve(myfun(x),from = 0,to=20)
curve(1-myfun(x),add=TRUE,col="red")

完结,明天更条形图和直线图进阶
撒花
网友评论