如何在R中的条形图中包含最大和最小偏差

如何在R中的条形图中包含最大和最小偏差,r,R,我想在R中绘制一个类似于我在Excel中所做的条形图,以计算偏差,但我不知道如何包括最大偏差和最小偏差。我已经开始在R中编写相同的绘图,我需要您的帮助来提出相同的解决方案。请参见下面的代码和Excel中的图形示例 group = c(15, 24, 34, 44,54,55,57,59,61,64,66,68) district = c(19.52, 3.33, 13.33,9.33,53.89,46.67,11.67,27.78

我想在R中绘制一个类似于我在Excel中所做的条形图,以计算偏差,但我不知道如何包括最大偏差和最小偏差。我已经开始在R中编写相同的绘图,我需要您的帮助来提出相同的解决方案。请参见下面的代码和Excel中的图形示例

group  = c(15, 24, 34, 44,54,55,57,59,61,64,66,68)                                
district = c(19.52, 3.33, 13.33,9.33,53.89,46.67,11.67,27.78,30.48,14.44,12.00,20.00)  # Rates for Total deviation


deviation = matrix(c(district), nrow=length(group), ncol=1, 
                    dimnames=list(group, c("district")))         

deviation1 = t(deviation)                             

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("district 1", "district 2 ", "district 3", "district 4", "district 5","district 6",
                           "district 7", "district 8", "district 9", "district 10", "district 11", "district 12"),    #Names for the bars  
               col=c("yellow4"),             # 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="Percentage (%)",                                # 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("Total Deviation","Max","Min"),              # Text for the legend  
       fill=c("yellow4"),bty = "n", cex=0.8) # Fill for boxes of the legend  
text(x=bp2,2,y=c(deviation1[1:12]),labels=c(deviation1[1:12]),cex=0.8,pos=3)
组=c(15,24,34,44,54,55,57,59,61,64,66,68)
地区=c(19.52,3.33,13.33,9.33,53.89,46.67,11.67,27.78,30.48,14.44,12.00,20.00)#总偏差率
偏差=矩阵(c(地区),nrow=长度(组),ncol=1,
dimnames=列表(c组(“地区”))
偏差1=t(偏差)
par(las=1,cex.axis=0.9,cex=0.9,cex.lab=0.9,cex.main=0.9,lwd=1)

bp2您需要使用
箭头
函数绘制错误条,如下所示,还需要定义您没有在问题中提出的最小和最大偏差,我从您的excel表格中获取了值:

mindev <- c(0,.0300,0,0,.3667,.1,.033,0,0,0,0,.03333)
maxdev <- c(0.5,.0333,.335,.1667,.900,.8,.2,.733,.5,.4667,.3333,.36667)


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("district 1", "district 2 ", "district 3", "district 4", "district 5","district 6",
                           "district 7", "district 8", "district 9", "district 10", "district 11", "district 12"),    #Names for the bars  
               col=c("yellow4"),             # Color of the bars  
               border="white",                         # Color of the bar borders  
               main=c(""),                     # Main title for the plot   Método utilizado
               xlab="",                                # X-axis label  
               ylab="Percentage (%)",                                # 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  

text(x=bp2,2,y=c(deviation1[1:12]),labels=c(deviation1[1:12]),cex=0.8,pos=3)
arrows(bp2, mindev*100, bp2,
       maxdev*100, lwd = 1.5, angle = 90,
       code = 3, length = 0.05)

mindev您需要使用
箭头
函数绘制错误条,如下所示,还需要定义您没有在问题中提出的最小和最大偏差,我从您的excel表格中获取了值:

mindev <- c(0,.0300,0,0,.3667,.1,.033,0,0,0,0,.03333)
maxdev <- c(0.5,.0333,.335,.1667,.900,.8,.2,.733,.5,.4667,.3333,.36667)


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("district 1", "district 2 ", "district 3", "district 4", "district 5","district 6",
                           "district 7", "district 8", "district 9", "district 10", "district 11", "district 12"),    #Names for the bars  
               col=c("yellow4"),             # Color of the bars  
               border="white",                         # Color of the bar borders  
               main=c(""),                     # Main title for the plot   Método utilizado
               xlab="",                                # X-axis label  
               ylab="Percentage (%)",                                # 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  

text(x=bp2,2,y=c(deviation1[1:12]),labels=c(deviation1[1:12]),cex=0.8,pos=3)
arrows(bp2, mindev*100, bp2,
       maxdev*100, lwd = 1.5, angle = 90,
       code = 3, length = 0.05)

mindev对于这类数据,可能尝试使用geom。对于这类数据,可能尝试使用geom。嗨,K..pradeeep!非常感谢你的帮助。Regards@AntonioRungo,谢谢,如果该解决方案对您有帮助,请在答案上打绿色勾。嗨,K…,普拉迪埃!非常感谢你的帮助。Regards@AntonioRungo,谢谢,如果该解决方案对您有帮助,请在答案上打绿色勾。