gpt4 book ai didi

r - 如何使用 ggplot2 在条形图 Axis 上为空因子水平创建占位符?

转载 作者:行者123 更新时间:2023-12-02 09:26:32 24 4
gpt4 key购买 nike

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

我的代码

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

结果是这样的: enter image description here

这很好,但由于我将生成其中一些图表,因此在 x Axis 上是否也有“1”的空间时,在视觉上更容易解释。

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

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

最佳答案

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

ggplot(mtcars, 
aes(factor(gear, levels = 1:5),
mpg)) +
geom_point() +
scale_x_discrete(drop=F)

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

关于r - 如何使用 ggplot2 在条形图 Axis 上为空因子水平创建占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37490420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com