R,ggplot 2条形图x轴刻度标记名称消失

时间:2018-06-26 19:10:11

标签: r ggplot2 graph

我是R和编程的新手,所以如果这是简单的解决方法,请原谅我。我试图在ggplot2的条形图中标记我的x轴刻度线,但是当我运行代码时,整个x轴刻度线名称消失了,只剩下轴标题了。

Here is a picture of the problem

这是我的代码:

#creating a bar graph of the "income" varibale

bar_graph_income <- ggplot(PRCPS, aes(income)) + geom_bar() + labs(title 
= "How much money are American families making?", subtitle = "Pew 
Research April 17 Political Survey") + labs(x="Income Ranges", y="Survey 
Responses") + theme_bw() 

#creating breaks and naming ticks 

bar_graph_income <- bar_graph_income + scale_x_discrete(breaks=c("1","2","3","4","5","6","7","8","9") , labels=c("1" = "Below $10,000", "2" = "$10,000-$19,999", "3" = "$20,000-$29,999", "4" = "$30,000-$39,999", "5" = "$40,000-$49,999", "6" = "$50,000-$74,999", "7" = "$75,000-$99,999", "8" = "$100,000-$149,999", "9" = "$150,000 or more"))

#changing the text of the title

bar_graph_income <- bar_graph_income + theme(plot.title=element_text(family="Times", face="bold", size=14))

#changing the rest of the text

bar_graph_income<- bar_graph_income + theme(axis.text = element_text(family="Times", size=12, colour="black"))

#calling bar_graph_income

bar_graph_income

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

没有样本数据很难诊断,但是我相信是由于您在连续数据( <li data-id="outline"> <div> <a href="abc/abd/ccc/html">Answer</a> </div> </li> )上调用scale_x_discrete引起的。

您可以通过先设置income来解决此问题:

limits

您也可以使用bar_graph_income <- bar_graph_income + scale_x_discrete(limits=1:9, labels = c("Below $10,000", "$10,000-$19,999", "$20,000-$29,999", "$30,000-$39,999", "$40,000-$49,999", "$50,000-$74,999", "$75,000-$99,999", "$100,000-$149,999", "$150,000 or more")) 一次通话来完成此操作:

ggplot2

或者,您可以将bar_graph_income <- ggplot(PRCPS, aes(income)) + geom_bar() + labs(title = "How much money are American families making?", subtitle = "Pew Research April 17 Political Survey", x = "Income Ranges", y = "Survey Responses") + theme_bw() + scale_x_discrete(limits=1:9, labels = c("Below $10,000", "$10,000-$19,999", "$20,000-$29,999", "$30,000-$39,999", "$40,000-$49,999", "$50,000-$74,999", "$75,000-$99,999", "$100,000-$149,999", "$150,000 or more")) + theme(plot.title=element_text(family="Times", face="bold", size=14), axis.text = element_text(family="Times", size=12, colour="black")) 字段作为因子(income进行突变,并在调用as.factor之前适当地设置级别。