推广

R语言三大绘图R包之二—ggpubr的使用介绍

iseeyu3年前 (2024-02-21)推广186

image

2.2 Histogram plot

# Histogram plot with mean lines and marginal rug
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline and fill colors by groups ("sex")
# Use custom color palette
gghistogram(wdata, x = "weight",
   add = "mean", rug = TRUE,
   color = "sex", fill = "sex",
   palette = c("#00AFBB", "#E7B800"))

参数与Density plots类似,便不再赘述。

image

2.3 Box plots and violin plots

# Load data
data("ToothGrowth")
df <- ToothGrowth
head(df, 4)
   len supp dose
1  4.2   VC  0.5
2 11.5   VC  0.5
3  7.3   VC  0.5
4  5.8   VC  0.5

# Box plots with jittered points
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline colors by groups: dose
# Use custom color palette
# Add jitter points and change the shape by groups
p <- ggboxplot(df, x = "dose", y = "len",
               color = "dose", palette =c("#00AFBB", "#E7B800", "#FC4E07"),
               add = "jitter", shape = "dose")
p

image

做什么类型的图,都要把参数调成示例的模样,然后再进一步地修改,上述参数意义为以dose为横坐标,len为纵坐标,根据不同的dose填充不同的颜色;add = "jitter"表示在boxplot上添加跳动的点,表示数据的分布;shape = "dose"表示点的形状依据dose。

给横坐标分组添加P值

# Add p-values comparing groups
 # Specify the comparisons you want
my_comparisons <- list( c("0.5", "1"), c("1", "2"), c("0.5", "2") )
p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
  stat_compare_means(label.y = 50)                   # Add global p-value

my_comparisons表示添加比较的分组;label.y表示总的P值显示的位置。若去除最后一个stat_compare_means,则不显示整体的P值。

image

小提琴图

# Violin plots with box plots inside
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change fill color by groups: dose
# add boxplot with white fill color
ggviolin(df, x = "dose", y = "len", fill = "dose",
         palette = c("#00AFBB", "#E7B800", "#FC4E07"),
         add = "boxplot", add.params = list(fill = "white"))+
  stat_compare_means(comparisons = my_comparisons, label = "p.signif")+ # Add significance levels
  stat_compare_means(label.y = 50)                                      # Add global the p-value 

image

label中P值有两种表示方式,Allowed values include “p.signif” (shows the significance levels), “p.format” (shows the formatted p value),默认是第二种方式。其中,add = "boxplot"表示在小提琴图上添加箱线图,add.params表示add添加图形的参数,此处为添加白色的箱线图。

2.4 Bar plots

2.4.1Ordered bar plots
data("mtcars")

df2 <- mtcars

df2$cyl <- factor(df2$cyl)

df2$name <- rownames(df2) #添加一行name

head(df2[, c("name", "wt", "mpg", "cyl")])
df2<-dfm

ggbarplot(dfm, x = "name", y = "mpg",
          fill = "cyl",               # change fill color by cyl
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "desc",          # Sort the value in dscending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90           # Rotate vertically x axis texts
)

image

fill = "cyl",表示颜色填充按另一个分组来;color = "white"改变柱子的边界,即无色;sort.val = "desc"表示变量按降序排列,其他的选择为 Allowed values are “none” (no sorting), “asc” (for ascending) or “desc” (for descending);sort.by.groups = FALSE表示分好的组并不聚集在一起,若为T,则聚集在一起; x.text.angle = 90X轴的名称逆时针旋转90度。

2.4.2 分组内部排序的barplot

通过设置sort.by.groups = TRUE。

ggbarplot(dfm, x = "name", y = "mpg",
          fill = "cyl",               # change fill color by cyl
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "asc",           # Sort the value in dscending order
          sort.by.groups = TRUE,      # Sort inside each group
          x.text.angle = 90           # Rotate vertically x axis texts
          )

image

2.4.3 Deviation graphs

偏差图显示了定量值与参考值的偏差,下面的代码中,我们将从mtcars数据集绘制mpg z-score。
Calculate the z-score of the mpg data:
因子的设定值得学习,因子里面夹带了一个判断语句。

# Calculate the z-score of the mpg data
dfm$mpg_z <- (dfm$mpg -mean(dfm$mpg))/sd(dfm$mpg)
dfm$mpg_grp <- factor(ifelse(dfm$mpg_z < 0, "low", "high"), 
                     levels = c("low", "high"))
# Inspect the data
head(dfm[, c("name", "wt", "mpg", "mpg_z", "mpg_grp", "cyl")])

创造一个有序的barplot,并且根据mpg因子的水平上色。

ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",           # change fill color by mpg_level
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "asc",           # Sort the value in ascending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = "MPG z-score",
          xlab = FALSE,
          legend.title = "MPG Group"
          )

image

legend.title对legend添加名称。

旋转图像。use rotate = TRUE and sort.val = “desc”

ggbarplot(dfm, x = "name", y = "mpg_z",
          fill = "mpg_grp",           # change fill color by mpg_level
          color = "white",            # Set bar border colors to white
          palette = "jco",            # jco journal color palett. see ?ggpar
          sort.val = "desc",          # Sort the value in descending order
          sort.by.groups = FALSE,     # Don't sort inside each group
          x.text.angle = 90,          # Rotate vertically x axis texts
          ylab = "MPG z-score",
          legend.title = "MPG Group",
          rotate = TRUE,
          ggtheme = theme_minimal()
          )

image

2.5 Dot charts

Lollipop chart(棒棒糖图)
当您有大量的值要可视化时,棒棒图是柱状图的另一种选择。

2.5.1 一般棒棒糖图

ggdotchart(dfm, x = "name", y = "mpg",
           color = "cyl",                                # Color by groups
           palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
           sorting = "ascending",                        # Sort value in descending order
           add = "segments",                             # Add segments from y = 0 to dots
           ggtheme = theme_pubr()                        # ggplot2 theme
           )

image

add = "segments"添加棒。

2.5.2 水平棒棒糖图

  • Sort in descending order. sorting = “descending”.
  • Rotate the plot vertically, using rotate = TRUE.
  • Sort the mpg value inside each group by using group = “cyl”.
  • Set dot.size to 6.
  • Add mpg values as label. label = “mpg” or label = round(dfm$mpg).
ggdotchart(dfm, x = "name", y = "mpg",
           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
           rotate = TRUE,                                # Rotate vertically
           group = "cyl",                                # Order by groups
           dot.size = 6,                                 # Large dot size
           label = round(dfm$mpg),                        # Add mpg values as dot labels
           font.label = list(color = "white", size = 9, 
                             vjust = 0.5),               # Adjust label parameters
           ggtheme = theme_pubr()                        # ggplot2 theme
           )

image

group=按组进行排序;dot.size设置点的大小;label将值写在点上,其中round()表示取整;font.label设置点的字体。

2.5.3 棒棒糖图类型的偏差图

  • Use y = “mpg_z”
  • Change segment color and size: add.params = list(color = “lightgray”, size = 2)
ggdotchart(dfm, 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(dfm$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()                        # ggplot2 theme
           )+
  geom_hline(yintercept = 0, linetype = 2, color = "lightgray")

image

geom_hline绘制指定的水平线。

2.5.4 Cleveland’s dot plot

说白了就是将Y轴的颜色和图中分组的颜色对应起来。Use y.text.col = TRUE.

image

<meta charset=”utf-8″>

3. 常用基本绘图函数及参数

基本绘图函数

gghistogram Histogram plot #绘制直方图

ggdensity Density plot #绘制密度图

ggdotplot Dot plot #绘制点图

ggdotchart Cleveland’s Dot Plots #绘制Cleveland点图

ggline Line plot #绘制线图

ggbarplot Bar plot #绘制柱状图

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 #绘制散点图矩阵

ggerrorplot Visualizing Error #绘制误差图

基本参数

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 #排版多个图形

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值

theme_transparent Create a ggplot with Transparent Background #设置透明背景

theme_pubr Publication ready theme #设置出版物主题

参考链接:
http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/
链接:https://www.jianshu.com/p/f53a05da7745
参考链接:https://www.jianshu.com/p/43967191bb06

扫描二维码推送至手机访问。

版权声明:本文由西安泽虎代运营发布,如需转载请注明出处。

转载请注明出处https://www.0291.com.cn/post/57080.html

相关文章

西安企业做百度推广,一个月花三千和花三万到底差在哪?

4|最近跟几个西安的老板聊天,发现一个挺有意思的现象:同样做百度推广,有人一个月花三千块,询盘不断;有人一个月砸三万,水花都没见着。问他们区别在哪,大多数人说不清楚,只知道'钱花了,没效果'。 5| 6|百度推广这东西,说复杂也复杂,说简单也简单。核心就一件事:让你的潜在客户搜某个词的时候,你的信息...

我想当策划,大学应该选什么专业?

我想当策划,大学应该选什么专业?

学,广告,传播,公关,这些专业其实都可以。另外也可以学像汉语言文学,以及一些跟文学相关的一些专业,会对你找工作有帮助。但是,你想从事这个这份职业,除了专业的学习之外,更多的是,你要在上学的时候积极参与一些更营销策划相关的社会实践。比如说你可以做一些兼职,比如说帮别人去写稿,...

服装市场营销策略分析,“定位不慎满盘皆输”

服装市场营销策略分析,“定位不慎满盘皆输”

不同的区域需要不同的策略,而不同的营销策略需要不同的销售思路来配合。因此,在研究女装产品销售思路之前,最好先对于市场与营销策略做一次深入地了解。1.市场区域化(market segmentation):目标营销的第一步,是将整个市场拆分成若干个同性质的小市场。2.目标市场的...

晚间悦读丨你的思维方式,决定你的人生高度!

晚间悦读丨你的思维方式,决定你的人生高度!

越长大,越发现,能够决定你人生高度的,往往是你的认知层次。 你的认知在哪个层级,你的人生就处在什么状态。你认知的高度,决定了你人生的高度。 那如何才能升级方式、提升认知能力呢?可以从这三个方面做出改变。 NO. 1 改变态度 有人...

编写项目营销策划方案六大要点

编写项目营销策划方案六大要点

现在商业发展非常迅速,的需求非常的大,但是要做好营销策划是有一定难度的,我把自己做营销策划的一些体会在这里分享给朋友们。营销策划方案六大要点我们先说项目营销策划方案这个话题。大部分的营销策划人都是在企业里工作,大部分项目之初都是来源于一个想法,创始人、老板或者创始团队的一个...

【商场营销策划】如何进行商场营销策划?

【商场营销策划】如何进行商场营销策划?

商场是策划者们关注的永恒话题,那么究竟该如何作好商场营销策划呢?掌握以下这几点营销策略设计原则,至关重要!1.义利兼顾的原则。商场在获取利润的同时要充分考虑到消费者的合法利益和商场的长远利益。在作商场活动营销策略方案的时候应该将这一点作为首要的原则。对商场有利而对顾客不利的...

现在,非常期待与您的又一次邂逅

我们努力让每一部企业宣传片和抖音短视频成为商业大片