我运行了发布在http://ggplot2.tidyverse.org/articles/extending-ggplot2.html#picking-defaults的 R 代码 ,以下是修改后的代码,添加了一些 print() 以显示每个步骤中的变量值,我的问题在代码中被标记为注释:
StatDensityCommon <- ggproto("StatDensityCommon", Stat, required_aes = "x",
setup_params = function(data, params) {
print("PARAMS BEFORE:")
print(params)
if(!is.null(params$bandwidth))
return(params)
print("DATA: ")
print(data)
#1. When and how does the data being modified and the "group" field added?
xs <- split(data$x, data$group)
print("XS: ")
print(xs)
bws <- vapply(xs, bw.nrd0, numeric(1))
print("BWS: ")
print(bws)
bw <- mean(bws)
print("BW: ")
print(bw)
message("Picking bandwidth of ", signif(bw, 3))
params$bandwidth <- bw
print("PARAMS AFTER: ")
print(params)
params
},
compute_group = function(data, scales, bandwidth = 1) {
#2. how does the bandwidth computed in setup_params passed into compute_group
#even if the bandwidth has already been set to 1 in the arguments?
d <- density(data$x, bw = bandwidth)
data.frame(x = d$x, y = d$y)
}
)
stat_density_common <- function(mapping = NULL, data = NULL, geom = "line", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, bandwidth = NULL, ...){
layer(stat = StatDensityCommon, data = data, mapping = mapping, geom = geom, position = position, show.legend = show.legend, inherit.aes = inherit.aes, params = list(bandwidth = bandwidth, na.rm = na.rm, ...))
}
ggplot(mpg, aes(displ, colour = drv)) + stat_density_common()
以下是除绘图外的输出:
[1] "PARAMS BEFORE:"
$bandwidth
NULL
$na.rm
[1] FALSE
[1] "DATA: "
x colour PANEL group
1 1.8 f 1 2
2 1.8 f 1 2
3 2.0 f 1 2
4 2.0 f 1 2
5 2.8 f 1 2
6 2.8 f 1 2
7 3.1 f 1 2
8 1.8 4 1 1
9 1.8 4 1 1
10 2.0 4 1 1
11 2.0 4 1 1
12 2.8 4 1 1
13 2.8 4 1 1
14 3.1 4 1 1
15 3.1 4 1 1
16 2.8 4 1 1
17 3.1 4 1 1
18 4.2 4 1 1
19 5.3 r 1 3
20 5.3 r 1 3
21 5.3 r 1 3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.8 f 1 2
108 2.0 f 1 2
109 2.4 f 1 2
110 2.4 f 1 2
111 2.4 f 1 2
112 2.4 f 1 2
113 2.5 f 1 2
114 2.5 f 1 2
115 3.3 f 1 2
116 2.0 f 1 2
117 2.0 f 1 2
118 2.0 f 1 2
119 2.0 f 1 2
120 2.7 f 1 2
121 2.7 f 1 2
122 2.7 f 1 2
123 3.0 4 1 1
124 3.7 4 1 1
125 4.0 4 1 1
126 4.7 4 1 1
127 4.7 4 1 1
128 4.7 4 1 1
129 5.7 4 1 1
130 6.1 4 1 1
131 4.0 4 1 1
132 4.2 4 1 1
133 4.4 4 1 1
134 4.6 4 1 1
135 5.4 r 1 3
136 5.4 r 1 3
137 5.4 r 1 3
138 4.0 4 1 1
139 4.0 4 1 1
140 4.6 4 1 1
141 5.0 4 1 1
142 2.4 f 1 2
143 2.4 f 1 2
144 2.5 f 1 2
145 2.5 f 1 2
146 3.5 f 1 2
147 3.5 f 1 2
148 3.0 f 1 2
149 3.0 f 1 2
150 3.5 f 1 2
151 3.3 4 1 1
152 3.3 4 1 1
153 4.0 4 1 1
154 5.6 4 1 1
155 3.1 f 1 2
156 3.8 f 1 2
157 3.8 f 1 2
158 3.8 f 1 2
159 5.3 f 1 2
160 2.5 4 1 1
161 2.5 4 1 1
162 2.5 4 1 1
163 2.5 4 1 1
164 2.5 4 1 1
165 2.5 4 1 1
166 2.2 4 1 1
167 2.2 4 1 1
168 2.5 4 1 1
169 2.5 4 1 1
170 2.5 4 1 1
171 2.5 4 1 1
172 2.5 4 1 1
173 2.5 4 1 1
174 2.7 4 1 1
175 2.7 4 1 1
176 3.4 4 1 1
177 3.4 4 1 1
178 4.0 4 1 1
179 4.7 4 1 1
180 2.2 f 1 2
181 2.2 f 1 2
182 2.4 f 1 2
183 2.4 f 1 2
184 3.0 f 1 2
185 3.0 f 1 2
186 3.5 f 1 2
187 2.2 f 1 2
188 2.2 f 1 2
189 2.4 f 1 2
190 2.4 f 1 2
191 3.0 f 1 2
192 3.0 f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