简体   繁体   English

设置 x 轴与 y 轴在 0 处与点阵相交

[英]set the x-axis to meet y-axis at 0 with lattice

I am struggling with some plots in R. At the moment I am running R version: R version 3.2.0 (2015-04-16) Platform: i386-w64-mingw32/i386 (32-bit) Running under: Windows 7 (build 7601) Service Pack 1我正在努力处理 R 中的一些情节。目前我正在运行 R 版本:R 版本 3.2.0 (2015-04-16) 平台:i386-w64-mingw32/i386 (32-bit) 运行在:Windows 7 (内部版本 7601) 服务包 1

I really hope someone can give me some advise on how to set my x-axis to meet the y-axis at 0. I am using lattice to create a barchart with error-bars created using a standard deviation values but it seems that the x-axis won't start at 0 while intersecting the y-axis.我真的希望有人可以给我如何设置我的X轴,以满足y轴为0。我使用的一些建议lattice来创建一个barchart错误酒吧使用标准偏差值创建的,但它似乎是X -axis 在与 y 轴相交时不会从 0 开始。 As you can see from here:正如您从这里看到的:

R_barchar_with_error_bars

I am using this dataset "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

And a friend helped me to have this code working:一位朋友帮助我让这段代码正常工作:

#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)
         }
)

I have tried to set xaxs='i' and yaxs='i' or even set xlim=(0,) but it won't do the trick, so I was wondering if anyone have any idea on how to sort it.我试图设置xaxs='i'yaxs='i'甚至设置xlim=(0,)但它不会成功,所以我想知道是否有人知道如何对其进行排序。

Yes, yaxs doesn't work in lattice .是的, yaxslattice不起作用。 See here for how it is handled.请参阅此处了解如何处理。

However, I got this to work by skipping the prepanel and calculating the ylim directly in the barchart call.但是,我通过跳过prepanel并直接在barchart调用中计算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)
         }
)

I added 4% to the top limit so the error bar doesn't fall on the bounding box.我在上限上加了 4%,这样误差条就不会落在边界框上。 I used zero for the lower limit rather than some other minimum value in the data since that is more appropriate for bar charts.我使用零作为下限而不是数据中的其他最小值,因为这更适合条形图。

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

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