【问题标题】:Is it possible to get `axis breaks` from `ggplots` that use `facet_wrap`?是否可以从使用 `facet_wrap` 的 `ggplots` 中获取`轴中断`?
【发布时间】:2018-10-11 08:44:56
【问题描述】:

我想为下图中的每个轴创建自定义标签。每个方面都有不同数量的中断。我不想在视觉上计算中断,而是调用一个函数来提供每个方面中断的长度。我知道scales 包中的axTicks 函数为基本R 图提供了这个功能。 ggplots 有什么东西吗?这是我正在使用的实际情节,然后是一些重现类似内容的代码。

require(tidyverse)
require(scales)

vary_1 <- c(
  exp(rnorm(250,5,1)),
  rnorm(250,10,10),
  exp(rnorm(250,20,1)),
  exp(rnorm(250,30,1)))


  vary_2 <- c(
      rep('A',250),
      rep('B',250),
      rep('C',250),
      rep('D',250))

  data_frame(vary_1,vary_2) %>% 
      ggplot(aes(vary_1,color = vary_2,fill = vary_2))+
      geom_density()+
      facet_wrap(~vary_2,scales = 'free')

axTicks(side = 2)
[1] 0.0 0.2 0.4 0.6 0.8 1.0 # not the correct answer

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    看看这是否能解决你的问题。

    p <- data_frame(vary_1,vary_2) %>% 
      ggplot(aes(vary_1,color = vary_2,fill = vary_2))+
      geom_density()+
      facet_wrap(~vary_2,scales = 'free')
    
    q <- layer_data(p)
    q %>%
      group_by(PANEL) %>%
      summarise_at(vars(x), funs(min, max))
    
    # # A tibble: 4 x 3
    #   PANEL      min     max
    #   <fct>    <dbl>   <dbl>
    # 1 1      9.95e 0 2.74e 3
    # 2 2     -2.10e 1 4.42e 1
    # 3 3      3.24e 7 7.95e 9
    # 4 4      7.42e11 1.45e14
    

    有关详细信息,请参阅此post

    【讨论】:

    • 效果很好。似乎ggplot 同时拥有layer_data()layer_scale() 来从绘图中获取数据。
    猜你喜欢
    • 1970-01-01
    • 2014-12-03
    • 2022-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多