R语言 如何在ggplot()中压缩X轴?

bihw5rsg  于 9个月前  发布在  其他
关注(0)|答案(2)|浏览(99)

第一个可重复的例子:

数据:

grid_tbl <- bind_cols(x = seq(from = -10, to = 10, by = 0.05), 
          y = seq(from = -10, to = 10, by = 0.05))

sine_points_vec <- map(grid_tbl$x, sinpi) %>% unlist()

sine_plot_tbl <- bind_cols(grid_tbl$x, sine_points_vec) %>% rename(x = `...1`, y = `...2`)

字符串

剧情:

sine_plot_tbl %>% 
    select(y,x) %>% 
    rename(y = x, x = y) %>% 
       ggplot(aes(x = y, y = x)) +
        coord_fixed(ratio = 0.8) + 
#        coord_flip() + 
        geom_line(color = "red") +
        geom_hline(yintercept = 0, color = "blue") +
        labs(
            title = "Inverse Sine Relation Plot",
            caption = "Vertical line passes through curve at many points"
        )

目标:

我正在寻找一种方法来压缩X轴的固定百分比,以改变绘制曲线的外观,使曲线的峰值更平坦。
例如,在发布的代码中,coord_fixed(ratio = 0.8)产生了期望的外观,但是如果我取消注解下一行以翻转坐标,则扁平化被丢弃。我得到这样的消息:

  • 坐标系已存在。正在添加新坐标系,该坐标系将替换现有坐标系。*

我并不想转换生成曲线的值,只是想改变曲线的外观。如果你在coord_fixed()函数和coord_flip()函数之间交换注解标记,你会看到挑战。
我尝试在ggplot中添加coord_fixed(),我希望曲线的外观变平。
实际发生的是,随后的coord_flip()函数覆盖了它。

xdnvmnnf

xdnvmnnf1#

使用扩展来扩大所示x轴的范围的轻微变化,这里使两侧的宽度为数据的5倍。

sine_plot_tbl %>% 
  ggplot(aes(x = y, y = x)) +
  geom_path(color = "red") +
  geom_vline(xintercept = 0, color = "blue") +
  scale_x_continuous(expand = expansion(mult = c(5,5))) +
  coord_fixed(ratio = 2) + 
  labs(
    title = "Inverse Sine Relation Plot",
    caption = "Vertical line passes through curve at many points"
  )

字符串


的数据

kqqjbcuj

kqqjbcuj2#

每个图只能有一个coord层。但是,代替coord_flip,你可以切换xy,并在geom_line中使用orientation="y"

library(ggplot2)
library(dplyr, warn=FALLSE)

sine_plot_tbl %>%
  select(y, x) %>%
  rename(y = x, x = y) %>%
  ggplot(aes(x = x, y = y)) +
  geom_line(color = "red", orientation = "y") +
  geom_vline(xintercept = 0, color = "blue") +
  coord_fixed(xlim = c(-5, 5), ratio = .8) +
  labs(
    title = "Inverse Sine Relation Plot",
    caption = "Vertical line passes through curve at many points"
  )

字符串


的数据

相关问题