将对数刻度应用于y轴以使用ggplot2可视化比例 [英] Applying log scale to y-axis for visualizing proportions with ggplot2

查看:106
本文介绍了将对数刻度应用于y轴以使用ggplot2可视化比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用R中的一篇研究文章重新创建一些图,并且遇到了将对数刻度应用于y轴的问题。我尝试重新创建的可视化效果是这样的:
具有y对数刻度的参考图

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

我目前有一个工作版本,没有将对数刻度应用于y轴:

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) 

这将产生原始图像:
正常缩放的y轴图

我想尝试应用对数缩放以使其更接近匹配。任何帮助表示赞赏。

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

推荐答案

根据Richard的建议,这是一个简单的示例,说明如何使用 scale_y_log10


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)

这篇关于将对数刻度应用于y轴以使用ggplot2可视化比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆