1
- # ' Contours of a 2d density estimate
1
+ # ' Contours of a 2D density estimate
2
2
# '
3
3
# ' Perform a 2D kernel density estimation using [MASS::kde2d()] and
4
4
# ' display the results with contours. This can be useful for dealing with
5
- # ' overplotting. This is a 2d version of [geom_density()].
5
+ # ' overplotting. This is a 2D version of [geom_density()]. `geom_density_2d()`
6
+ # ' draws contour lines, and `geom_density_2d_filled()` draws filled contour
7
+ # ' bands.
6
8
# '
7
9
# ' @eval rd_aesthetics("geom", "density_2d")
8
- # ' @seealso [geom_contour()] for information about how contours
9
- # ' are drawn; [geom_bin2d()] for another way of dealing with
10
+ # ' @eval rd_aesthetics("geom", "density_2d_filled")
11
+ # ' @seealso [geom_contour()], [geom_contour_filled()] for information about
12
+ # ' how contours are drawn; [geom_bin2d()] for another way of dealing with
10
13
# ' overplotting.
11
14
# ' @param geom,stat Use to override the default connection between
12
15
# ' `geom_density_2d` and `stat_density_2d`.
13
16
# ' @inheritParams layer
14
17
# ' @inheritParams geom_point
15
18
# ' @inheritParams geom_path
19
+ # ' @param contour_var Character string identifying the variable to contour
20
+ # ' by. Can be one of `"density"`, `"ndensity"`, or `"count"`. See the section
21
+ # ' on computed variables for details.
16
22
# ' @export
17
23
# ' @examples
18
24
# ' m <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
19
25
# ' geom_point() +
20
26
# ' xlim(0.5, 6) +
21
27
# ' ylim(40, 110)
28
+ # '
29
+ # ' # contour lines
22
30
# ' m + geom_density_2d()
31
+ # '
23
32
# ' \donttest{
24
- # ' m + stat_density_2d(aes(fill = after_stat(level)), geom = "polygon")
33
+ # ' # contour bands
34
+ # ' m + geom_density_2d_filled(alpha = 0.5)
35
+ # '
36
+ # ' # contour bands and contour lines
37
+ # ' m + geom_density_2d_filled(alpha = 0.5) +
38
+ # ' geom_density_2d(size = 0.25, colour = "black")
25
39
# '
26
40
# ' set.seed(4393)
27
41
# ' dsmall <- diamonds[sample(nrow(diamonds), 1000), ]
30
44
# ' # set of contours for each value of that variable
31
45
# ' d + geom_density_2d(aes(colour = cut))
32
46
# '
33
- # ' # Similarly, if you apply faceting to the plot, contours will be
34
- # ' # drawn for each facet, but the levels will calculated across all facets
35
- # ' d + stat_density_2d(aes(fill = after_stat(level)), geom = "polygon") +
36
- # ' facet_grid(. ~ cut) + scale_fill_viridis_c()
37
- # ' # To override this behavior (for instace, to better visualize the density
38
- # ' # within each facet), use after_stat(nlevel)
39
- # ' d + stat_density_2d(aes(fill = after_stat(nlevel)), geom = "polygon") +
40
- # ' facet_grid(. ~ cut) + scale_fill_viridis_c()
47
+ # ' # If you draw filled contours across multiple facets, the same bins are
48
+ # ' # used across all facets
49
+ # ' d + geom_density_2d_filled() + facet_wrap(vars(cut))
50
+ # ' # If you want to make sure the peak intensity is the same in each facet,
51
+ # ' # use `contour_var = "ndensity"`.
52
+ # ' d + geom_density_2d_filled(contour_var = "ndensity") + facet_wrap(vars(cut))
53
+ # ' # If you want to scale intensity by the number of observations in each group,
54
+ # ' # use `contour_var = "count"`.
55
+ # ' d + geom_density_2d_filled(contour_var = "count") + facet_wrap(vars(cut))
41
56
# '
42
- # ' # If we turn contouring off, we can use use geoms like tiles:
43
- # ' d + stat_density_2d(geom = "raster", aes(fill = after_stat(density)), contour = FALSE)
57
+ # ' # If we turn contouring off, we can use other geoms, such as tiles:
58
+ # ' d + stat_density_2d(
59
+ # ' geom = "raster",
60
+ # ' aes(fill = after_stat(density)),
61
+ # ' contour = FALSE
62
+ # ' ) + scale_fill_viridis_c()
44
63
# ' # Or points:
45
64
# ' d + stat_density_2d(geom = "point", aes(size = after_stat(density)), n = 20, contour = FALSE)
46
65
# ' }
47
66
geom_density_2d <- function (mapping = NULL , data = NULL ,
48
- stat = " density2d " , position = " identity" ,
67
+ stat = " density_2d " , position = " identity" ,
49
68
... ,
69
+ contour_var = " density" ,
50
70
lineend = " butt" ,
51
71
linejoin = " round" ,
52
72
linemitre = 10 ,
@@ -65,6 +85,8 @@ geom_density_2d <- function(mapping = NULL, data = NULL,
65
85
lineend = lineend ,
66
86
linejoin = linejoin ,
67
87
linemitre = linemitre ,
88
+ contour = TRUE ,
89
+ contour_var = contour_var ,
68
90
na.rm = na.rm ,
69
91
...
70
92
)
@@ -84,3 +106,42 @@ geom_density2d <- geom_density_2d
84
106
GeomDensity2d <- ggproto(" GeomDensity2d" , GeomPath ,
85
107
default_aes = aes(colour = " #3366FF" , size = 0.5 , linetype = 1 , alpha = NA )
86
108
)
109
+
110
+ # ' @export
111
+ # ' @rdname geom_density_2d
112
+ geom_density_2d_filled <- function (mapping = NULL , data = NULL ,
113
+ stat = " density_2d_filled" , position = " identity" ,
114
+ ... ,
115
+ contour_var = " density" ,
116
+ na.rm = FALSE ,
117
+ show.legend = NA ,
118
+ inherit.aes = TRUE ) {
119
+ layer(
120
+ data = data ,
121
+ mapping = mapping ,
122
+ stat = stat ,
123
+ geom = GeomDensity2dFilled ,
124
+ position = position ,
125
+ show.legend = show.legend ,
126
+ inherit.aes = inherit.aes ,
127
+ params = list (
128
+ na.rm = na.rm ,
129
+ contour = TRUE ,
130
+ contour_var = contour_var ,
131
+ ...
132
+ )
133
+ )
134
+ }
135
+
136
+ # ' @export
137
+ # ' @rdname geom_density_2d
138
+ # ' @usage NULL
139
+ geom_density2d_filled <- geom_density_2d_filled
140
+
141
+ # ' @rdname ggplot2-ggproto
142
+ # ' @format NULL
143
+ # ' @usage NULL
144
+ # ' @export
145
+ # ' @include geom-polygon.r
146
+ GeomDensity2dFilled <- ggproto(" GeomDensity2dFilled" , GeomPolygon )
147
+
0 commit comments