简体   繁体   English

R中的Barplot:更改顺序和控制标签

[英]Barplot in R: change the order and control labels

I have small data and I want to get bar-plot. 我的数据很少,我想获取条形图。 I managed to graph the data. 我设法绘制数据图。 However, I have two questions. 但是,我有两个问题。

1) I want to make the bars to be plotted in order according to the X-axis, not according the values. 1)我想使条形图按X轴顺序绘制,而不是按值绘制。 The x-axis is a range like : <40,100-500,500-1000, ..... x轴的范围是:<40,100-500,500-1000,.....

some values like 40-100 is greater than 500-1000, it is plotted at the end. 一些值(例如40-100大于500-1000)会在最后绘制。 I want the bars to be based on the values of X-axis. 我希望条形图基于X轴的值。

2) How do I control the position of the label value on each bar. 2)如何控制每个条形上标签值的位置。

Here is my data from dput : 这是我从dput数据:

structure(list(range = structure(c(1L, 6L, 2L, 7L, 3L, 4L, 5L
), .Label = c("<40", "100-500", "1000-1468", "1469-1479", "1480-1500", 
"40-100", "500-1000"), class = "factor"), values = c(100L, 10000L, 
1000L, 505L, 2000L, 50L, 5000L)), .Names = c("range", "values"
), class = "data.frame", row.names = c(NA, -7L))

here is my code: 这是我的代码:

DF$X<-NULL
colnames(DF)<-c("Size","occurances")
gr2<- ggplot(DF, aes(x =Size , y= occurances))+ 
      geom_bar(aes(fill=Size),stat="identity") +
      geom_text(aes(label = paste(sprintf("%0.0f", occurances)),
            y = occurances+0.25, x=Size),
           size = 5, face="bold",position = position_dodge(width=0.9)) +
      theme(axis.text.x=element_text(size=12,colour="gray19",face="bold"),
            axis.text.y=element_text(colour="gray19",size=12,face="bold"))+
      theme(axis.ticks.x = element_line(size = 2))+
      scale_x_discrete(breaks=c("<40","40-100","100-500","500-1000","1000-1468","1469-  1479","1480-1500"),expand=c(0,0) )+
      scale_y_continuous(expand=c(0.008,0.5))+
      guides(fill=FALSE)
      print(gr2)

Any suggestions !! 有什么建议么 !!

Try: 尝试:

DF$Size <- reorder(DF$Size, seq_along(DF$Size))
gr2<- ggplot(DF, aes(x =Size , y= occurances))+ 
  geom_bar(aes(fill=Size),stat="identity") +
  geom_text(aes(label = paste(sprintf("%0.0f", occurances)),
                y = occurances+0.25, x=Size),
            size = 5, face="bold", vjust=-.8) +
  theme(axis.text.x=element_text(size=12,colour="gray19",face="bold"),
        axis.text.y=element_text(colour="gray19",size=12,face="bold"))+
  theme(axis.ticks.x = element_line(size = 2))+
  scale_y_continuous(expand=c(0.1,0))+
  guides(fill=FALSE)
print(gr2)

在此处输入图片说明

The key thing is to re-order your factor levels so that they actually are in increasing order (here I took advantage of the fact that your DF was already sorted in that order). 关键是对因子水平进行重新排序,以使它们实际上按升序排列(在此,我利用了您的DF已经按该顺序排序的事实)。 Then you can use vjust in geom_text to get the alignment right. 然后,您可以在geom_text使用vjust获得正确的对齐方式。 Note I also messed with expand with the y scale. 注意:我也搞砸与expand与y规模。 Also, got rid of your scale_x_discrete call as I think you were trying to use that to change the ordering, but that isn't necessary if you get the factor levels right. 另外,摆脱了scale_x_discrete调用,因为我认为您试图使用它来更改顺序,但是如果您正确设置了因子水平,则不必这样做。

As an aside, how did you generate the buckets? 顺便说一句,您是如何生成存储桶的? You should consider using cut , as that should set up your levels in the correct order. 您应该考虑使用cut ,因为这应该以正确的顺序设置级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM