如何在格子中的xy图中添加水平线
How to add horizontal line to xy plot in lattice
我正在使用 lattice 生成如下图所示的图
我用来生成绘图的代码是:
xyplot(RMSE ~ Dimensions, data=afterdim,groups = paste("", Dim_Reduction),
type = "l", auto.key =list(spline = "bottom", points = FALSE, lines = TRUE),
xlab="Dimensions", ylab="RMSE",scales=list(ylim=c(0,1)))
我想在这张图的 y-axis 0.23
处用不同的颜色画一条水平线。添加水平线的目的是显示基线。这可能吗?
我用来绘制的示例数据是:
Dim_Reduction, Dimensions, Time, RMSE
PCA, 9, 15.39, 0.287
PCA, 8, 16.84, 0.290
PCA, 7, 14.13, 0.289
PCA, 6, 12.14, 0.292
PCA, 5, 12.54, 0.293
PCA, 4, 11.23, 0.295
ICA, 11, 20.23, 0.287
ICA, 10, 20.88, 0.288
ICA, 9, 16.34, 0.290
ICA, 8, 16.99, 0.294
ICA, 7, 14.34, 0.291
ICA, 6, 13.33, 0.292
ICA, 5, 12.12, 0.294
你可以试试下面的代码
# data
afterdim <- read.table(header = TRUE, text = "
Dim_Reduction Dimensions Time RMSE
PCA, 9, 15.39, 0.287
PCA, 8, 16.84, 0.290
PCA, 7, 14.13, 0.289
PCA, 6, 12.14, 0.292
PCA, 5, 12.54, 0.293
PCA, 4, 11.23, 0.295
ICA, 11, 20.23, 0.287
ICA, 10, 20.88, 0.288
ICA, 9, 16.34, 0.290
ICA, 8, 16.99, 0.294
ICA, 7, 14.34, 0.291
ICA, 6, 13.33, 0.292
ICA, 5, 12.12, 0.294")
xyplot(RMSE ~ Dimensions, data=afterdim,groups = Dim_Reduction,
type = c("l", "g"), auto.key =list(spline = "bottom", points = FALSE, lines = TRUE),
xlab="Dimensions", ylab="RMSE",scales=list(ylim=c(0,1)),
panel=function(...) {
panel.xyplot(...)
panel.abline(h=.29)
})
需要阅读 'scales' 和 ?llines:
上的 ?xyplot 部分
mylattice <- xyplot(RMSE ~ Dimensions, data=afterdim, groups = Dim_Reduction,
panel =function(x,y,groups,...){
panel.xyplot(x,y,groups,...);
panel.lines(x=3:12, y=rep(0.23,10), col="red") },
scales=list( y=list( limits= c(0.22,0.3))),
type = "l", xlab="Dimensions", ylab="RMSE")
png(); print(mylattice); dev.off()
我正在使用 lattice 生成如下图所示的图
我用来生成绘图的代码是:
xyplot(RMSE ~ Dimensions, data=afterdim,groups = paste("", Dim_Reduction),
type = "l", auto.key =list(spline = "bottom", points = FALSE, lines = TRUE),
xlab="Dimensions", ylab="RMSE",scales=list(ylim=c(0,1)))
我想在这张图的 y-axis 0.23
处用不同的颜色画一条水平线。添加水平线的目的是显示基线。这可能吗?
我用来绘制的示例数据是:
Dim_Reduction, Dimensions, Time, RMSE
PCA, 9, 15.39, 0.287
PCA, 8, 16.84, 0.290
PCA, 7, 14.13, 0.289
PCA, 6, 12.14, 0.292
PCA, 5, 12.54, 0.293
PCA, 4, 11.23, 0.295
ICA, 11, 20.23, 0.287
ICA, 10, 20.88, 0.288
ICA, 9, 16.34, 0.290
ICA, 8, 16.99, 0.294
ICA, 7, 14.34, 0.291
ICA, 6, 13.33, 0.292
ICA, 5, 12.12, 0.294
你可以试试下面的代码
# data
afterdim <- read.table(header = TRUE, text = "
Dim_Reduction Dimensions Time RMSE
PCA, 9, 15.39, 0.287
PCA, 8, 16.84, 0.290
PCA, 7, 14.13, 0.289
PCA, 6, 12.14, 0.292
PCA, 5, 12.54, 0.293
PCA, 4, 11.23, 0.295
ICA, 11, 20.23, 0.287
ICA, 10, 20.88, 0.288
ICA, 9, 16.34, 0.290
ICA, 8, 16.99, 0.294
ICA, 7, 14.34, 0.291
ICA, 6, 13.33, 0.292
ICA, 5, 12.12, 0.294")
xyplot(RMSE ~ Dimensions, data=afterdim,groups = Dim_Reduction,
type = c("l", "g"), auto.key =list(spline = "bottom", points = FALSE, lines = TRUE),
xlab="Dimensions", ylab="RMSE",scales=list(ylim=c(0,1)),
panel=function(...) {
panel.xyplot(...)
panel.abline(h=.29)
})
需要阅读 'scales' 和 ?llines:
上的 ?xyplot 部分mylattice <- xyplot(RMSE ~ Dimensions, data=afterdim, groups = Dim_Reduction,
panel =function(x,y,groups,...){
panel.xyplot(x,y,groups,...);
panel.lines(x=3:12, y=rep(0.23,10), col="red") },
scales=list( y=list( limits= c(0.22,0.3))),
type = "l", xlab="Dimensions", ylab="RMSE")
png(); print(mylattice); dev.off()