##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
# 偏差图绘制(Deviation graphs),偏差图展示了与参考值之间的偏差。df2$mpg_z <- (df2$mpg-mean(df2$mpg))/sd(df2$mpg)
# 相当于Zscore标准化,减均值,除标准差df2$mpg_grp <-factor(ifelse(df2$mpg_z<0, "low", "high"),
levels =c("low", "high"))
#棒棒糖偏差图p12 <-ggdotchart(df2, x ="name", y ="mpg_z",
color ="cyl", # Color by groups palette =c("#00AFBB", "#E7B800", "#FC4E07"),
# Custom color palette sorting ="descending", # Sort value in descending order add ="segments", # Add segments from y = 0 to dots add.params =list(color ="lightgray", size =2),
# Change segment color and size group ="cyl", # Order by groups dot.size =6, # Large dot size label =round(df2$mpg_z,1),
# Add mpg values as dot labels,设置一位小数 font.label =list(color ="white", size =9, vjust =0.5),
# Adjust label parameters ggtheme =theme_pubr()) +geom_hline(yintercept =0, linetype =2,
color ="lightgray")
ggplotly(p12)
## Warning: `gather_()` was deprecated in tidyr 1.2.0.
## ℹ Please use `gather()` instead.
## ℹ The deprecated feature was likely used in the plotly package.
## Please report the issue at <]8;;https://github.com/plotly/plotly.R/issueshttps://github.com/plotly/plotly.R/issues]8;;>.
2.7Cleveland点图绘制
library(ggpubr)
library(ggpubr)
# 加载数据集data("mtcars")
df2 <- mtcars
df2$cyl <-factor(df2$cyl)
df2$name <-rownames(df2) #添加一新列name# 偏差图绘制(Deviation graphs),偏差图展示了与参考值之间的偏差。df2$mpg_z <- (df2$mpg-mean(df2$mpg))/sd(df2$mpg)
# 相当于Zscore标准化,减均值,除标准差df2$mpg_grp <-factor(ifelse(df2$mpg_z<0, "low", "high"),
levels =c("low", "high"))
# theme_cleveland()主题可设置为Cleveland点图样式p13 <-ggdotchart(df2, x ="name", y ="mpg",
color ="cyl", # Color by groups palette =c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette sorting ="descending", # Sort value in descending order rotate =TRUE, # Rotate vertically dot.size =2, # Large dot size y.text.col =TRUE, # Color y text by groups ggtheme =theme_pubr() # ggplot2 theme ) +theme_cleveland() # Add dashed grids
## Warning: Vectorized input to `element_text()` is not officially supported.
## Results may be unexpected or may change in future versions of ggplot2.
p13
3.导出图片
library(ggpubr)
library(datasets)
# Load datadata(ToothGrowth)
df <- ToothGrowth
df$dose <-as.factor(df$dose)
# Box plotbxp <-ggboxplot(df, x ="dose", y ="len",
color ="dose", palette ="jco")
# Dot plotdp <-ggdotplot(df, x ="dose", y ="len",
color ="dose", palette ="jco")
# Density plotdens <-ggdensity(df, x ="len", fill ="dose", palette ="jco")
# Export to pdf#ggarrange(bxp, dp, dens, ncol = 2) %>%# ggexport(filename = "test.pdf")# Export to png#ggarrange(bxp, dp, dens, ncol = 2) %>%#ggexport(filename = "test.png")
4.常用基本绘图函数及参数
4.1基本绘图函数
#gghistogram Histogram plot #绘制直方图#ggdensity Density plot #绘制密度图#ggdotplot Dot plot #绘制点图#ggdotchart Cleveland's Dot Plots #绘制Cleveland点图#ggline Line plot #绘制线图#ggbarplot Bar plot #绘制条形/柱状图#ggerrorplot Visualizing Error #绘制误差棒图#ggstripchart Stripcharts #绘制线带图#ggboxplot Box plot #绘制箱线图#ggviolin Violin plot #绘制小提琴图#ggpie Pie chart #绘制饼图#ggqqplot QQ Plots #绘制QQ图#ggscatter Scatter plot #绘制散点图#ggmaplot MA-plot from means and log fold changes #绘制M-A图#ggpaired Plot Paired Data #绘制配对数据#ggecdf Empirical cumulative density function #绘制经验累积密度分布图
4.2基本参数
# ggtext Text #添加文本# border Set ggplot Panel Border Line #设置画布边框线# grids Add Grids to a ggplot #添加网格线# font Change the Appearance of Titles and Axis Labels #设置字体类型# bgcolor Change ggplot Panel Background Color #更改画布背景颜色# background_image Add Background Image to ggplot2 #添加背景图片# facet Facet a ggplot into Multiple Panels #设置分面# ggpar Graphical parameters #添加画图参数# ggparagraph Draw a Paragraph of Text #添加文本段落# ggtexttable Draw a Textual Table #添加文本表格# ggadd Add Summary Statistics or a Geom onto a ggplot #添加基本统计结果或其他几何图形# ggarrange Arrange Multiple ggplots #排版多个图形# annotate_figure Annotate Arranged Figure #添加注释信息# gradient_color Set Gradient Color #设置连续型颜色# xscale Change Axis Scale: log2, log10 and more #更改坐标轴的标度# add_summary Add Summary Statistics onto a ggplot #添加基本统计结果# set_palette Set Color Palette #设置画板颜色# rotate Rotate a ggplot Horizontally #设置图形旋转# rotate_axis_text Rotate Axes Text #旋转坐标轴文本# stat_stars Add Stars to a Scatter Plot #添加散点图星标# stat_cor Add Correlation Coefficients with P-values to a Scatter Plot #添加相关系数# stat_compare_means Add Mean Comparison P-values to a ggplot #添加平均值比较的P值# diff_express Differential gene expression analysis results #内置差异分析结果数据集# ggexport Export ggplots # 导出图片# theme_transparent Create a ggplot with Transparent Background #设置透明背景# theme_pubr Publication ready theme #设置出版物主题