Sam Min
Sam Min

Reputation: 87

ggplot2 extension with log xy scales using ggproto

is it possible to make a ggplot2 extension (ex. geom_smooth) that converts the existing xy axes into log10 scales automatically?

here's a reprex for example

set.seed(11) # generate random data
method1 = c(rnorm(19,0,1),2.5)
method2 = c(rnorm(19,0,1),2.5)
Subject <- rep(paste0('S',seq(1:20)), 2)
Data <- data.frame(Value = matrix(c(method1,method2),ncol=1))
Method <- rep(c('Method 1', 'Method 2'), each = length(method1))
df_corr <- data.frame(first = method1, second = method2) # used for correlation

ggplot(data = df_corr, mapping = aes(x = first, y = second)) +
  geom_smooth() + # line 2
  scale_x_continuous(trans = 'log10') +  # line 3
  scale_y_continuous(trans = 'log10') # line4

Instead I would like to create a ggplot2 extension function (that receives all the aesthetics defined by ggplot2()) that does what lines 2-4 at once using ggproto.

Upvotes: 0

Views: 113

Answers (2)

Jon Spring
Jon Spring

Reputation: 66415

You could also throw the three lines into a list:

geom_smooth_log <- list(
  geom_smooth(), # line 2
    scale_x_continuous(trans = 'log10'),  # line 3
    scale_y_continuous(trans = 'log10') # line4
)

ggplot(data = df_corr, mapping = aes(x = first, y = second)) +
  geom_smooth_log

Upvotes: 2

philiptomk
philiptomk

Reputation: 763

You can implement your own function in the following manner:

library(tidyverse)
set.seed(11) # generate random data
method1 = c(rnorm(19,0,1),2.5)
method2 = c(rnorm(19,0,1),2.5)
Subject <- rep(paste0('S',seq(1:20)), 2)
Data <- data.frame(Value = matrix(c(method1,method2),ncol=1))
Method <- rep(c('Method 1', 'Method 2'), each = length(method1))
df_corr <- data.frame(first = method1, second = method2) # used for correlation

my_log_plot <- function(data, x, y) {
  ggplot(data, aes({{x}}, {{y}})) +
    geom_smooth() +
    scale_x_log10() +
    scale_y_log10()
}

my_log_plot(df_corr, first, second)

To create your own ggplot function, you need to use ggproto, and the process is outlined here.

Upvotes: 1

Related Questions