ggplot2-qplot基本用法
install.packages("ggplot2")
library(ggplot2)
散点图:
plot(mtcars$wt,mtcars$mpg)
线图:
plot(pressure$temperature,pressure$pressure,type="l")
points(pressure$temperature,pressure$pressure)
lines(pressure$temperature,pressure$pressure/2,type="l",col="red")
points(pressure$temperature,pressure$pressure/2,col="red")
Bar图
barplot(table(mtcars$cyl))
Hist图
hist(mtcars$mpg,breks=10)
hist(mtcars$cyl,breks=10)
Box plot箱线图
boxplot(mtcars$mpg)
curve(x^2-5*x,from-4,to-4)
myfun<-function(xvar){1/(1+exp(-xvar+10))}
curve(myfun(x),from=0,to=20)
dat=diamonds
qplot(carat,price,data=diamonds)
qplot(log(carat),log(price),data=diamonds)
qplot(carat,x*y*z,data=diamonds)
#colour,size,shape
dsmall=diamonds[sample(nrow(diamonds),100),]
qplot(carat,price,data=dsmall,colour=color)
qplot(carat,price,data=dsmall,shape=cut)
#make a semi-transparent colour
qplot(carat,price,data=diamonds,alpha=I(1/100))
qplot(carat,price,data=diamonds,alpha=I(1/100))
qplot(carat,price,data=diamonds,alpha=I(1/200))
#
library(splines)
qplot(carat,price,data=dsmall,geom=c("point","smooth"),method="lm")
qplot(carat,price,data=dsmall,geom=c("point","smooth"),method="lm",formula=y~ns(x,5))
#
color,fill
qplot(color,price,data=dsmall,geom="boxplot")
qplot(color,price,data=dsmall,geom="boxplot",fill="blue")
qplot(color,price,data=dsmall,geom="boxplot",fill=blue)
qplot(color,price,data=dsmall,geom="boxplot",size=2)#size线的粗细
qplot(color,price,data=dsmall,geom="boxplot")+geom_boxplot(outlier.colour="green",outlier.size=10,fill="red",colour="blue",size=2)
qplot(carat,data=diamonds,geom="histogram",colour=color)
qplot(carat,data=diamonds,geom="density")
qplot(carat,data=diamonds,geom="histogram",fill=color)