ggplot2 facet_wrap:仅使用每组中存在的x轴标签

abs*_*urd 2 plot r ggplot2 facet-wrap axis-labels

我有以下数据集:

subj <- c(rep(11,3),rep(12,3),rep(14,3),rep(15,3),rep(17,3),rep(18,3),rep(20,3))
group <- c(rep("u",3),rep("t",6),rep("u",6),rep("t",6))
time <- rep(1:3,7)
mean <- c(0.7352941, 0.8059701, 0.8823529, 0.9264706, 0.9852941, 0.9558824, 0.7941176, 0.8676471, 0.7910448, 0.7058824, 0.8382353, 0.7941176, 0.9411765, 0.9558824, 0.9852941, 0.7647059, 0.8088235, 0.7968750, 0.8088235, 0.8500000, 0.8412698)
df <- data.frame(subj,group,time,mean)
df$subj <- as.factor(df$subj)
df$time <- as.factor(df$time)
Run Code Online (Sandbox Code Playgroud)

现在我用ggplot2创建一个条形图:

library(ggplot2)
qplot(x=subj, y=mean*100, fill=time, data=df, geom="bar",stat="identity",position="dodge") +
   facet_wrap(~ group)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如何使其不显示每个方面中不存在的x轴标签?如何在每个subj之间获得相等的距离(即摆脱更大的间隙)?

Jot*_*ota 9

你可以使用scale="free":

ggplot(df, aes(x=subj, y=mean*100, fill=time)) +
  geom_bar(stat="identity", position="dodge") +
  facet_wrap(~ group, scale="free") 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

使用时美学略有不同的另一种选择facet_grid.与上面的图形相比,这里的面板宽度不同,但由于"space="free_x"条形宽度相同.

ggplot(df, aes(x=subj, y=mean*100, fill=time)) +
  geom_bar(stat="identity", position="dodge") +
  facet_grid(~ group, scale="free", space="free_x")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述