1、Basic use
#import data
library(ggplot2)
dat <- diamonds
qplot(carat,price,data = dat)
qplot(log(carat),log(price),data = dat)
qplot(carat, x * y * z, data = dat)
2、Coulour、size、shape and other aesthetic attributes
dsmall <- diamonds[sample(nrow(diamonds), 100), ] #select 100 samples
qplot(carat, price, data = dsmall, colour = color) # colour can be replaced by color
qplot(carat, price, data = dsmall, colour = color, shape = cut)
#set a semi-transparent color by "alpha", from 0 to 1
qplot(carat,price,data = dat, alpha = I(1/10))
qplot(carat,price,data = dat, alpha = I(1/100))
qplot(carat,price,data = dat, alpha = I(1/200))
> qplot(carat, price, data = dsmall, geom = c("point","smooth"))
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
library(splines)
qplot(carat, price, data = dsmall, geom = c("point","smooth"), method = "lm") #use "method" to change regression model
qplot(carat, price, data = dsmall, geom = "boxplot")
#fill用于填充色,用I()指定颜色
qplot(carat, price, data = dsmall, geom = "boxplot", fill = I("blue"))
#fill用于填充色,此处fill = "blue"相当于直接指定一个向量,类似前面的colour = color,而最终颜色为红色是因为红色为默认颜色
qplot(carat, price, data = dsmall, geom = "boxplot", fill = "blue")
#colour用于外部线条颜色,用I()指定颜色
qplot(carat, price, data = dsmall, geom = "boxplot", colour = I("blue"))
#colour用于外部线条颜色,此处colour = "blue"相当于直接指定一个向量,类似前面的colour = color,而最终颜色为红色是因为红色为默认颜色
qplot(carat, price, data = dsmall, geom = "boxplot", colour = "blue")
qplot(color, price,
data = dsmall,
geom = "boxplot") + geom_boxplot(outlier.colour = "green",
outlier.size = 10,
fill = "blue",
colour = "green",
size = 2)
#outlier.colour表示外部点的颜色;outlier.size表示外部点的大小;
#在geom_boxplot()函数中可以通过fill = "blue"和colour = "green"直接指定填充色和线条颜色;size表示线条的大小
qplot(carat, data = diamonds, geom = "histogram", colour = color)
qplot(carat,data = diamonds, geom = "density")
qplot(carat,data = diamonds, geom = "density", colour = color)
qplot(carat,data = diamonds, geom = "density", fill = color)
qplot(color,data = diamonds, geom = "bar", fill = color)
dat2 <- economics
qplot(date, unemploy/pop, data = dat2, geom = "line") #unemploy/pop失业率随时间的变化
qplot(date, uempmed, data = dat2, geom = "line") #uempmed失业人口随时间的变化
qplot(unemploy/pop, uempmed, data = dat2, geom = c("point","path"))
#失业率和失业人口随时间的一个相关的走势
year <- function(x) as.POSIXlt(x)$year + 1900
qplot(unemploy/pop, uempmed, data = dat2, geom = "path", colour = year(date))
欢迎关注微信公众号:BioLearner