前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言之可视化⑥R图形系统续目录

R语言之可视化⑥R图形系统续目录

作者头像
用户1359560
发布2018-12-12 15:42:48
1.9K0
发布2018-12-12 15:42:48
举报
文章被收录于专栏:生信小驿站生信小驿站

目录

R语言之可视化①误差棒
R语言之可视化②点图
R语言之可视化③点图续
R语言之可视化④点韦恩图upsetR
R语言之可视化⑤R图形系统
R语言之可视化⑥R图形系统续

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

ggplot2包中的主要功能是ggplot(),它可用于使用数据和x / y变量初始化绘图系统。 例如,以下R代码将数据集初始化为ggplot,然后将一个图层(geom_point())添加到ggplot上,以创建x = Sepal.Length的散点图y = Sepal.Width:

代码语言:javascript
复制
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分组变量。

代码语言:javascript
复制
# 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()函数手动更改颜色和填充
代码语言:javascript
复制
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代码:

代码语言:javascript
复制
theme_set(
  theme_classic()
)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width))+
  geom_point()

ggpubr用于发布准备好的图

ggpubr R软件包有助于为具有非高级编程背景的研究人员创建基于ggplot2的漂亮图形(Kassambara 2017)。例如,要创建“Sepal.Length”的密度分布,按组(“Species”)着色。

代码语言:javascript
复制
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值:

代码语言:javascript
复制
# 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”),

代码语言:javascript
复制
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()
代码语言:javascript
复制
# 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。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.11.14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
    • 改变颜色形状
      • 分面板
        • 修改主题
          • ggpubr用于发布准备好的图
            • 导出R图形
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档