I have an xyplot grouped by a factor. I plot salinity (AvgSal = Y) against time (DayN = X) for 16 different sites, site being the factor (SiteCode). I want all of the site plots stacked so I set the layout to one column with 16 rows.
First issue: I would like to remove the strip above each plot that contains only the SiteCode label, as it takes up a lot of space. Instead, I could introduce a second column with the SiteCode names or introduce a legend in the same strip as the plot. Can anyone tell me how to remove the label strip and introduce labelling in a different fashion?
Here's the code:
Sample Data
zz <- "SiteCode DayN AvgSal
1 CC 157 29.25933
2 CC 184 29.68447
3 DW 160 26.47328
4 DW 190 29.07192
5 FP 157 30.40344
6 FP 184 30.58842
7 IN 157 30.25319
8 IN 184 29.20716
9 IP 156 29.09548
10 IP 187 27.86887
11 LB 162 27.58603
12 LB 191 28.86910
13 LR 160 28.06035
14 LR 190 29.52723
15 PB 159 30.10903
16 PB 188 29.46113
17 PG 161 29.67765
18 PG 189 28.90864
19 SA 162 23.23362
20 SA 190 26.96549
21 SH 156 24.86752
22 SH 187 23.12184
23 SP 161 18.95347
24 SP 189 19.16433
25 VC 162 29.49714
26 VC 186 29.66493
27 WP 157 27.33631
28 WP 183 27.18465
29 YB 157 30.50193
30 YB 183 30.49824
31 ZZ 159 30.14175
32 ZZ 186 29.44860"
Data <- read.table(text=zz, header = TRUE)
xyplot(AvgSal~DayN | factor(SiteCode),
layout = c(1, 16),
xlab = "Time (Day of the year)",
ylab = "Average Salinity (PSU)",
strip = function(bg = 'white', ...) strip.default(bg = 'white', ...),
data = Data, type = c("a","p"))
Second issue: The strips are ordered by SiteCode alphabetically, or in the original order had them entered into the csv datafile. I would like to order them from highest to lowest average salinity, but I do not know how to achieve this. Can anyone help?
I have tried using order ()
to change the data layout so it is sorted by ascending salinity before running the plot, but this doesn't seem to work, even when I remove the rownames.
I also tried the solution in How to change the order of the panels in simple Lattice graphs, by assigning set levels, i.e.
levels(Data$SiteCode) <- c("SP", "SA", "SH", "LB", "DW",
"LR", "PG", "VC", "ZZ", "PB",
"WP", "IP", "IN", "CC", "FP", "YB")
This seemed to change the label above each panel, but it did not change the corresponding plots, leaving plots with the wrong label. It also seems like an inefficient way to go about it if I want to do this process for a large number of variables.
Any help will be really appreciated! Cheers :)
The solutions always seem so simple, in hindsight.
Issue 1: I had to use levels()
and reorder()
in the factor command, where X = the numeric factor I want to order SiteCode by.
xyplot(AvgSal ~ DayN | factor(SiteCode, levels(reorder(SiteCode, X),
Issue 2: Turned out to be very simple, once I knew what I was doing. Just had to 'turn off' strip, with the following getting rid of the title strip altogether.
strip = FALSE
In addition, I decided having the strip vertically aligned on the left would be nice:
strip = FALSE,
strip.left = strip.custom(horizontal = FALSE)