2014-09-22 125 views
0

我有以下数据如何在R中的barplot中显示x值(名称)?

r<-structure(list(Relation = structure(c(3L, 4L, 1L, 2L), .Label = c("Neighbours", 
"Other known persons", "Parents/Close Family Members", "Relatives" 
), class = "factor"), Number = c(539L, 2315L, 10782L, 18171L)), .Names = c("Relation", 
"Number"), class = "data.frame", row.names = c(NA, -4L)) 

当我绘制barplot使用下面的命令:

barplot(r[,2],names.arg=r$Relation,col="green") 

关系列名称下的x值的名称是不可见/在图中示出。这里有什么问题? 谢谢

+1

代码中引用的'm'对象在哪里?替换为'names.arg = letters [1:4]'似乎命名条形图。你的意思是'barplot(r [,2],names.arg = r $ Relation,col =“green”)'? – MrFlick 2014-09-22 17:30:18

+0

是的,先生,基本上,R在x轴上跳过较长的名称我已将名称更改为在此处缩短。有没有解决方案? – 2014-09-22 17:33:08

+0

使你的阴谋更宽。 R不喜欢重叠轴标签。 – MrFlick 2014-09-22 17:36:28

回答

3

尝试替代在names.arg每一空间中的线料:

barplot(r[,2],names.arg=gsub("\\s","\n", r$Relation),col="green", line=2) 

enter image description here

(需要对标签向下与 “线” 参数转移)

3

答案“这里有什么问题”在评论中。这是一个使用ggplot的解决方案,它可以更好地管理长名称。

library(ggplot2) 
ggplot(r, aes(x=Relation, y=Number)) + 
    geom_bar(stat="identity", fill="lightgreen", color="grey50") 

enter image description here

当你有很长的名字,像这样有时是更好地使用单杠。

ggplot(r, aes(x=Relation, y=Number)) + 
    geom_bar(stat="identity", fill="lightgreen", color="grey50")+ 
    coord_flip() 

enter image description here