【问题标题】:ggplot Multiple facets and combined x axisggplot 多面和组合的x轴
【发布时间】:2018-11-12 20:39:03
【问题描述】:

我正在尝试创建一个图表来跟踪多个因素的结果。理想情况下,我希望我的 xaxis 是 Day,天数位于特定日期的代表中间,y 轴是结果,而 facet 是 Lot (1-4)。我很难使用可重复的文本使一天以底部为中心,因为代表的数量可能会有所不同。

我正在使用这篇文章中显示的想法:Multi-row x-axis labels in ggplot line chart,但无法取得任何进展。

这是我一直在使用的一些代码以及到目前为止的情节。 x 轴太忙了,我正在尝试巩固它。

data <- data.frame(System = rep(c("A", "B"), each = 120), Lot = rep(1:4, each = 30),
               Day = rep(1:5, each = 6), Rep = rep(1:6, 40), Result = rnorm(240))

library(ggplot2)

ggplot(data, aes(x = interaction(Day, Rep, lex.order = TRUE), y = Result, color = System, group = System)) +
geom_point() +
geom_line() +
theme(legend.position = "bottom") + 
facet_wrap(~Lot, ncol = 1) +
geom_vline(xintercept = (which(data$Rep == 1 & data$Day != 1)), color = "gray60")

【问题讨论】:

    标签: r ggplot2 axis facet


    【解决方案1】:

    我不能 100% 确定这是否正是您所追求的,但这会以 x 轴为中心。

    library(dplyr)
    library(tidyr)
    library(ggplot2)
    
    df <- data.frame(System = rep(c("A", "B"), each = 120), Lot = rep(1:4, each = 30),
                       Day = rep(1:5, each = 6), Rep = rep(1:6, 40), Result = rnorm(240))
    
    df <- df %>% 
         unite(Day_Rep, Day, Rep, sep = ".", remove = F) %>% 
         mutate(Day_Rep = as.numeric(Day_Rep))
    
    
    ggplot(df, aes(x = Day_Rep, y = Result, color = System, group = System)) +
         geom_point() +
         geom_line() +
         theme(legend.position = "bottom") + 
         facet_wrap(~Lot, ncol = 1) +
         scale_x_continuous(labels = df$Day, breaks = df$Day + 0.5)+
         geom_vline(xintercept = setdiff(unique(df$Day), 1))
    

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-10-16
      • 2021-12-16
      相关资源
      最近更新 更多