如何使用 ggplot2 在条形图轴上创建空因子水平的占位符?

han*_*ck1 3 axis r histogram ggplot2

我有一些调查数据,我想从中简单地绘制出每个问题的每个响应的频率。

我的代码

library(ggplot2)

Df1 # data frame with 3 variables: Provider, Question, Score

Df1$Score <- factor(Df1$Score, levels = c(0,1,2,3))

# Plot
q <- qplot(Score, data = Df1, facets = .~Provider, geom="bar", fill=Provider)

q + labs(title = wrapper(Question, width = 70), x = "Score", y = "Frequency")   # Add title and axes labels
Run Code Online (Sandbox Code Playgroud)

结果是这样的: 在此输入图像描述

这很棒,但由于我将生成其中一些图表,如果 x 轴上也有“1”的空格,那么在视觉上更容易解释。

我花了很多时间尝试各种组合scale_x_discretelabels但我breaks仍然reorder无法生成一个难以捉摸的占位符。

有没有一种简单的方法可以强制 ggplot2 绘制空因子?

luk*_*keA 7

由于您没有提供完整的可重现示例:

ggplot(mtcars, 
            aes(factor(gear, levels = 1:5), 
                mpg)) +
  geom_point() + 
  scale_x_discrete(drop=F)
Run Code Online (Sandbox Code Playgroud)

用于drop=FALSE保留未使用的因子水平。