简体   繁体   中英

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. 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). But, actually I would like to plot group1 and group2 in the same panel with different colors (so that I have only 3 rows). 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'. 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.

Now, the only thing left to do is define different colors for all levels(df$type) . The names() operation is not mandatory; 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)

坐标图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM