ggplot折线图中的多行x轴标签

jlh*_*ard 45 r ggplot2

编辑:此问题已被标记为重复,但此处的回复已经过尝试但无效,因为相关案例是折线图,而不是条形图.应用这些方法会生成一个包含5行的图表,每年1行 - 没用.投票标记为重复的任何人是否真的在这个问题提供的样本数据集上尝试这些方法?如果是这样,请发布作为答案.

原始问题:

Excel枢轴图中有一个功能,允许多级分类轴.我正在尝试找到一种方法ggplot(或R中的任何其他绘图包).

请考虑以下数据集:

set.seed(1)
df=data.frame(year=rep(2009:2013,each=4),
              quarter=rep(c("Q1","Q2","Q3","Q4"),5),
              sales=40:59+rnorm(20,sd=5))
Run Code Online (Sandbox Code Playgroud)

如果将其导入Excel数据透视表,则可以直接创建以下图表:

注意x轴有两个级别,一个用于四分之一,一个用于分组变量year.是否可以使用多级轴ggplot

注意:有一个带有小平面的黑客会产生类似的东西,但这不是我想要的.

library(ggplot2)
ggplot(df) +
  geom_line(aes(x=quarter,y=sales,group=year))+
  facet_grid(.~year,scales="free")
Run Code Online (Sandbox Code Playgroud)

Hen*_*rik 49

我们使用参数annotate(geom = "text",来删除默认的x轴文本(clip = "off"/ coord_cartesian)并添加额外的边距(theme).

使用添加新标签plot.margin.通过将绘图对象转换为grob(element_blank()),可以关闭x轴标签的裁剪.

library(ggplot2)

ggplot(data = df, aes(x = interaction(year, quarter, lex.order = TRUE), 
                      y = sales, group = 1)) +
  geom_line(colour = "blue") +
  annotate(geom = "text", x = seq_len(nrow(df)), y = 34, label = df$quarter, size = 4) +
  annotate(geom = "text", x = 2.5 + 4 * (0:4), y = 32, label = unique(df$year), size = 6) +
  coord_cartesian(ylim = c(35, 65), expand = FALSE, clip = "off") +
  theme_bw() +
  theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank())
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


另请参阅@ eipi10这里的好答案:带有嵌套x变量的两行上的轴标签(年份低于月份)

  • 哟伙计,这是一个极好的答案 (2认同)

VDK*_*VDK 27

Henrik建议的代码确实起作用并帮助了我很多!我认为该解决方案具有很高的价值.但请注意,代码的第一行存在一个小错误,导致数据的顺序错误.代替

... aes(x = interaction(year,quarter), ...
Run Code Online (Sandbox Code Playgroud)

它应该是

... aes(x = interaction(quarter,year), ...
Run Code Online (Sandbox Code Playgroud)

生成的图形具有正确顺序的数据.

在此输入图像描述

PS我建议编辑(直到现在才被拒绝),并且由于声誉很小,我不能发表评论,我宁愿做什么.


sta*_*tor 8

用户在这个线程上Tung有一个很好的答案

library(tidyverse)
library(lubridate)
library(scales)

set.seed(123)
df <- tibble(
  date = as.Date(41000:42000, origin = "1899-12-30"), 
  value = c(rnorm(500, 5), rnorm(501, 10))
)

# create year column for facet
df <- df %>% 
  mutate(year = as.factor(year(date)))

p <- ggplot(df, aes(date, value)) + 
  geom_line() + 
  geom_vline(xintercept = as.numeric(df$date[yday(df$date) == 1]), color = "grey60") + 
  scale_x_date(date_labels = "%b", 
               breaks = pretty_breaks(),
               expand = c(0, 0)) +
  # switch the facet strip label to the bottom
  facet_grid(.~ year, space = 'free_x', scales = 'free_x', switch = 'x') +
  labs(x = "") +
  theme_classic(base_size = 14, base_family = 'mono') +
  theme(panel.grid.minor.x = element_blank()) + 
  # remove facet spacing on x-direction
  theme(panel.spacing.x = unit(0,"line")) +
  # switch the facet strip label to outside 
  # remove background color
  theme(strip.placement = 'outside',
        strip.background.x = element_blank())
p
Run Code Online (Sandbox Code Playgroud)

图片