[英]xyplot in R with several plots in one panel, lattice
I have a test data set constructed in the following way:我有一个按以下方式构建的测试数据集:
d <- seq.Date(as.Date("2000-01-01"), as.Date("2000-01-08"), by=1)
df1 <- data.frame(time = d, type = 'type1', value = runif(length(d)))
df2 <- data.frame(time = d, type = 'group1', value = runif(length(d)))
df3 <- data.frame(time = d, type = 'group2', value = runif(length(d)))
df4 <- data.frame(time = d, type = 'pen', value = runif(length(d)))
df <- rbind(df1, df2, df3, df4)
I want to plot the data in lattice using the xyplot
function, where the panels are displayed in one column.我想使用xyplot
函数在点阵中绘制数据,其中面板显示在一列中。 I can do the following:我可以执行以下操作:
xyplot(value~time | type, df, layout=c(1, length(levels(df$type))))
This way I have 4 rows (as many types I have).这样我有 4 行(我有很多类型)。 But, actually I would like to plot group1 and group2 in the same panel with different colors (so that I have only 3 rows).但是,实际上我想在同一个面板中用不同的颜色绘制 group1 和 group2(这样我只有 3 行)。 Could someone please help me out on this?有人可以帮我解决这个问题吗?
You can create one panel for all 'group' records through temporarily disregarding the digits in 'group1' and 'group2'.您可以通过暂时忽略“group1”和“group2”中的数字为所有“group”记录创建一个面板。 That's what gsub()
is doing in the below code – it's replacing all digits in your 'group' types with ""
, thus creating a conditional plot with three panels instead of four.这就是gsub()
在下面的代码中所做的——它将“组”类型中的所有数字替换为""
,从而创建一个包含三个面板而不是四个面板的条件图。
Now, the only thing left to do is define different colors for all levels(df$type)
.现在,剩下要做的就是为所有levels(df$type)
定义不同的颜色。 The names()
operation is not mandatory; names()
操作不是强制性的; it just helps to maintain an overview of the different factor levels and, in particular, their order.它只是有助于维护不同因子水平的概览,特别是它们的顺序。
# colors
clr = c("black", "black", "orange", "black")
names(clr) = levels(df$type) # for clarification only
# grouped scatterplot
xyplot(value~time | gsub("group[[:digit:]]", "group", type), df, group = type
, layout = c(1, length(levels(df$type)) - 1), col = clr, cex = 1.2, pch = 20)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.