【问题标题】:Alternate tick labels in grouped ggplot to avoid overlap in R分组ggplot中的备用刻度标签以避免R中的重叠
【发布时间】:2018-11-01 18:09:42
【问题描述】:

我已经用 ggplot 对箱线图进行了分组

require(ggplot2)
require(tidyr)
require(lubridate)

dat.1415<-as.data.frame(sample(1:1000, 181))
dat.1415$date<-seq(as.Date("2014-11-1"), as.Date("2015-4-30"), "day")
names(dat.1415)<-c("value", "date")
dat.1415$month<-month(dat.1415$date)
dat.1415$season<-"2014/15"

dat.1516<-as.data.frame(sample(1:1000, 182))
dat.1516$date<-seq(as.Date("2015-11-1"), as.Date("2016-4-30"), "day")
names(dat.1516)<-c("value", "date")
dat.1516$month<-month(dat.1516$date)
dat.1516$season<-"2015/16"

dat.1617<-as.data.frame(sample(1:1000, 181))
dat.1617$date<-seq(as.Date("2016-11-1"), as.Date("2017-4-30"), "day")
names(dat.1617)<-c("value", "date")
dat.1617$month<-month(dat.1617$date)
dat.1617$season<-"2016/17"

dat.1718<-as.data.frame(sample(1:1000, 181))
dat.1718$date<-seq(as.Date("2017-11-1"), as.Date("2018-4-30"), "day")
names(dat.1718)<-c("value", "date")
dat.1718$month<-month(dat.1718$date)
dat.1718$season<-"2017/18"


dat<-rbind(dat.1415, dat.1516, dat.1617, dat.1718)
dat$month<-month.abb[dat$month]
dat$month<-factor(dat$month)
dat$facet = factor(dat$month, levels = c("Nov", "Dec", "Jan", "Feb", "Mar", "Apr"))

ggplot(dat, aes(x=season, y=value)) + 
    geom_boxplot(fill="grey50") + 
    facet_grid(~facet) + 
    theme_classic()+    
    theme(legend.position="top") +
    labs(x="", y="", title="") +
    guides(fill=F) +
    theme(panel.background = element_rect(fill="grey95"))

但是因为它有太多的框,所以我在 x 轴上得到了重叠的标签。有没有办法让它们在不同的方面交替出现?我不希望 x 轴的位置交替,而是实际标签,例如在方面 1 是“2014/15”和“2016/17”,在方面 2 是“2015/16”和“2017/18” “ 等等。这可能吗?

【问题讨论】:

  • 您是否考虑过翻转刻面以便只有一个 x 轴? facet_grid(facet ~ .)
  • 这是一个相当不寻常的请求,但如果过去有类似的问题,那么基于 ggplot2 的答案可能会涉及使用guide-type 函数。
  • 如果您发布 reproducible example,我会尝试一下。这包括具有代表性但最少的数据样本和最少的代码(即没有清理,只是重新创建问题所需要的)
  • @Jordo82:是的,这将是一个选项,但我只是尝试了一下,感觉它使图表的阅读变得不那么直观。
  • @42 不确定指南类型的功能是什么,但会调查一下。谢谢

标签: r ggplot2


【解决方案1】:

尝试旋转标签以获得完整信息

+ theme(axis.text.x = element_text(angle = 30, hjust = 1)) 

编辑

或者尝试以某种方式操纵您的数据并使用类似的东西

+ scale_x_discrete(breaks=c("1","3"), labels=c(...))

Edits2:我将颜色设置为 0 以便跳过。

ggplot(dat, aes(x=season, y=value)) + 
  geom_boxplot(fill="grey50") + 
  facet_grid(~facet) + 
  theme_classic()+    
  theme(legend.position="top") +
  labs(x="", y="", title="") +
  guides(fill=F) +
  theme(panel.background = element_rect(fill="grey95"))+ 
  theme(axis.text.x = element_text(color=c(1,0,1,0))) 

【讨论】:

  • 这是一个选项,是的,但它也会使它与我拥有的其他图表不一致。但这可能是最后的手段
  • 我刚刚在我的原始帖子中添加了上面的数据。我尝试使用 scale_x_discrete() 手动分配标签,但这不起作用。例如,我可以做所有的“2014/15”和“2016/17”,但不能交替使用它们。现在将尝试您编辑的方法。
  • 请查看我的编辑。通过设置颜色,可以省略(隐藏)一些标签。
【解决方案2】:

ggplot v3.3.0 的新版本增加了使用guide_axis 来躲避标签的能力。

scale_x_discrete(guide = guide_axis(n.dodge = 2))

因此将其添加到您的情节中:

ggplot(dat, aes(x=season, y=value)) + 
    geom_boxplot(fill="grey50") + 
    facet_grid(~facet) + 
    theme_classic()+    
    theme(legend.position="top") +
    labs(x="", y="", title="") +
    guides(fill=F) +
    theme(panel.background = element_rect(fill="grey95")) + 
    scale_x_discrete(guide = guide_axis(n.dodge = 2))

产生以下内容:

【讨论】:

  • 我到处找这个!
猜你喜欢
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 2015-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多