首先看下函数的所有参数
gghistogram(data, x, y = "..count..", combine = FALSE, merge = FALSE, color = "black", fill = NA, palette = NULL, size = NULL, linetype = "solid", alpha = 0.5, bins = NULL, binwidth = NULL, title = NULL, xlab = NULL, ylab = NULL, facet.by = NULL, panel.labs = NULL, short.panel.labs = TRUE,add = c("none", "mean", "median"), add.params = list(linetype = "dashed"), rug = FALSE, add_density = FALSE, label = NULL, font.label = list(size = 11, color = "black"), label.select = NULL, repel = FALSE, label.rectangle = FALSE, ggtheme = theme_pubr(), ...)
以下为各参数的定义
data 所需的数据框(dataframe)
x x轴作图所需的数据
y 设置为密度或count数("..density.." or "..count..")
combine 对于多个变量的数据是否分面。逻辑值,默认是FALSE。
merge 对于多个变量数据是否合并,默认是FALSE
color, fill 线条颜色与填充色
palette 自定义颜色画板
size 设置点和轮廓的大小
linetype 线条类型
alpha 透明度设置
bins bin(x轴的区间)的个数,默认最高到30
binwidth bin的宽度,数值在(0,1)
title 设置标题
xlab 设置x轴标题
ylab 设置y轴标题
facet.by 设置分组分面
panel.labs 设置分面各组的标题
short.panel.labs 是否缩写分面标题,逻辑值,默认是TRUE。
add 添加均值或中位数线("mean" or "median")
add.params 给add参数的对象添加其他参数/属性
rug 是否添加边际线
add_density 是否添加密度曲线
label 设置列标签
font.label 设置标签字体
repel 逻辑值,是否使用ggrepel避免字体重叠
label.rectangle 是否给标签添加方框
ggtheme 设置画图主题
这次我们用mtcars数据集进行模拟,数据集详细情况见下连接
https://vincentarelbundock.github.io/Rdatasets/doc/datasets/mtcars.html
具体解释见下
A data frame with 32 observations on 11 (numeric) variables.
[, 1] mpg Miles/(US) gallon
[, 2] cyl Number of cylinders
[, 3] disp Displacement (cu.in.)
[, 4] hp Gross horsepower
[, 5] drat Rear axle ratio
[, 6] wt Weight (1000 lbs)
[, 7] qsec 1/4 mile time
[, 8] vs Engine (0 = V-shaped, 1 = straight)
[, 9] am Transmission (0 = automatic, 1 = manual)
[,10] gear Number of forward gears
[,11] carb Number of carburetors
library(ggpubr)
data(mtcars)
df <- mtcars
df$vs <- as.factor(df$vs)
str(df)
展示数据集
gghistogram(df, #数据集
x="disp", #x轴选定
add = "mean", #添加平均值线
rug = TRUE, #添加地毯线
fill = "lightgray")#确定填充颜色
初步图形
gghistogram(df, x="disp", #确实数据集及X坐标
add = "mean",
color="vs",#分组颜色
rug = TRUE)
按照vs添加分组颜色
gghistogram(df,
x="disp",
add = "mean",
rug = TRUE,
fill = "vs",#设置填充颜色
color = "vs",#设置边框颜色
palette = "jco",#设置色板为jco
bins = 32)#设置X轴区间个数
设置边框及填充颜色并设置区间个数
gghistogram(df,
x="disp",
y = "..density..",#将y设定为密度比例
add = "mean",
rug = TRUE,
fill = "vs",#设置填充颜色
color = "vs",#设置边框颜色
palette = "jco",#设置色板为jco
bins = 32,#设置X轴区间个数
add_density = T)#添加密度线
将y设定为密度比例并添加密度线
gghistogram(df,
x="disp",
y = "..density..",
facet.by = "vs", #设定分面分组依据
panel.labs = list(vs = c("vs0", "vs1")),#设定分面分组名称
add ="mean",
rug = TRUE,
fill ="vs",
palette = c("#00AFBB", "#E7B800"),
add_density =TRUE,
bins = 32)
设置分面并依据分面设置颜色
网友评论