facet_wrap中的各个轴标签,不带scales =“ free”

时间:2019-07-31 09:42:31

标签: r ggplot2

我的数据如下:

df <- data.frame(Year = as.factor(c(rep(2015, 3), rep(2016, 3), rep(2017,3))),
                 Tax = as.factor(c(rep(c("A", "B", "C"), 3))),
                 Depth = as.factor(c(10, 30, 50, 20,30,50,10,30,40)),
                 values= c(0.5, 0.25, 0.25, 0.1, 0.4, 0.5, 0.2, 0.6, 0.2))

我想用一些间隙来绘制缺失数据和单个轴标签。

library(ggplot2)     

scale的{​​{1}}自变量给出了各个轴,但由于未反映出丢失的数据而无法达到预期的性能。

facet_wrap

没有ggplot(df, aes(Depth, values, fill=Tax)) + geom_bar(stat="identity")+ facet_wrap(~Year, scale="free") + coord_flip()

scales

表示缺少的数据(我想要!),但是缺少轴标签(我需要)。

我能做些什么吗?

1 个答案:

答案 0 :(得分:1)

似乎可以使用lemon软件包来完成此操作:

library(tidyverse)
library(lemon)

df <- data.frame(Year = as.factor(c(rep(2015, 3), rep(2016, 3), rep(2017,3))),
                 Tax = as.factor(c(rep(c("A", "B", "C"), 3))),
                 Depth = as.factor(c(10, 30, 50, 20,30,50,10,30,40)),
                 values= c(0.5, 0.25, 0.25, 0.1, 0.4, 0.5, 0.2, 0.6, 0.2))

ggplot(df, aes(Depth, values, fill=Tax)) + geom_bar(stat="identity")+
   facet_rep_wrap(~Year,repeat.tick.labels = T) +
   coord_flip()