使用ggplot2作图时,尤其是箱线图时,我们都是直接使用的原始数据,而最终图中呈现出了最大值、最小值、中位数之类的数值,这些是由ggplot2内置函数自动计算得来的。
那如何直接提取这些已经计算好结果呢?
太长不看版:使用ggplot_build()可直接提取绘图参数中的data数据
具体实现:
#以内置ToothGrowth数据为例
#作图,箱线图
library(ggplot2)
p <- ggplot(ToothGrowth, aes(x=dose, y=len,group = dose)) + geom_boxplot()
p
#提取绘图参数
pg <- ggplot_build(p)
pg
得到图片为:
输出结果如下:
$data
$data[[1]]
ymin lower middle upper ymax outliers notchupper notchlower x flipped_aes PANEL group ymin_final ymax_final xmin xmax xid newx new_width
1 4.2 7.225 9.85 12.250 17.6 21.5 11.62533 8.074674 1 FALSE 1 1 4.2 21.5 0.625 1.375 1 1 0.75
2 13.6 16.250 19.25 23.375 27.3 21.76725 16.732746 2 FALSE 1 2 13.6 27.3 1.625 2.375 2 2 0.75
3 18.5 23.525 25.95 27.825 33.9 27.46918 24.430815 3 FALSE 1 3 18.5 33.9 2.625 3.375 3 3 0.75
weight colour fill size alpha shape linetype
1 1 grey20 white 0.5 NA 19 solid
2 1 grey20 white 0.5 NA 19 solid
3 1 grey20 white 0.5 NA 19 solid
$layout
<ggproto object: Class Layout, gg>
coord: <ggproto object: Class CoordCartesian, Coord, gg>
aspect: function
backtransform_range: function
clip: on
default: TRUE
distance: function
expand: TRUE
is_free: function
is_linear: function
labels: function
limits: list
modify_scales: function
range: function
render_axis_h: function
render_axis_v: function
render_bg: function
render_fg: function
setup_data: function
setup_layout: function
setup_panel_guides: function
setup_panel_params: function
setup_params: function
train_panel_guides: function
transform: function
super: <ggproto object: Class CoordCartesian, Coord, gg>
coord_params: list
facet: <ggproto object: Class FacetNull, Facet, gg>
compute_layout: function
draw_back: function
draw_front: function
draw_labels: function
draw_panels: function
finish_data: function
init_scales: function
map_data: function
params: list
setup_data: function
setup_params: function
shrink: TRUE
train_scales: function
vars: function
super: <ggproto object: Class FacetNull, Facet, gg>
facet_params: list
finish_data: function
get_scales: function
layout: data.frame
map_position: function
panel_params: list
panel_scales_x: list
panel_scales_y: list
render: function
render_labels: function
reset_scales: function
setup: function
setup_panel_guides: function
setup_panel_params: function
train_position: function
xlabel: function
ylabel: function
super: <ggproto object: Class Layout, gg>
$plot
attr(,"class")
[1] "ggplot_built"
总的绘图参数包含了$data
, $layout
, $plot
三部分内容,其中data[[1]]
就是我们想要的计算后数据
可以直接调取:
pg$data[[1]]
ymin lower middle upper ymax outliers notchupper notchlower x flipped_aes PANEL group ymin_final ymax_final xmin xmax xid newx new_width
1 4.2 7.225 9.85 12.250 17.6 21.5 11.62533 8.074674 1 FALSE 1 1 4.2 21.5 0.625 1.375 1 1 0.75
2 13.6 16.250 19.25 23.375 27.3 21.76725 16.732746 2 FALSE 1 2 13.6 27.3 1.625 2.375 2 2 0.75
3 18.5 23.525 25.95 27.825 33.9 27.46918 24.430815 3 FALSE 1 3 18.5 33.9 2.625 3.375 3 3 0.75
weight colour fill size alpha shape linetype
1 1 grey20 white 0.5 NA 19 solid
2 1 grey20 white 0.5 NA 19 solid
3 1 grey20 white 0.5 NA 19 solid
首先第1,2,3行分别对应着从左往右的3个箱线图,
其中ymin/ymax 是 最小/大值(除异常值之外的)
lower/upper 是 箱体下边缘/上边缘
middle 是 中位值
outliers 是 异常值
……