Applying log scale to y-axis for visualizing propo

2019-07-19 17:08发布

问题:

I am attempting to recreate some plots from a research article in R and am running into an issue with applying a log scale to y axis. The visualization I'm attempting to recreate is this: reference plot with y log scale

I currently have a working version without the logarithmic scale applied to the y-axis:

  Proportion_Mean_Plot <- ggplot(proportions, aes(days2, 
                                 proportion_mean, group = observation)) +
  geom_point(aes(shape = observation)) + 
  geom_line() +
  scale_x_continuous(breaks = seq(0,335,20)) +
  scale_y_continuous(breaks = seq(0,6,.5)) +
  theme_tufte() + 
  geom_rangeframe() +
  theme(legend.position="none") +
  theme(axis.line.x = element_line(colour = "black", size = 0.5, linetype = 1),
        axis.line.y = element_line(colour = "black", size = 0.5, linetype = 1)) +
  labs(title = "Proportion of Baseline Mean",
       subtitle = "Daily steps within each intervention phase",
       x = "DAYS", 
       y = "PROPORTION OF BASELINE \n(MEAN)") +
  geom_vline(xintercept = 164.5) +
  geom_hline(yintercept = 1) +
  annotate("text", x = c(82, 246), y = 5,
           label = c("Intervention 1", "Intervention 2")) +
  geom_segment(aes(x = 0, y = mean, xend = end, yend = mean),
               data = proportion_intervention1_data) +
  geom_segment(aes(x = start, y = mean, xend = end, yend = mean),
               data = proportion_intervention2_data, linetype = 4) 

This produces a decent representation of the original: normally scaled y-axis plot

I would like to try to apply that logarithmic scaling to more closely match it. Any help is appreciated.

回答1:

As per Richard's suggestion, here is a quick example how you can use scale_y_log10:

suppressPackageStartupMessages(library(tidyverse))
set.seed(123)
# generate some data
proportions <- tibble(interv_1 = pmax(0.4, rnorm(160, mean = 1.3, sd = 0.2)),
                      interv_2 = pmax(0.01, rnorm(160, mean = 1.6, sd = 0.5)))

proportions <- proportions %>% 
  gather(key = observation, value = proportion_mean) %>% 
  mutate(days2 = 1:320)

# create the plot
ggplot(proportions, aes(days2, proportion_mean, group = observation)) +
  geom_point(aes(shape = observation)) + 
  geom_line() +
  scale_x_continuous(breaks = seq(0,335,20), expand = c(0, 0)) +
  scale_y_log10(breaks = c( 0.1, 0.5, 1, 2, 3, 4, 5), limits = c(0.1, 5)) +
  # theme_tufte() + 
  # geom_rangeframe() +
  theme(legend.position="none") +
  theme(axis.line.x = element_line(colour = "black", size = 0.5, linetype = 1),
        axis.line.y = element_line(colour = "black", size = 0.5, linetype = 1)) +
  labs(title = "Proportion of Baseline Mean",
       subtitle = "Daily steps within each intervention phase",
       x = "DAYS", 
       y = "PROPORTION OF BASELINE \n(MEAN)") +
  geom_vline(xintercept = 164.5) +
  geom_hline(yintercept = 1) +
  annotate("text", x = c(82, 246), y = 5,
           label = c("Intervention 1", "Intervention 2")) +
  # plugged the values for the means of the two distributions
  geom_segment(aes(x = 0, y = 1.3, xend = 164.5, yend = 1.3)) +
  geom_segment(aes(x = 164.5, y = 1.6, xend = 320, yend = 1.6), linetype = 4)