GeomXmeanlabel <- ggplot2::ggproto("GeomXmeanlabel", ggplot2::Geom,
draw_panel = function(data, panel_params, coord) {
ranges <- coord$backtransform_range(panel_params)
data_raw <- data
data$x <- unique(data_raw$x)
# data$yend <- mean(data$y)
data$count <- NA
for (i in 1:length(data$x)){
data$count[i] <- nrow(data_raw[data_raw$x == data$x[i],])
}
data$y <- data$count + (ranges$y[1] + ranges$y[2])*2/10
# data$y <- (ranges$y[1] + ranges$y[2])*3/10
# data$xend <- ranges$x[2]
data$label <- data$count
data <- unique(data)
GeomLabel$draw_panel(unique(data), panel_params, coord)
},
default_aes = ggplot2::aes(colour = "black", size = 5,
linetype = 1,
fill = "white",
alpha = 1),
required_aes = "x",
draw_key = ggplot2::draw_key_label
)
#' Lines defined by mean value of y
#'
#'
#' @param mapping
#' @param data
#' @param ...
#' @param x
#' @param na.rm
#' @param show.legend
#'
#' @return
#' @export
#'
#' @examples
#' library(ggplot2)
#' ggplot(data = cars) +
#' aes(x = speed > 15) + geom_bar() + geom_x_mean_label() +
#' scale_y_continuous(limits = c(0, 30))
#'
#' ggplot2::layer_data(last_plot(), 2)
geom_x_mean_label <- function(mapping = NULL, data = NULL,
...,
x,
na.rm = FALSE,
show.legend = NA) {
ggplot2::layer(
data = data,
mapping = mapping,
stat = ggplot2::StatIdentity,
geom = GeomXmeanlabel,
position = ggplot2::PositionIdentity,
show.legend = show.legend,
inherit.aes = TRUE,
params = list(
na.rm = na.rm,
...
)
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.