小平面包裹的x轴,每层都有垂直线

时间:2018-06-05 15:55:40

标签: r ggplot2 facet-wrap

我有多级x轴的情节:

Years <-  c("2016","2016","2016","2016",
           "2017","2017","2017","2017")
Quarters <-  c("Q1","Q2","Q3","Q4",
        "Q1","Q2","Q3","Q4")
Series1 <- c("100","200","300","400","500","600","700","800")
Series1 <- as.numeric(Series1)

df <- data.frame(Years,Quarters, Series1)

library(ggplot2)
ggplot(df) +
        geom_point(aes(x = Quarters, y = Series1)) + 
        facet_wrap( ~ Years, strip.position = "bottom",scales = "free_x") + 
        theme(panel.spacing = unit(0,"lines"), strip.background = 
element_blank(),
              strip.placement = "outside")

Current R Chart

我想知道ggplot是否允许我在群组周围添加一行看起来像这样:

Excel Chart

1 个答案:

答案 0 :(得分:0)

它不像您要求的那样,但这很接近:

Years <-  factor(x = c("2016","2016","2016","2016",
            "2017","2017","2017","2017"), 
            levels = c("2016", "2017"))
Quarters <-  factor(x = c("Q1","Q2","Q3","Q4", "Q1","Q2","Q3","Q4"), 
                    levels = c("Q1","Q2","Q3","Q4"))
Series1 <- c("100","200","300","400","500","600","700","800")
Series1 <- as.numeric(Series1)

df <- data.frame(Years,Quarters, Series1)

library(ggplot2)
ggplot(df) +
    geom_point(aes(x = Quarters, y = Series1)) + 
    facet_wrap( ~ Years , strip.position = "bottom",scales = "free_x") 

testplot1