gpt4 book ai didi

r - 如何向用lattice和latticeExtra制作的图添加图例?

转载 作者:行者123 更新时间:2023-12-04 12:26:08 28 4
gpt4 key购买 nike

我使用格子和格子额外来绘制五个不同区域的观察值和预测值。我使用 xyplot 来绘制观察值,然后在lattice extra 中使用 as.layer 函数来添加预测线。我想在图表中添加一个图例,但没有任何运气。

以下是两个示例数据集以及用于绘图的代码。

示例数据集一。我只包括两个区域。

example1 <- 
structure(list(model_predict = c(10, 25, 95, 23, 56, 70, 56,
45, 25, 50), Shell_Height = c(27, 33, 115, 25, 46, 50, 35, 35,
23, 45), SAMS_region_2015 = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L), .Label = c("DMV", "LI"), class = "factor")), .Names = c("model_predict",
"Shell_Height", "SAMS_region_2015"), row.names = c(NA, -10L), class = "data.frame")

示例数据集二。也只包括两个区域。
example2 <-
structure(list(Meat_Weight = c(15, 27, 100, 15, 60, 75, 50, 37,
28, 60), Shell_Height = c(25, 30, 110, 20, 45, 48, 35, 30, 25,
50), SAMS_region_2015 = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L), .Label = c("DMV", "LI"), class = "factor")), .Names = c("Meat_Weight",
"Shell_Height", "SAMS_region_2015"), row.names = c(NA, -10L), class = "data.frame")

绘图代码
library(lattice)
library(latticeExtra)

#observed vs predicted values by SAMS region

foo<-xyplot(Meat_Weight~Shell_Height|SAMS_region_2015,data=example2,
ylab="Meat Weight (g)",xlab="Shell Height (mm)",type="p",
col="red",pch=3)

#add layer of observed values

foo <- foo +
as.layer(xyplot(model_predict~Shell_Height|SAMS_region_2015,
type = "l", data = example1, col = "blue", lwd = 4, lty = 3))

我想为图表添加一个图例,其中包含文本观察和预测以及图表中用于不同变量的符号或线条和颜色。

信息
session 信息()
R 版本 3.2.1 (2015-06-18)
平台:i386-w64-mingw32/i386(32位)
运行于:Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] grid stats graphics grDevices utils datasets
[7] methods base

other attached packages:
[1] latticeExtra_0.6-26 RColorBrewer_1.1-2 nlme_3.1-120
[4] MASS_7.3-40 xlsx_0.5.7 xlsxjars_0.6.1
[7] rJava_0.9-6 plyr_1.8.3 RODBC_1.3-12
[10] lattice_0.20-31

loaded via a namespace (and not attached):
[1] tools_3.2.1 Rcpp_0.11.6

任何帮助,将不胜感激。

最佳答案

xyplot()的 key 参数需要一个参数列表,可以很好地处理这个问题。唯一真正的技巧是,如果您包含 lines列表中的元素,您可以在其中包含一个额外的 type=元素指示哪些项目将绘制为点,哪些项目将绘制为线。

## Set up a key
foo_key <- list(x = .97, y = .92, corner = c(1, 1),
text = list(c("Observed", "Predicted")),
lines = list(type = c("p", "l"), col = c("red", "blue"),
pch = 3, lwd = 4, lty = 3))

## Then pass it in to xyplot() via its 'key' argument
foo <- xyplot(Meat_Weight~Shell_Height|SAMS_region_2015,data=example2,
ylab="Meat Weight (g)",xlab="Shell Height (mm)",
type = "p", col = "red", pch = 3,
key = foo_key)
## ... and carry on with your code, adding a layer and printing the whole thing
foo <- foo +
as.layer(xyplot(model_predict~Shell_Height|SAMS_region_2015,
type = "l", data = example1, col = "blue", lwd = 4, lty = 3))

foo

enter image description here

关于r - 如何向用lattice和latticeExtra制作的图添加图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33532403/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com