2

我有一些来自李克特调查的数据。调查的范围是从 1 到 5。我正在计算为每个问题选择的答案的百分比。我的问题是在绘制这些百分比时,x 轴的边界似乎是无穷无尽的。这是我用来绘制的代码:

#Get the proportion table for the factored responses and melt it using the reshape2 package
#Graph it using the factor levels for X and the value converted into percentage for y

ggplot(melt(prop.table(table(factor(jsp.df$Q1, 
    levels =c(1, 2, 3, 4, 5))))),
    aes(x=Var1, y=value*100)) + 
geom_bar(stat="identity") + 
labs(title = "Q1: Deaf People Will be in College\n", x = "\nLikert Factors\n", y="Percent Circled\n") +
ylim(c(0,100)) +

#Replace factor levels with label names
scale_x_discrete(breaks=c(1, 2, 3, 4, 5), labels=c("Very Good \nChance", "Fairly Good \nChance", "Some \nChance", "A Little \nChance", "No \nChance"))

结果附后。固定x轴的最佳方法是什么?谢谢

结果

4

1 回答 1

5

融化后,您的prop.table() Var1值数据是整数而不是因素,因为级别是 1,2,....所以在ggplot()通话中您需要使用scale_x_continuous()而不是scale_x_discrete().

+ scale_x_continuous(breaks=c(1, 2, 3, 4, 5), 
                   labels=c("Very Good \nChance", "Fairly Good \nChance", 
                          "Some \nChance", "A Little \nChance", "No \nChance"))

另一种方法是limits=scale_x_discrete(). 对于 limits=您应该使用与 for 相同的值breaks=

+ scale_x_discrete(breaks=c(1, 2, 3, 4, 5), limits=c(1,2,3,4,5),
                   labels=c("Very Good \nChance", "Fairly Good \nChance", 
                      "Some \nChance", "A Little \nChance", "No \nChance"))
于 2013-08-06T17:10:08.867 回答