如何在条形图中包含NA观察的类别级别

时间:2017-05-16 10:13:43

标签: r

我想在R中显示一个图,显示一些我们没有收到任何数据的区域。所以,我希望/需要显示那些没有数据的情节中包含的区域,以便我们可以在报告中显示我们没有收到信息。我在Excel中做了这个(见附件),并希望在R中完成。

下面,找到我在R中开始做的代码但是当我运行它时显示错误:

lags = c(15, 24, 34, 44,54,64,68,70,72,74,76,78)                                
hf = c(2.56,2.16,4.23,0.00,2.73,,,,10.81,3.13,16.67,10.34)                       
district = c(2.56,2.16,21.69,0.00,2.73,,,,10.81,2.34,33.33,7.76)                      
province = c(2.56,2.16,21.69,0.00,2.73,,,,10.81,2.34,33.33,7.76)
deviation = matrix(c(hf,district,province), nrow=length(lags), ncol=3, 
                    dimnames=list(lags, c("hf","district","province")))         
# Create a matrix deviation with the data  
deviation1 = t(deviation)                             # Transpose the deviation matrix  


#par(mfrow = c(1, 1))                                   # Open a new window for the plot  
par(las=1,cex.axis=0.9,cex=0.9,cex.lab=0.9,cex.main=0.9,lwd=1)
bp2 <- barplot(deviation1,                             # Data (bar heights) to plot  
               beside=TRUE,                            # Plot the bars beside one another; default is to plot stacked bars  
               space=c(0.2,0.8),                       # Amount of space between i) bars within a group, ii) bars between groups  
               names.arg=c("Prov1", "Prov2", "Prov3", "Prov4", "Prov5","Prov6","Prov7", "Prov8", "Prov9", "Prov10",
                           "Prov11", "Prov12"),    #Names for the bars  
               col=c("yellow4","orange1","darkred"),             # Color of the bars  
               border="black",                         # Color of the bar borders  
               main=c(""),                     # Main title for the plot   Método utilizado
               xlab="",                                # X-axis label  
               ylab="",                                # Y-axis label  
               font.lab=2, ylim=c(0,100))              # Font to use for the axis labels: 1=plain text, 2=bold, 3=italic, 4=bold italic  

legend("topleft",                                      # Add a legend to the plot  
       legend=c("Health Facility","District","Province"),              # Text for the legend  
       fill=c("yellow4","orange1","darkred"),bty = "n", cex=0.8) # Fill for boxes of the legend  
text(x=bp2,2,y=c(deviation1[1:36]),labels=c(deviation1[1:36]),cex=0.8,pos=3)


 Error in c(2.56, 2.16, 4.23, 0, 2.73, , , , 10.81, 3.13, 16.67, 10.34) : 
      argument 6 is empty

附上Excel中生成的数字:figure 1 - deviations

提前感谢您的帮助。

此致 荣高。

0 个答案:

没有答案