工具:R+ggplot2工具包
ggplot画图比较简单就直接上代码了
library(ggplot2) #加载工具包
mtcars #使用R的数据集
plot(mtcars$mpg,mtcars$drat) #将mpg和drat按照(x,y)做散点图
plot(mtcars$mpg,mtcars$drat,type = ‘b’) #将mpg和drat按照(x,y)做线图,
其中,type有以下赋值:
点"p" for points,
线"l" for lines,
点线"b" for both,
点线图去掉点"c" for the lines part alone of "b",
覆盖式的电线"o" for both ‘overplotted’,
类似直方图"h" for ‘histogram’ like (or ‘high-density’) vertical lines,
楼梯状"s" for stair steps,
楼梯状"S" for other steps, see ‘Details’ below,
不显示"n" for no plotting.
qplot(mtcars$mpg,mtcars$drat)
ggplot(mtcars,aes(mpg,drat)) + geom_point() #散点图
用R中自带数据集Pressure
plot(pressure$temperature,pressure$pressure,type = 'b')
qplot(pressure$temperature,pressure$pressure,geom = c("line","point"))
ggplot(pressure,aes(temperature,pressure)) + geom_point() +geom_line()
mtcars
p<-ggplot(mtcars,aes(mpg,drat,colour = factor(carb))) + geom_point(aes(size =gear))
p + annotate("text",x = 20,y = 4,label = "hjhjhjh") + geom_hline(yintercept = 4) + geom_vline(xintercept = 20)
# 按照gear大小分类,按照carb进行颜色区分
#做直线x=20,y=4,并在(20,4)处标记“hjhjhjh”
p + facet_grid(vs ~ .) #以VS为分类变量
p + facet_grid(. ~ vs) #以VS为分类变量
p + facet_grid(vs ~ am, scales = "free") #以vs和am为分类变量
p + coord_flip() #将X,Y相互颠倒
p1 <- p + ylim(3.5,5) + xlim(15,30) #限定坐标轴的范围
p2 <- p1 + scale_x_continuous(breaks = c(15,20,25,30),labels = c("A","B","C","D"))
p2 + scale_y_continuous(breaks = c(3.5,4.0,4.5,5.0),labels = c("A","B","C","D"))
#将X和Y轴的轴标签改为A,B,C,D
暂时写到这里,如有错误欢迎指正。