Skip to content

Commit fab9732

Browse files
committed
Make level numeric for filled contours. Fixes #3875.
1 parent c235a4d commit fab9732

File tree

5 files changed

+15
-18
lines changed

5 files changed

+15
-18
lines changed

R/geom-contour.r

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#' @seealso [geom_density_2d()]: 2d density contours
2121
#' @export
2222
#' @examples
23-
#' #' # Basic plot
23+
#' # Basic plot
2424
#' v <- ggplot(faithfuld, aes(waiting, eruptions, z = density))
2525
#' v + geom_contour()
2626
#'

R/geom-density2d.r

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,13 @@
4040
#' # set of contours for each value of that variable
4141
#' d + geom_density_2d(aes(colour = cut))
4242
#'
43-
#' # When coloring by level across multiple facets, it is best to set breaks
44-
#' # manually to ensure they are consistent across facets
45-
#' breaks <- seq(0, 0.45, by = 0.05)
46-
#' d + geom_density_2d_filled(breaks = breaks) +
47-
#' facet_grid(. ~ cut)
43+
#' # Coloring by level across multiple facets
44+
#' d + geom_density_2d_filled() +
45+
#' facet_grid(. ~ cut) + scale_fill_viridis_c()
4846
#' # If you want to make sure the peak intensity is the same in each facet,
4947
#' # use `after_stat(nlevel)`. Note: this is a numerical value, not a factor,
5048
#' # and it represents the upper limit of each contour band.
51-
#' d + geom_density_2d_filled(aes(fill = after_stat(nlevel)), breaks = breaks) +
49+
#' d + geom_density_2d_filled(aes(fill = after_stat(nlevel))) +
5250
#' facet_grid(. ~ cut) + scale_fill_viridis_c()
5351
#' # You can also use `after_stat(count)` to show the density scaled by the
5452
#' # number of observations in the group. This is useful to highlight differences

R/stat-contour.r

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ StatContourFilled <- ggproto("StatContourFilled", Stat,
108108
names(isobands) <- pretty_isoband_levels(names(isobands))
109109
path_df <- iso_to_polygon(isobands, data$group[1])
110110

111-
path_df$level <- ordered(path_df$level, levels = names(isobands))
112-
path_df$level_low <- breaks[as.numeric(path_df$level)]
113-
path_df$level_high <- breaks[as.numeric(path_df$level) + 1]
114-
path_df$nlevel <- rescale_max(path_df$level_high)
111+
path_df$bin <- ordered(path_df$level, levels = names(isobands))
112+
path_df$level_low <- breaks[as.numeric(path_df$bin)]
113+
path_df$level_high <- breaks[as.numeric(path_df$bin) + 1]
114+
path_df$level <- 0.5*(path_df$level_low + path_df$level_high)
115+
path_df$nlevel <- rescale_max(path_df$level)
115116

116117
path_df
117118
}

man/geom_contour.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geom_density_2d.Rd

Lines changed: 4 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)