scale_centerY: Center y axes of different facets around 0 or 1

scale_centerYR Documentation

Center y axes of different facets around 0 or 1

Description

solution adapted from https://stackoverflow.com/questions/31782799/how-to-use-free-scales-but-keep-a-fixed-reference-point-in-ggplot

Usage

scale_centerY(bycols, fun = min, dplot, FLAGcenterLog = TRUE, ...)

Arguments

bycols

character vector of columns to summarize by. These are typically columns you facet over

fun

min or max

dplot

data.table(measurement = nominal values, bycols...). Typically, the data that went into the plot

FLAGcenterLog

do the centering on log scale

...

arguments to geom_blank

Details

You need to call this function twice, once with min and once with max

Value

geom_blank

Examples


# linear scale
dx <- copy(mtcars)
dx <- data.table(dx)
dx[,`:=`(measurement = wt-mean(wt))]

ggplot(dx, aes(mpg, measurement)) + 
  facet_wrap( ~ cyl, scales = "free_y") + 
  geom_point() +
  scale_centerY("cyl", min, dx, FALSE) +
  scale_centerY("cyl", max, dx, FALSE)

# log scale
dx <- copy(mtcars)
dx <- data.table(dx)
dx[,`:=`(measurement = wt/mean(wt))]

ggplot(dx, aes(mpg, measurement)) + 
  facet_wrap( ~ cyl, scales = "free_y") + 
  geom_point() +
  scale_centerY("cyl", min, dx, TRUE) +
  scale_centerY("cyl", max, dx, TRUE) + 
  scale_y_log10()


dlill/conveniencefunctions documentation built on Sept. 30, 2022, 4:40 a.m.