R绘图_ggplot2分面Scatter plots
2020-02-28 本文已影响0人
谢俊飞

Scatter plots
根据说明文档,运行代码……
rm(list = ls())
#convert cyl column from a numeric to a factor variable
mtcars$cyl <- as.factor(mtcars$cyl)
head(mtcars)
library(ggplot2)
#basic scatter plot
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
#change the point size, and shape
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(size = 2, shape = 23)
#change the point size
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(aes(size = qsec))
#label points in the scatter plot
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_text(label = rownames(mtcars))
#Add regression lines
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_smooth(method = lm)
#remove the confidence interval
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_smooth(method = lm, se = FALSE)
#Loess method
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_smooth() #by default,method = loess
#change the point colors and shapes
#change the line type and color
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(shape = 18, color = "blue" ) +
geom_smooth(method = lm, se = FALSE, linetype = "dashed", color = "darkred")
#change the confidence interval fill color
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(shape = 18, color = "blue" ) +
geom_smooth(method = lm, linetype = "dashed", color = "darkred", fill = "blue")
#change point shapes by the levels of cyl
ggplot(mtcars, aes(x=wt, y=mpg, shape = cyl)) +
geom_point()
#change point shapes and colors
ggplot(mtcars, aes(x=wt, y=mpg, shape = cyl, color = cyl)) +
geom_point()
#change point shapes, sizes, colors
ggplot(mtcars, aes(x=wt, y=mpg, color = cyl, shape = cyl, size = cyl)) +
geom_point()
#add regresssion lines
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm)
#remove confidence intervals and extend the regression lines
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(aes(linetype = cyl), method=lm, se = FALSE, fullrange = TRUE)
ggplot(mtcars, aes(x=wt, y=mpg, color = cyl, shape = cyl)) +
geom_point() +
geom_smooth(method = lm, aes(fill = cyl))
#change point shapes and colors manually
ggplot(mtcars, aes(x=wt, y=mpg, color = cyl, shape = cyl)) +
geom_point() +
geom_smooth(method = lm, se = FALSE, fullrange = TRUE) +
scale_shape_manual(values = c(3, 16, 17)) +
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9')) +
theme(legend.position = "top")
#change the point sizes manually
ggplot(mtcars, aes(x=wt, y=mpg, color = cyl, shape = cyl)) +
geom_point() +
geom_smooth(method = lm, se = FALSE, fullrange = TRUE) +
scale_shape_manual(values = c(3, 16, 17)) +
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9')) +
scale_size_manual(values = c(2, 3, 4))
theme(legend.position = "top")
p <- ggplot(mtcars, aes(x=wt, y=mpg, color = cyl, shape = cyl)) +
geom_point() +
geom_smooth(method = lm, se = FALSE, fullrange = TRUE) +
theme_classic()
#use brewer color palettes
p + scale_color_brewer(palette = "Dark2")
#use the grey scale
p + scale_color_grey()
#add marginal rugs将边缘地毯添加到散布图
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() + geom_rug()
# Change colors
ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) +
geom_point() + geom_rug()
# Add marginal rugs using faithful data
ggplot(faithful, aes(x=eruptions, y=waiting)) +
geom_point() + geom_rug()
#scatter plot with the 2d density estimation
sp <- ggplot(faithful, aes(x=eruptions, y=waiting)) +
geom_point()
sp + geom_density2d()
# Gradient color
sp + stat_density2d(aes(fill = ..level..), geom = "polygon")
#change the gradient color
sp + stat_density2d(aes(fill = ..level..), geom = "polygon") +
scale_fill_gradient(low = "blue", high = "red")
#one ellipse arround all points
ggplot(faithful, aes(waiting, eruptions))+
geom_point()+
stat_ellipse()
#ellipse by groups
p <- ggplot(faithful, aes(waiting, eruptions, color = eruptions >3))+
geom_point()
p + stat_ellipse()
#change the type of ellipses: possible values are "t","norm","euclid"
p + stat_ellipse(type = "norm")
#Scatter plots with rectangular bins
head(diamonds)
p <- ggplot(diamonds, aes(carat, price))
p + geom_bin2d()
#change the number of bins
p + geom_bin2d(bins = 10)
#or specify the width of bins
p + geom_bin2d(binwidth = c(1, 1000))
#Scatter plot with marginal density distribution plot
set.seed(1234)
x <- c(rnorm(500, mean = -1), rnorm(500, mean = 1.5))
y <- c(rnorm(500, mean = 1), rnorm(500, mean = 1.7))
group <- as.factor(rep(c(1,2), each=500))
df <- data.frame(x, y, group)
head(df)
# scatter plot of x and y variables
# color by groups
scatterPlot <- ggplot(df,aes(x, y, color=group)) +
geom_point() +
scale_color_manual(values = c('#999999','#E69F00')) +
theme(legend.position=c(0,1), legend.justification=c(0,1))
scatterPlot
# Marginal density plot of x (top panel)
xdensity <- ggplot(df, aes(x, fill=group)) +
geom_density(alpha=.5) +
scale_fill_manual(values = c('#999999','#E69F00')) +
theme(legend.position = "none")
xdensity
# Marginal density plot of y (right panel)
ydensity <- ggplot(df, aes(y, fill=group)) +
geom_density(alpha=.5) +
scale_fill_manual(values = c('#999999','#E69F00')) +
theme(legend.position = "none")
ydensity
#创建一个空白的占位符图:
blankPlot <- ggplot()+geom_blank(aes(1,1))+
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank()
)
library(gridExtra)
grid.arrange(xdensity, blankPlot, scatterPlot, ydensity,
ncol=2, nrow=2, widths=c(4, 1.4), heights=c(1.4, 4))
#Customized scatter plots
# Basic scatter plot
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
geom_smooth(method=lm, color="black")+
labs(title="Miles per gallon \n according to the weight",
x="Weight (lb/1000)", y = "Miles/(US) gallon")+
theme_classic()
# Change color/shape by groups
# Remove confidence bands
p <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point()+
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
labs(title="Miles per gallon \n according to the weight",
x="Weight (lb/1000)", y = "Miles/(US) gallon")
p + theme_classic()
# Continuous colors
p + scale_color_brewer(palette="Paired") + theme_classic()
# Discrete colors
p + scale_color_brewer(palette="Dark2") + theme_minimal()
# Gradient colors
p + scale_color_brewer(palette="Accent") + theme_minimal()