R語言之可視化⑥R圖形系統續目錄

目錄

R語言之可視化①誤差棒

R語言之可視化②點圖

R語言之可視化③點圖續

R語言之可視化④點韋恩圖upsetR

R語言之可視化⑤R圖形系統

R語言之可視化⑥R圖形系統續

======================================

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代碼,我們將:

  • 使用geom_point()添加點,按組着色。
  • 使用geom_smooth()添加擬合的平滑迴歸線。 默認情況下,函數geom_smooth()添加迴歸線和置信- 區域。
  • 按小組將圖片分成多個面板
  • 使用scale_color_manual()和scale_fill_manual()函數手動更改顏色和填充
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用於發佈準備好的圖

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圖形

可以將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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章