======================================
ggplot2包中的主要功能是ggplot(),它可用于使用数据和x / y变量初始化绘图系统。 例如,以下R代码将数据集初始化为ggplot,然后将一个图层(geom_point())添加到ggplot上,以创建x = Sepal.Length的散点图y = Sepal.Width:
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
geom_point()
# Change point size, color and shape
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
geom_point(size = 1.2, color = "steelblue", shape = 21)
也可以通过分组变量(此处为Species)控制点的形状和颜色。 例如,在下面的代码中,我们将点颜色和形状映射到Species分组变量。
# Control points color by groups
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
geom_point(aes(color = Species, shape = Species))
# Change the default color manually.
# Use the scale_color_manual() function
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
geom_point(aes(color = Species, shape = Species))+
scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
您还可以根据分组变量将绘图拆分为多个面板。 R函数:facet_wrap()。 ggplot2的另一个有趣特性是可以在同一个图上组合多个图层。 例如,使用以下R代码,我们将:
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
geom_point(aes(color = Species))+
geom_smooth(aes(color = Species, fill = Species))+
facet_wrap(~Species, ncol = 3, nrow = 1)+
scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))+
scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))
请注意,ggplots的默认主题是theme_gray()(或theme_grey()),它是具有灰色背景和白色网格线的主题。 更多主题可用于专业演示或出版物。 这些包括:theme_bw(),theme_classic()和theme_minimal()。
要更改给定ggplot(p)的主题,请使用:p + theme_classic()。 要在整个R会话期间将所有ggplots的默认主题更改为theme_classic(),请键入以下R代码:
theme_set(
theme_classic()
)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
geom_point()
ggpubr R软件包有助于为具有非高级编程背景的研究人员创建基于ggplot2的漂亮图形(Kassambara 2017)。例如,要创建“Sepal.Length”的密度分布,按组(“Species”)着色。
library(ggpubr)
# Density plot with mean lines and marginal rug
ggdensity(iris, x = "Sepal.Length",
add = "mean", rug = TRUE, # Add mean line and marginal rugs
color = "Species", fill = "Species", # Color by groups
palette = "jco") # use jco journal color palette
image.png
创建一个箱形图,并且比较不同组P值:
# Groups that we want to compare
my_comparisons <- list(
c("setosa", "versicolor"), c("versicolor", "virginica"),
c("setosa", "virginica")
)
# Create the box plot. Change colors by groups: Species
# Add jitter points and change the shape by groups
ggboxplot(
iris, x = "Species", y = "Sepal.Length",
color = "Species", palette = c("#00AFBB", "#E7B800", "#FC4E07"),
add = "jitter"
)+
stat_compare_means(comparisons = my_comparisons, method = "t.test")
可以将R图形导出为多种文件格式,包括:PDF,PostScript,SVG矢量文件,WindowsMetaFile(WMF),PNG,TIFF,JPEG等。
从R保存任何图形的标准程序如下:
pdf(“r-graphics.pdf”), postscript(“r-graphics.ps”), svg(“r-graphics.svg”), png(“r-graphics.png”), tiff(“r-graphics.tiff”), jpeg(“r-graphics.jpg”), win.metafile(“r-graphics.wmf”),
pdf("r-base-plot.pdf")
# Plot 1 --> in the first page of PDF
plot(x = iris$Sepal.Length, y = iris$Sepal.Width)
# Plot 2 ---> in the second page of the PDF
hist(iris$Sepal.Length)
dev.off()
# Create some plots
library(ggplot2)
myplot1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point()
myplot2 <- ggplot(iris, aes(Species, Sepal.Length)) +
geom_boxplot()
# Print plots to a pdf file
pdf("ggplot.pdf")
print(myplot1) # Plot 1 --> in the first page of PDF
print(myplot2) # Plot 2 ---> in the second page of the PDF
dev.off()
请注意,对于ggplot,还可以使用以下函数导出图形: ggsave()[在ggplot2中]。 保存ggplot很容易。 它从文件扩展名中猜出图形设备的类型。 ggexport()[在ggpubr中]。 一次安排和导出多个ggplots。