小提琴图是连续分布的紧凑显示。它是 geom_boxplot() 和 geom_density() 的混合:小提琴图是镜像密度图,显示方式与箱线图相同。
用法
geom_violin(
  mapping = NULL,
  data = NULL,
  stat = "ydensity",
  position = "dodge",
  ...,
  draw_quantiles = NULL,
  trim = TRUE,
  scale = "area",
  na.rm = FALSE,
  orientation = NA,
  show.legend = NA,
  inherit.aes = TRUE
)
stat_ydensity(
  mapping = NULL,
  data = NULL,
  geom = "violin",
  position = "dodge",
  ...,
  bw = "nrd0",
  adjust = 1,
  kernel = "gaussian",
  trim = TRUE,
  scale = "area",
  na.rm = FALSE,
  orientation = NA,
  show.legend = NA,
  inherit.aes = TRUE
)
参数
- mapping
 - 
由
aes()创建的一组美学映射。如果指定且inherit.aes = TRUE(默认),它将与绘图顶层的默认映射组合。如果没有绘图映射,则必须提供mapping。 - data
 - 
该层要显示的数据。有以下三种选择:
如果默认为
NULL,则数据继承自ggplot()调用中指定的绘图数据。data.frame或其他对象将覆盖绘图数据。所有对象都将被强化以生成 DataFrame 。请参阅fortify()将为其创建变量。将使用单个参数(绘图数据)调用
function。返回值必须是data.frame,并将用作图层数据。可以从formula创建function(例如~ head(.x, 10))。 - position
 - 
位置调整,可以是命名调整的字符串(例如
"jitter"使用position_jitter),也可以是调用位置调整函数的结果。如果需要更改调整设置,请使用后者。 - ...
 - 
其他参数传递给
layer()。这些通常是美学,用于将美学设置为固定值,例如colour = "red"或size = 3。它们也可能是配对的 geom/stat 的参数。 - draw_quantiles
 - 
如果
not(NULL)(默认),则在密度估计的给定分位数处绘制水平线。 - trim
 - 
如果
TRUE(默认),将小提琴的尾部修剪到数据范围。如果FALSE,不要修剪尾巴。 - scale
 - 
如果"area"(默认),则所有小提琴都具有相同的面积(修剪尾部之前)。如果"count",面积将根据观测值的数量按比例缩放。如果"width",所有小提琴都具有相同的最大宽度。
 - na.rm
 - 
如果
FALSE,则默认缺失值将被删除并带有警告。如果TRUE,缺失值将被静默删除。 - orientation
 - 
层的方向。默认值 (
NA) 自动根据美学映射确定方向。万一失败,可以通过将orientation设置为"x"或"y"来显式给出。有关更多详细信息,请参阅方向部分。 - show.legend
 - 
合乎逻辑的。该层是否应该包含在图例中?
NA(默认值)包括是否映射了任何美学。FALSE从不包含,而TRUE始终包含。它也可以是一个命名的逻辑向量,以精细地选择要显示的美学。 - inherit.aes
 - 
如果
FALSE,则覆盖默认美学,而不是与它们组合。这对于定义数据和美观的辅助函数最有用,并且不应继承默认绘图规范的行为,例如borders()。 - geom, stat
 - 
用于覆盖
geom_violin()和stat_ydensity()之间的默认连接。 - bw
 - 
要使用的平滑带宽。如果是数字,则为平滑内核的标准差。如果是字符,则选择带宽的规则,如
stats::bw.nrd()中列出。 - adjust
 - 
多重带宽调整。这使得在仍然使用带宽估计器的同时调整带宽成为可能。例如
adjust = 1/2表示使用默认带宽的一半。 - kernel
 - 
核心。请参阅
density()中的可用内核列表。 
方向
该几何体以不同的方式对待每个轴,因此可以有两个方向。通常,方向很容易从给定映射和使用的位置比例类型的组合中推断出来。因此,ggplot2 默认情况下会尝试猜测图层应具有哪个方向。在极少数情况下,方向不明确,猜测可能会失败。在这种情况下,可以直接使用 orientation 参数指定方向,该参数可以是 "x" 或 "y" 。该值给出了几何图形应沿着的轴,"x" 是您期望的几何图形的默认方向。
美学
geom_violin() 理解以下美学(所需的美学以粗体显示):
- 
x - 
y - 
alpha - 
colour - 
fill - 
group - 
linetype - 
linewidth - 
weight 
在 vignette("ggplot2-specs") 中了解有关设置这些美学的更多信息。
计算变量
这些是由层的 'stat' 部分计算的,可以使用 delayed evaluation 访问。
- 
after_stat(density)
密度估计。 - 
after_stat(scaled)
密度估计,缩放至最大值 1。 - 
after_stat(count)
密度 * 点数 - 对于小提琴图可能没用。 - 
after_stat(violinwidth)
根据面积、计数或恒定最大宽度缩放小提琴图的密度。 - 
after_stat(n)
点数。 - 
after_stat(width)
小提琴边界框的宽度。 
也可以看看
geom_violin() 为示例,stat_density() 为沿 x 轴的数据示例。
例子
p <- ggplot(mtcars, aes(factor(cyl), mpg))
p + geom_violin()
# Orientation follows the discrete axis
ggplot(mtcars, aes(mpg, factor(cyl))) +
  geom_violin()
# \donttest{
p + geom_violin() + geom_jitter(height = 0, width = 0.1)
# Scale maximum width proportional to sample size:
p + geom_violin(scale = "count")
# Scale maximum width to 1 for all violins:
p + geom_violin(scale = "width")
# Default is to trim violins to the range of the data. To disable:
p + geom_violin(trim = FALSE)
# Use a smaller bandwidth for closer density fit (default is 1).
p + geom_violin(adjust = .5)
# Add aesthetic mappings
# Note that violins are automatically dodged when any aesthetic is
# a factor
p + geom_violin(aes(fill = cyl))
p + geom_violin(aes(fill = factor(cyl)))
p + geom_violin(aes(fill = factor(vs)))
#> Warning: Groups with fewer than two data points have been dropped.
p + geom_violin(aes(fill = factor(am)))
# Set aesthetics to fixed value
p + geom_violin(fill = "grey80", colour = "#3366FF")
# Show quartiles
p + geom_violin(draw_quantiles = c(0.25, 0.5, 0.75))
# Scales vs. coordinate transforms -------
if (require("ggplot2movies")) {
# Scale transformations occur before the density statistics are computed.
# Coordinate transformations occur afterwards.  Observe the effect on the
# number of outliers.
m <- ggplot(movies, aes(y = votes, x = rating, group = cut_width(rating, 0.5)))
m + geom_violin()
m +
  geom_violin() +
  scale_y_log10()
m +
  geom_violin() +
  coord_trans(y = "log10")
m +
  geom_violin() +
  scale_y_log10() + coord_trans(y = "log10")
# Violin plots with continuous x:
# Use the group aesthetic to group observations in violins
ggplot(movies, aes(year, budget)) +
  geom_violin()
ggplot(movies, aes(year, budget)) +
  geom_violin(aes(group = cut_width(year, 10)), scale = "width")
}
#> Warning: Removed 53573 rows containing non-finite values (`stat_ydensity()`).
#> Warning: Groups with fewer than two data points have been dropped.
# }
相关用法
- R ggplot2 geom_qq 分位数-分位数图
 - R ggplot2 geom_spoke 由位置、方向和距离参数化的线段
 - R ggplot2 geom_quantile 分位数回归
 - R ggplot2 geom_text 文本
 - R ggplot2 geom_ribbon 函数区和面积图
 - R ggplot2 geom_boxplot 盒须图(Tukey 风格)
 - R ggplot2 geom_hex 二维箱计数的六边形热图
 - R ggplot2 geom_bar 条形图
 - R ggplot2 geom_bin_2d 二维 bin 计数热图
 - R ggplot2 geom_jitter 抖动点
 - R ggplot2 geom_point 积分
 - R ggplot2 geom_linerange 垂直间隔:线、横线和误差线
 - R ggplot2 geom_blank 什么也不画
 - R ggplot2 geom_path 连接观察结果
 - R ggplot2 geom_dotplot 点图
 - R ggplot2 geom_errorbarh 水平误差线
 - R ggplot2 geom_function 将函数绘制为连续曲线
 - R ggplot2 geom_polygon 多边形
 - R ggplot2 geom_histogram 直方图和频数多边形
 - R ggplot2 geom_tile 矩形
 - R ggplot2 geom_segment 线段和曲线
 - R ggplot2 geom_density_2d 二维密度估计的等值线
 - R ggplot2 geom_map 参考Map中的多边形
 - R ggplot2 geom_density 平滑密度估计
 - R ggplot2 geom_abline 参考线:水平、垂直和对角线
 
注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Violin plot。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
