将x轴设置为在0处与晶格相交的y轴

时间:2015-05-18 09:25:26

标签: r bar-chart lattice

我正在努力研究R中的一些情节。目前我正在运行R版本: R版本3.2.0(2015-04-16) 平台:i386-w64-mingw32 / i386(32位) 运行于:Windows 7(内部版本7601)Service Pack 1

我真的希望有人可以给我一些关于如何设置我的x轴以满足y轴为0的建议。我正在使用lattice创建一个barchart并创建了错误栏使用标准偏差值,但似乎x轴在与y轴相交时不会从0开始。 正如你从这里看到的那样:

R_barchar_with_error_bars

我正在使用此数据集" order":

 Order    Area   Count        Stdev
order1  Area 1  224122  37263.35097
order2  Area 1    2091  253.0564825
order3  Area 1   45867  4450.600737
order4  Area 1   32816  4563.089816
order1  Area 2   71548  2046.367025
order2  Area 2     309  24.74873734
order3  Area 2   22564  315.3696244
order4  Area 2   10686  987.1210665

一位朋友帮助我让这段代码正常工作:

#it is better to prepare the prepanel before with all the setting 
prepanel=function(y, Stdev, subscripts=subscripts, ...){
  uy <- as.numeric(y+Stdev[subscripts])#upper y so max value for error bar
  ly <- as.numeric(y-Stdev[subscripts])#lower y so min value for error bar
  list(ylim=range(y,uy,ly, finite=TRUE))
}
panel.err=function(x, y, subscripts, groups, Stdev, box.ratio, ...){
  d <- 1/(nlevels(groups)+nlevels(groups)/box.ratio)
  g <- (as.numeric(groups[subscripts])-1); g <- (g-median(g))*d
  panel.arrows(as.numeric(x)+g,y-Stdev[subscripts], as.numeric(x)+g, y+Stdev[subscripts],
               code=3,angle=90, length=0.025)
}

barchart(Count~Order,#tells it what x and y are for the plot
         groups=Area,#tells it the subdivision in x
         data=order,#tells it where to get the info from
         Stdev=order$Stdev,
         auto.key=list(points=FALSE,rectangles=TRUE,columns=2, title="Area",cex.title=1),#adds the key how to spread the legend and the title of it
         main="Order per Area", #this create the title of the graph 
         #scales=list(x=list(rot=0))), #rotate the x-axis labels 
         par.settings=list(superpose.polygon=list(col=grey.colors(2))),
         prepanel=prepanel,
         panel=function(x, y, subscripts, groups, Stdev, box.ratio, ...){
           panel.barchart(x, y, subscripts=subscripts,
                          groups=groups, box.ratio=box.ratio, ...)
           panel.err(x, y, subscripts=subscripts,
                     groups=groups, box.ratio=box.ratio, Stdev=order$Stdev)
         }
)

我试图设置xaxs='i'yaxs='i'甚至设置xlim=(0,)但是它不会做这个伎俩,所以我想知道是否有人知道如何排序。

1 个答案:

答案 0 :(得分:2)

是的,yaxslattice中不起作用。请参阅here了解其处理方式。

但是,我通过跳过prepanel并直接在ylim电话中计算barchart来实现此目的。

library(lattice)
order=data.frame(Order=rep(paste0("order",1:4),times=2),
                 Area=rep(paste0("Area ",1:2),each=4),
                 Count=c(224122,2091,45867,32816,71548,309,22564,10686),
                 Stdev=c(37263,253,4450,4563,2046,25,315,987))

panel.err=function(x, y, subscripts, groups, Stdev, box.ratio, ...){
  d <- 1/(nlevels(groups)+nlevels(groups)/box.ratio)
  g <- (as.numeric(groups[subscripts])-1); g <- (g-median(g))*d
  panel.arrows(as.numeric(x)+g,y-Stdev[subscripts], as.numeric(x)+g, y+Stdev[subscripts],
               code=3,angle=90, length=0.025)
}

barchart(Count~Order,#tells it what x and y are for the plot
         groups=Area,#tells it the subdivision in x
         data=order,#tells it where to get the info from
         Stdev=order$Stdev,
         auto.key=list(points=FALSE,rectangles=TRUE,columns=2, title="Area",cex.title=1),#adds the key how to spread the legend and the title of it
         main="Order per Area", #this create the title of the graph 
         #scales=list(x=list(rot=0))), #rotate the x-axis labels 
         par.settings=list(superpose.polygon=list(col=grey.colors(2))),
         ylim=c(0,max(order$Count+order$Stdev)*1.04),
         panel=function(x, y, subscripts, groups, Stdev, box.ratio, ...){
           panel.barchart(x, y, subscripts=subscripts,
                          groups=groups, box.ratio=box.ratio, ...)
           panel.err(x, y, subscripts=subscripts,
                     groups=groups, box.ratio=box.ratio, Stdev=order$Stdev)
         }
)

我在上限中添加了4%,因此错误栏不会落在边界框上。我使用零作为下限而不是数据中的其他最小值,因为这更适合条形图。