如何根据列在格子中绘制图形
How to plot a graph in lattice based on columns
我有一个这样的数据框:
epochs lm le kd
-------|-------|------|----
1 | 0.38 | 0.25 | 0.21
2 | 0.34 | 0.22 | 0.44
3 | 0.45 | 0.33 | 0.22
我想在 lattice
中使用 xyplot
绘制此图,并与 lm
、le
和 kd
交互。 X 轴为 epochs
,Y 轴的范围为 0.10 到 0.60(取决于数据)
我在下面尝试过但它不起作用,因为我不知道在 Y 轴上放什么?
xyplot(epochs ~ 'whattoputhere??', data=data,
+ groups = paste("Le =", le, "lm =", lm, "kd = ", kd),
+ type = "l",
+ auto.key =
+ list(space = "right", points = FALSE, lines = TRUE))
一般情况下,使用 "long data" 的格函数会更容易,不幸的是你的 "wide"。 melt
函数是送给 R 用户的一份很棒的礼物(谢谢你,Hadley)。
> dat <- read.table(text="epochs | lm | le | kd
+ 1 | 0.38 | 0.25 | 0.21
+ 2 | 0.34 | 0.22 | 0.44
+ 3 | 0.45 | 0.33 | 0.22", header=TRUE,sep="|")
> require(reshape2)
Loading required package: reshape2
> datm <- melt(dat, id.var="epochs")
> str(datm)
'data.frame': 9 obs. of 3 variables:
$ epochs : num 1 2 3 1 2 3 1 2 3
$ variable: Factor w/ 3 levels "lm","le","kd": 1 1 1 2 2 2 3 3 3
$ value : num 0.38 0.34 0.45 0.25 0.22 0.33 0.21 0.44 0.22
xyplot(value ~ epochs, groups=variable, datm, type="b",
auto.key = list( space="right", points = FALSE, lines = TRUE) )
我有一个这样的数据框:
epochs lm le kd
-------|-------|------|----
1 | 0.38 | 0.25 | 0.21
2 | 0.34 | 0.22 | 0.44
3 | 0.45 | 0.33 | 0.22
我想在 lattice
中使用 xyplot
绘制此图,并与 lm
、le
和 kd
交互。 X 轴为 epochs
,Y 轴的范围为 0.10 到 0.60(取决于数据)
我在下面尝试过但它不起作用,因为我不知道在 Y 轴上放什么?
xyplot(epochs ~ 'whattoputhere??', data=data, + groups = paste("Le =", le, "lm =", lm, "kd = ", kd), + type = "l", + auto.key = + list(space = "right", points = FALSE, lines = TRUE))
一般情况下,使用 "long data" 的格函数会更容易,不幸的是你的 "wide"。 melt
函数是送给 R 用户的一份很棒的礼物(谢谢你,Hadley)。
> dat <- read.table(text="epochs | lm | le | kd
+ 1 | 0.38 | 0.25 | 0.21
+ 2 | 0.34 | 0.22 | 0.44
+ 3 | 0.45 | 0.33 | 0.22", header=TRUE,sep="|")
> require(reshape2)
Loading required package: reshape2
> datm <- melt(dat, id.var="epochs")
> str(datm)
'data.frame': 9 obs. of 3 variables:
$ epochs : num 1 2 3 1 2 3 1 2 3
$ variable: Factor w/ 3 levels "lm","le","kd": 1 1 1 2 2 2 3 3 3
$ value : num 0.38 0.34 0.45 0.25 0.22 0.33 0.21 0.44 0.22
xyplot(value ~ epochs, groups=variable, datm, type="b",
auto.key = list( space="right", points = FALSE, lines = TRUE) )