设置 x 轴与 y 轴相交于 0 点阵
set the x-axis to meet y-axis at 0 with lattice
我正在为 R 中的一些情节而苦苦挣扎。目前我是 运行 R 版本:
R 版本 3.2.0 (2015-04-16)
平台:i386-w64-mingw32/i386(32 位)
运行 下:Windows 7(内部版本 7601)Service Pack 1
我真的希望有人能给我一些关于如何设置我的 x 轴以在 0 处与 y 轴相交的建议。我正在使用 lattice
创建一个 barchart
,但有错误-使用标准偏差值创建的条形图,但似乎 x 轴在与 y 轴相交时不会从 0 开始。
从这里可以看出:
我正在使用这个数据集 "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,)
但它不会成功,所以我想知道是否有人知道如何设置排序。
是的,yaxs
在 lattice
中不起作用。有关处理方式,请参阅 。
但是,我通过跳过 prepanel
并直接在 barchart
调用中计算 ylim
来实现它。
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% 添加到上限,因此误差条不会落在边界框上。我使用零作为下限而不是数据中的其他最小值,因为这更适合条形图。
我正在为 R 中的一些情节而苦苦挣扎。目前我是 运行 R 版本: R 版本 3.2.0 (2015-04-16) 平台:i386-w64-mingw32/i386(32 位) 运行 下:Windows 7(内部版本 7601)Service Pack 1
我真的希望有人能给我一些关于如何设置我的 x 轴以在 0 处与 y 轴相交的建议。我正在使用 lattice
创建一个 barchart
,但有错误-使用标准偏差值创建的条形图,但似乎 x 轴在与 y 轴相交时不会从 0 开始。
从这里可以看出:
我正在使用这个数据集 "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,)
但它不会成功,所以我想知道是否有人知道如何设置排序。
是的,yaxs
在 lattice
中不起作用。有关处理方式,请参阅
但是,我通过跳过 prepanel
并直接在 barchart
调用中计算 ylim
来实现它。
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% 添加到上限,因此误差条不会落在边界框上。我使用零作为下限而不是数据中的其他最小值,因为这更适合条形图。