首页 > 解决方案 > 在ggplot R中创建自定义/更多刻度x轴距离

问题描述

请在下面找到我的代码(我没有在此处放置每个图像的更新代码。)下面的代码用于标记为 original 的图像:

packages <- c("ggplot2", "dplyr", "quantreg", "officer","tidyverse","here","glue","rvg","viridis","scales")
install.packages(setdiff(packages, rownames(installed.packages())))
lapply(packages, require, character.only = TRUE)

data <- read.csv(file='Spiderplot_data.csv', header=TRUE)
Progressed <- as.factor(data$Progression)
Response <- as.factor(data$X)
Response <- factor(data$X, levels = c("Progressive Disease", "Stable Disease", "Partial Response", "Complete Response")) 
highlight_df <- filter(data, Progression == 1)

p <- ggplot() +
  geom_line(
    data,
    mapping = aes(
      x = Cycle,
      y = Change,
      group = Study_ID,
      color = Response
        )
      ) +
  geom_point(
    data,
    mapping = aes(
      x = Cycle,
      y = Change,
      group = Study_ID,
      color = Response,
      shape = Progressed
        )
      ) +
  geom_point( # Data set with just points
    highlight_df,
    shape = 18,
    size = 2.8,
    mapping = aes(
      x = Cycle,
      y = Change,
      group = Study_ID,
      )
    ) +
  scale_shape_manual(
    values = c(16,18)
      ) +
  scale_color_brewer(
    palette = "Set1"
      ) +
  scale_x_continuous(
    name = "Cycle",
    breaks = c(0,2,4,6,8,10,12,14,16,18,20,30,40,45),
    limits = c(0,45),
    expand = expansion(mult = c(0,0.001))
      ) +
  scale_y_continuous(
    name = "Percent Change in Lesion SLD (%)",
    breaks = c(-1,-.75,-.50,-.40,-.30,-.20,-.10,.10,.20,.30,.40,.50,.65),
    limits = c(-1,.65),
    labels = percent
    ) + 
  ggtitle("Response Plot")
p

p_dml <- rvg::dml(ggobj = p)

# initialize PowerPoint slide ----
officer::read_pptx() %>%
  # add slide ----
officer::add_slide() %>%
  # specify object and location of object ----
officer::ph_with(p_dml, ph_location(width = 10, height = 5)) %>%
  # export slide -----
base::print(
  target = here::here(
    "demo_3.pptx"
  )
)

如您所见,x 轴上每个点之间的距离是线性且相等的,有没有办法将绘图上的距离分散在 0-20 之间,并使 20-40 之间的空间更小?

如您所见,我曾尝试使用扩展功能来做到这一点,但无济于事,我使用的功能错了吗?

任何帮助将非常感激。

原始图像

原始图像

带有 scale_x_sqrt 的图像

带有 scale_x_sqrt 的图像

带有 scale_x_continuous(trans = scales::pseudo_log_trans(sigma = 3)) 的图像

带有 scale_x_continuous(trans = scales::pseudo_log_trans(sigma = 3)) 的图像

虽然这与我们所寻找的一样接近,但是否有一个功能可以让我准确地自定义刻度在轴上的位置?

标签: rggplot2

解决方案


推荐阅读