ggplot2在线学习:STHDA :Statistical tools for high-throughput data analysis.
ggplot2使用说明:https://ggplot2.tidyverse.org/reference/
qplot
英文:http://www.sthda.com/english/wiki/qplot-quick-plot-with-ggplot2-r-software-and-data-visualization
根据说明文档,运行代码……
#The data set mtcars is used in the examples below:
data(mtcars)
head(mtcars)
df <- mtcars[, c("mpg","cyl","wt")]
head(df)
#The plot can be created using data from either numeric vectors or a data frame:
# Use data from numeric vectors
x <- 1:10; y = x*x
# Basic plot
qplot(x,y)
# Add line
qplot(x, y, geom=c("point", "line"))
# Use data from a data frame
qplot(mpg, wt, data = mtcars)
#Scatter plots with smoothed line
# Smoothing
qplot(mpg, wt, data = mtcars, geom = c("point", "smooth"))
#The argument color is used to tell R that we want to color the points by groups:
# Linear fits by group
qplot(mpg, wt, data = mtcars, color = factor(cyl), geom = c("point","smooth"))
# Change the color by a continuous numeric variable
qplot(mpg, wt, data = mtcars, color = cyl)
# Change the color by groups (factor)
qplot(mpg, wt, data = mtcars, color = factor(cyl))
qplot(mpg, wt, data = mtcars, color = factor(cyl), geom = c("point","line"))
# Change the size of points according to
# the values of a continuous variable
qplot(mpg, wt, data = mtcars, size = mpg)
# Change point shapes by groups
qplot(mpg, wt, data = mtcars, shape = factor(cyl))
#Scatter plot with texts
qplot(mpg, wt, data = mtcars, label = rownames(mtcars),
geom = c("point", "text"),)
#hjust 和 vjust 的值仅定义在0和1之间:#0表示左对齐 #1表示右对齐
qplot(mpg, wt, data = mtcars, label = rownames(mtcars),
geom=c("point", "text"),
hjust=0, vjust=0)
#Box plot, dot plot and violin plot
head(PlantGrowth)
x <- 1
y <- rnorm(100)
qplot(x, y, geom = "boxplot")
qplot(group, weight, data = PlantGrowth, geom = "boxplot")
#Dot plot
qplot(group, weight, data = PlantGrowth)
qplot(group, weight, data = PlantGrowth, geom = "dotplot", stackdir = "center", binaxis = "y")
#violin plot
qplot(group, weight, data = PlantGrowth, geom = "violin", trim = FALSE)
#add jitter and change fill color by group
qplot(group, weight, data = PlantGrowth, geom = c("boxplot", "jitter"), fill = group)
#dot plot
qplot(group, weight, data = PlantGrowth,
geom = "dotplot", stackdir = "center", binaxis = "y",
color = group, fill = group)
#The histogram and density plots are used to display the distribution of data.
set.seed(1234)
mydata <- data.frame(
sex = factor(rep(c("F","M"), each = 200)),
weight = c(rnorm(200, 55), rnorm(200, 58))
)
#notice the difference : rep(c("1","2"),each = 5) / rep(c("1","2"), 5)
head(mydata)
#basic histogram
#the histogram and density plots are used to display the distributin of data
qplot(weight, data = mydata, geom = "histogram")
qplot(weight, data = mydata, geom = "histogram", color = sex)
qplot(weight, data = mydata, geom = "histogram", fill = sex)
#basic density plot
qplot(weight, data = mydata, geom = "density")
#change the density plot line color by group(sex) \ change the line type
qplot(weight, data = mydata, geom = "density", color = sex, linetype = sex)
#main title and axis labels
qplot(weight, data = mydata, geom = "density",
xlab = "Weight(kg)", ylab = "Density",
main = "Density plot of Weight(kg)")