ggplot 对象的重头创建,主要有两个步骤: ggproto() 方法从父类Geom创建ggproto 类,并个性化类中的方法和属性,以控制输出的图像; 通过上一步创建的类,创建一个geom_* 方法 ,该方法就类似于我们在一般ggplot 作图时使用的方法了,如geom_point; 1-创建ggproto 类 有如下几个设置的内容: GeomNEW <- ggproto("GeomNEW", Geom 下面是一个示例: library(grid) GeomMyPoint <- ggproto("GeomMyPoint", Geom, required_aes 简单查看一下这个类: > str(GeomMyPoint) Classes 'GeomMyPoint', 'Geom', 'ggproto', 'gg' <ggproto object: Class GeomMyPoint 3-查看draw_panel 函数相关的几个对象 先前我们通过sink 捕获了几个对象: library(grid) GeomMyPoint <- ggproto("GeomMyPoint", Geom
可以发现layer函数先处理了一个图层 的geom和stat对象,然后解析出来各种美学和图层参数,最后返回一个ggproto对象用于描述图层的各种信息。 因此可做如下总结 一个图层的定义(geom或者stat开头的函数)必须同时包含一个geom和一个stat对象,通过layer函数将其串联在一起,返回一个ggproto对象。 这个返回的ggproto对象是基于一个LAYER父类,这个LAYER是一个容器,里面有Geom和Stat对象。 ggproto("LayerInstance", layer_class, geom = geom, geom_params = geom_params, stat = stat 函数的第二个参数代表一个ggproto类的父类,在这里是layer_class,它是layer函数的一个参数,其值是一个ggplot2预先定义的Layer类。
new_layer <- ggproto(NULL, layer) old_geom <- new_layer$geom geom <- ggproto( NULL, old_geom, new_layer <- ggproto(NULL, layer) old_geom <- new_layer$geom geom <- ggproto( NULL, old_geom,
StatTextcircle <- ggplot2::ggproto( `_class` = "StatTextcircle", `_inherit` = ggplot2::Stat, required_aes default_aes = ggplot2::aes( x = ggplot2::after_stat(x), y = ggplot2::after_stat(y) ) ) ❝使用 ggproto
「报错信息:」 ❝Error in UseMethod("grid.draw") : "grid.draw"没有适用于"c('LayerInstance', 'Layer', 'ggproto', 'gg
" [7] "facet" "plot_env" "labels" [10] "guides" 其实部分地方也是彼此对应的: > pb$layout$facet <ggproto setup_params: function shrink: TRUE train_scales: function vars: function super: <ggproto object: Class FacetNull, Facet, gg> > plot$facet <ggproto object: Class FacetNull, Facet, gg> compute_layout setup_params: function shrink: TRUE train_scales: function vars: function super: <ggproto
layers[[1]]$compute_aesthetics # <ggproto method> # <Wrapper function> # function (... 0x00000269e8aba280> # <environment: namespace:ggplot2> 我们进一步看一下scale对象的map_df函数: npscales$scales[[1]]$map_df # <ggproto [j]][i])) # } # } 这里是对每一个scale记录的美学映射对data调用map方法,那么继续看map方法的细节: npscales$scales[[1]]$map # <ggproto
mtcars) > summary(p1) data: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb [32x11] faceting: <ggproto mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb [32x11] mapping: x = ~wt, y = ~hp faceting: <ggproto
Petal.Length, Petal.Width, Species [150x5] mapping: x = ~Sepal.Width, y = ~Sepal.Length faceting: <ggproto Petal.Length, Petal.Width, Species [150x5] mapping: x = ~Sepal.Width, y = ~Sepal.Length faceting: <ggproto
它利用ggproto系统扩展ggplot2,提供额外的“geoms”、“stats”和“position”。扩展与现有的ggplot2层元素集成。
, drv, cty, hwy, fl, class [234x11] mapping: colour = factor(cyl), x = displ, y = hwy faceting: <ggproto shrink: TRUE train: function train_positions: function train_scales: function super: <ggproto
) { GeomCustom <- ggproto("GeomCustom", Geom, setup_data = function(self, data, params) { data <- ggproto_parent(Geom, self)$setup_data(data, params) data }, draw_group
questions/47651868/split-violin-plot-with-ggplot2-with-quantiles library(ggplot2) GeomSplitViolin <- ggproto