ggplot2中的离散X标签不会结束

Jon*_*ner 2 r ggplot2

我有一些来自李克特调查的数据.调查的范围是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"))
Run Code Online (Sandbox Code Playgroud)

结果附后.修复x轴的最佳方法是什么?谢谢

结果

Did*_*rts 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"))
Run Code Online (Sandbox Code Playgroud)

另一种方法是设置limits=内部scale_x_discrete().对于l,imits=您应该使用与之相同的值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"))
Run Code Online (Sandbox Code Playgroud)