Adjusting legend and colors in ggplot2 - r

I am using following data:
testdf = structure(list(var1 = c(14.9, 15.5, 16.5, 16.6, 15.1, 13.8, 13.2,
27.6, 22.3, 29.1, 18.4, 14.8, 15.7, 14.3, 15.5, 15.8, 17.6, 14.9,
16.9, 20.8, 13.9, 20.1, 16.9, 24.7, 15.2, 15.9, 15.8, 17.1, 15.9,
17.3, 17.5, 14.7, 21, 12, 18.6, 16.1, 16.1, 15.8, 15.9, 13.9,
13.6, 13.6, 14.2, 13.9, 14.1, 13.9, 13.7, 13.6, 13.9, 13.2),
age = c(7L, 7L, 8L, 10L, 7L, 11L, 9L, 14L, 12L, 15L, 10L,
12L, 12L, 9L, 9L, 10L, 15L, 10L, 12L, 14L, 15L, 13L, 15L,
13L, 11L, 9L, 14L, 12L, 12L, 15L, 13L, 12L, 15L, 7L, 14L,
8L, 10L, 8L, 9L, 9L, 8L, 10L, 9L, 9L, 11L, 10L, 10L, 9L,
9L, 9L)), .Names = c("var1", "age"), row.names = c(NA, 50L
), class = "data.frame")
I can have a histogram with following code:
ggplot(testdf)+geom_histogram(aes(var1,group=age,color=age,fill=age))
But how can I get ages 7,8,9,10,11,12,13,14,15 in legend and different colours for all these age groups, eg rainbow(9)
I tried following codes but they work only partially:
ggplot(testdf)+geom_histogram(aes(var1,group=age,color=age,fill=age))+scale_colour_continuous(breaks=c(7:15),color=rainbow(9))
ggplot(testdf)+geom_histogram(aes(var1,group=age,color=age,fill=age, legend=F))+scale_colour_continuous(breaks=c(7:15))

As #nrussel said, you have to convert age to a factor variable. You can do that within ggplot2. Moreover, you don't really need the group and colour parameters in this case.
With:
ggplot(testdf)+
geom_histogram(aes(var1, fill=as.factor(age)))
you should get the following result:

Related

Error when running GLMM in lme4: Error in pwrssUpdate

I have a dataset containing male and female data. I have the response variable metabolic rate, and several predictors (Behaviour, Temperature 1, Temperature 2, Activity, Sex, Body Size, and Body Mass).
First, I conduct the GLMM using an identity link on male and female combined data:
glmer(log(Metabolic_Rate)~ Temperature.1 + Behaviour * Temperature.2 + Sex + Activity + Body_Size + Body_Mass + (1|Week), data= AMRdata, family = Gamma(link = 'identity'))
And then run model simplification. The model works just fine and I have no error messages.
Then, I separate the data by sex and run the model on just female data:
females<-subset(AMRdata,Sex=="F")
glmer(log(Metabolic_Rate)~ Temperature.1 + Behaviour * Temperature.2 + Activity + Body_Size + Body_Mass + (1|Week), data = females, family = Gamma(link = 'identity'))
I am greeted by the error message:
Error in pwrssUpdate(pp, resp, tol = tolPwrss, GQmat = GQmat, compDev = compDev, :
(maxstephalfit) PIRLS step-halvings failed to reduce deviance in pwrssUpdate
Removal of Temperature.2 takes removes this error, but I really need to keep it in the model.
Does anyone have any suggestions as to how to remove the error?
I'm using lme4 version 1.1.21, and the female model doesn't work if I don't use the log transformation, either. I have tried using a log link, but I get the same error message.
Some data:
structure(list(Metabolic_Rate = c(8.79514591, 16.71840387, 14.1932374,
10.90741585, 10.7436911, 14.97469781, 19.88267242, 12.43274774,
15.12038794, 11.84916117, 11.05467852, 19.53495917, 12.14440531,
12.09564168, 6.78392472, 10.51570692, 8.527792046, 8.731880804,
10.71404367), Behaviour = c(23L, 17L, 14L, 7L, 99L, 78L, 90L,
1L, 9L, 29L, 76L, 66L, 43L, 36L, 13L, 4L, 82L, 14L, 59L), Temperature.1 = c(21.9,
21.7, 18.52, 19.85, 20.45, 20.54, 21.7, 22, 21.32, 21.4, 21.44,
22.1, 22.22, 22.25, 20.43, 20.9, 21.63, 21.2, 21.52), Temperature.2 = c(17.5,
15.6, 12.5, 19.8, 16.6, 20.8, 21.4, 21.9, 21, 21.3, 20.5, 22,
22.1, 22.2, 20.6, 21.2, 21.9, 21.1, 21.5), Activity = c(39.54664352,
66.75914352, 40.85949074, 44.8505787, 37.20023148, 69.75388889,
72.43981481, 70.42199074, 20.71481481, 77.27662037, 62.21712963,
93.22673611, 82.39247685, 89.42141204, 35.35729167, 31.97777778,
74.65821759, 40.80590278, 54.3755787), Sex = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("F", "M"), class = "factor"), Body_Size = c(7.6,
5.8, 7.9, 7.6, 8, 7.5, 7.9, 7.6, 7.2, 7.8, 7.8, 7.4, 7.1, 8.4,
6, 7.9, 7.2, 7, 8.2), Body_Mass = c(0.747, 0.55, 0.76, 0.673,
0.691, 0.683, 0.689, 0.789, 0.6, 0.612, 0.637, 0.511, 0.582,
0.603, 0.408, 0.527, 0.666, 0.483, 0.602), Week = c(1L, 1L, 2L,
3L, 3L, 3L, 3L, 4L, 5L, 6L, 6L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 10L
)), class = "data.frame", row.names = c(NA, -19L))

Splitting a Time Series Data

I'm trying to split an earthquake data "A" into a test and a training set.
As I used the code for time series slicing it gave this error.
Error seq.default(1, min(ncol(x) - 1, len), by = 1) : wrong sign in 'by' argumentused .
Thus this the data,
structure(list(Month. = c(9L, 9L, 7L, 3L, 5L, 1L, 6L, 6L, 6L,
2L, 2L, 1L, 10L, 9L, 7L, 6L, 5L, 5L, 4L, 1L), Day. = c(19L, 22L,
24L, 20L, 20L, 7L, 29L, 18L, 16L, 23L, 17L, 26L, 22L, 9L, 17L,
12L, 16L, 11L, 27L, 25L), Mag. = c(7.1, 4.6, 4.5, 5.3, 4.9, 4.1,
4.5, 4, 5.8, 4.1, 4, 4.1, 4.3, 4, 4.1, 4, 4, 4, 4, 4), Lat = c(18.55,
18.02, 18.06, 18.07, 18.16, 18.18, 18.16, 18.06, 18.16, 18.17,
17.91, 18.09, 17.84, 17.82, 18.62, 17.84, 17.93, 17.72, 18.11,
18.08), Lon = c(-98.5, -98.4, -97.2, -98.52, -99.95, -99.72,
-99.15, -100.02, -99.2, -99.37, -98.24, -99.58, -97.7, -99.93,
-99.93, -97.74, -97.75, -99.85, -99.86, -99.98), Depth.km = c(51,
50.9, 69.7, 51.8, 62.7, 45.3, 59.6, 52.7, 52, 38, 49.4, 51.7,
64.8, 65, 75.7, 65.6, 63.7, 37.7, 52.1, 51.8), Day.since.Mw8.1.earthquake = c(11.55922454,
-350.4447106, -410.5616088, -902.2633565, -1207.132025, -1339.944618,
-1531.312454, -1542.510671, -1544.97941, -1657.711794, -1663.206956,
-1685.732604, -1781.218218, -1824.403634, -1878.53603, -1914.132917,
-1940.750532, -1945.763854, -1959.212801, -2052.762095)), row.names = c(NA,
20L), class = "data.frame")
**
I used this code for the time series slicing
I don't know what to do to clear this error. Can someone please help me out?
library(caret)
library(ggplot2)
library(pls)
myTimeControl <- trainControl(method = "timeslice",
initialWindow = 36,
horizon = 12,
fixedWindow = TRUE)
plsFitTime <- train(Day.since.Mw8.1.earthquake ~ Mag.,
data = A,
method = "pls",
preProc = c("center", "scale"),
trControl = myTimeControl)

Count mean subgroup occurrence within subgroup

I have the following dataframe:
date hour_of_day distance weather_of_the_day
2017-06-13 6 10.32 1
2017-06-13 8 2.32 1
2017-06-14 10 4.21 2
2017-06-15 7 4.56 4
2017-06-15 7 8.92 4
2017-06-16 22 2.11 3
structure(list(startdat = structure(c(17272, 17272, 17272, 17272,17272, 17272, 17272, 17272, 17272, 17272, 17272, 17272, 17272,17272, 17272, 17272, 17273, 17273, 17273, 17273), class = "Date"), hOfDay = c(22L, 16L, 12L, 13L, 18L, 19L, 19L, 16L, 22L, 10L,
10L, 16L, 11L, 20L, 9L, 15L, 18L, 12L, 16L, 18L), tripDKM = c(0.2,
6.4, 3.4, 0.8, 2.4, 2.2, 2.2, 7.3, 2.6, 3.8, 7.5, 5.8, 3.7,
2.1, 2.6, 5.2, 2.9, 1.7, 3.2, 3.1), totDMIN = c(1.85, 27.4,
8.2, 4.21666666666667, 15.65, 8.91666666666667, 11.5666666666667,
29.5166666666667, 7.01666666666667, 12.2166666666667, 15.8833333333333,
19.5666666666667, 21.7166666666667, 8.66666666666667, 11.2333333333333,
13.4, 7.58333333333333, 10.6166666666667, 6.76666666666667,
17.7), weather_day = structure(c(3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("1",
"2", "3", "4"), class = "factor")), row.names = c(1L, 2L,3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 15L, 16L, 17L, 19L, 20L, 21L, 22L), class = "data.frame")
My final goal is to have a line ggplot, where the x-axis shows the hour_of_day, the y-axis stands for the mean number of occurrences. Eventually the lines should represent the 4 weather conditions. So one line ought to represent weather_of_the_day=1, and the y axis shows how often, on average weather_day=1 has an occurrence with hour_of_day=6 (as an example) and so on for 7, 8, etc.. What I want, are not only the number of occurrences, but the average number of occurrences.
I've been struggling for 2 days with this. I've tried different approaches, with for loops and subgrouping. But non of them brought a usable solution. Thank you very much for your help in advance!
Your posted data set is a little small but this is what I would suggest. It only makes sense with more data points though. df is the set you posted.
library(dplyr)
library(ggplot2)
df_plot <- df %>%
mutate(weather_of_the_day = factor(weather_of_the_day)) %>%
group_by(hour_of_day, weather_of_the_day) %>%
summarize(occurances = n())
ggplot(data = df_plot,
aes(x = hour_of_day,
y = occurances,
group = weather_of_the_day,
color = weather_of_the_day)) +
geom_line()+
geom_point()
I'm not completely sure if this mathes your desired output, but I gave it a try:
#Importing packages
library(dplyr)
library(ggplot2)
d <- structure(list(startdat = structure(c(17272, 17272, 17272, 17272,17272, 17272, 17272, 17272, 17272, 17272, 17272, 17272, 17272,17272, 17272, 17272, 17273, 17273, 17273, 17273),
class = "Date"),
hOfDay = c(22L, 16L, 12L, 13L, 18L, 19L, 19L, 16L, 22L, 10L, 10L, 16L, 11L, 20L, 9L, 15L, 18L, 12L, 16L, 18L),
tripDKM = c(0.2, 6.4, 3.4, 0.8, 2.4, 2.2, 2.2, 7.3, 2.6, 3.8, 7.5, 5.8, 3.7, 2.1, 2.6, 5.2, 2.9, 1.7, 3.2, 3.1),
totDMIN = c(1.85, 27.4, 8.2, 4.21666666666667, 15.65, 8.91666666666667, 11.5666666666667, 29.5166666666667, 7.01666666666667, 12.2166666666667, 15.8833333333333, 19.5666666666667, 21.7166666666667, 8.66666666666667, 11.2333333333333, 13.4, 7.58333333333333, 10.6166666666667, 6.76666666666667, 17.7),
weather_day = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L),
.Label = c("1", "2", "3", "4"),
class = "factor")),
row.names = c(1L, 2L,3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 15L, 16L, 17L, 19L, 20L, 21L, 22L),
class = "data.frame")
#Count how often every weather_day occurs during every hOfDay
plot_data <- d %>%
group_by(hOfDay, weather_day) %>%
summarize(n_occurences = n())
#Create plot
ggplot(plot_data, aes(x = hOfDay, y = n_occurences)) +
geom_line(aes(col = weather_day))

2d contour color map in ggplot2

I have a huge data, please bear with me.
df <- structure(list(W = c(5216400.4123, 5399804.7349, 5595563.3087,
5792353.9932, 5993467.7466, 6189404.9279, 6380940.454, 6566630.3544,
6747453.6816, 6917820.9796, 7086201.8275, 7248213.5225, 5402700.4252,
5592654.9057, 5795404.8549, 5999223.7818, 6207520.1695, 6410455.1037,
6608831.1825, 6801152.8706, 6988434.1695, 7164886.0132, 7339280.46,
7507078.2886, 5589000.4397, 5785505.0748, 5995246.3993, 6206093.5662,
6421572.589, 6631505.2817, 6836721.9131, 7035675.3828, 7229414.6578,
7411951.0459, 7592359.0997, 7765943.0584, 5775300.4552, 5978355.2455,
6195087.9484, 6412963.3504, 6635625.0103, 6852555.4544, 7064612.6447,
7270197.897, 7470395.1463, 7659016.0859, 7845437.7356, 8024807.8293,
5961600.4694, 6171205.4145, 6394929.4952, 6619833.1358, 6849677.4283,
7073605.633, 7292503.372, 7504720.4062, 7711375.6354, 7906081.1161,
8098516.3724, 8283672.5987, 6147900.4816, 6364055.583, 6594771.0412,
6826702.9212, 7063729.8478, 7294655.8114, 7520394.1023, 7739242.9195,
7952356.1253, 8153146.1541, 8351595.0092, 8542537.3639, 6334200.5005,
6556905.7553, 6794612.5881, 7033572.7089, 7277782.2661, 7515705.9882,
7748284.8337, 7973765.4322, 8193336.6123, 8400211.1873, 8604673.6477,
8801402.1321, 6520500.5136, 6749755.9226, 6994454.1327, 7240442.494,
7491834.6868, 7736756.1617, 7976175.5621, 8208287.9459, 8434317.1037,
8647276.2228, 8857752.2827, 9060266.9029, 6706800.528, 6942606.0899,
7194295.6809, 7447312.2777, 7705887.1079, 7957806.3409, 8204066.2961,
8442810.4584, 8675297.5886, 8894341.2573, 9110830.9206, 9319131.6718,
6893100.5418, 7135456.2602, 7394137.2288, 7654182.0653, 7919939.5232,
8178856.5136, 8431957.0234, 8677332.969, 8916278.0804, 9141406.2918,
9363909.5543, 9577996.4404, 7079400.5588, 7328306.4296, 7593978.7729,
7861051.8503, 8133991.9462, 8399906.6912, 8659847.7579, 8911855.4821,
9157258.5684, 9388471.3277, 9616988.1903, 9836861.209, 7265700.5699,
7521156.5994, 7793820.3185, 8067921.639, 8348044.3652, 8620956.868,
8887738.4844, 9146377.9962, 9398239.0552, 9635536.362, 9870066.8286,
10095725.9764, 7377480.5806, 7636866.7018, 7913725.2471, 8192043.5082,
8476475.8166, 8753586.9712, 9024472.9255, 9287091.5011, 9542827.3494,
9783775.3823, 10021914.0099, 10251044.8357, 7452000.5846, 7714006.7682,
7993661.8669, 8274791.4235, 8562096.7846, 8842007.041, 9115629.2162,
9380900.5049, 9639219.5459, 9882601.398, 10123145.4642, 10354590.7474,
7526520.5952, 7791146.8356, 8073598.4841, 8357539.3348, 8647717.7534,
8930427.113, 9206785.5068, 9474709.5143, 9735611.7438, 9981427.4094,
10224376.9194, 10458136.6527, 7638300.6025, 7906856.9365, 8193503.4143,
8481661.2074, 8776149.2063, 9063057.2172, 9343519.9463, 9615423.0209,
9880200.0383, 10129666.4312, 10376224.1032, 10613455.5131, 7824600.6172,
8099707.1074, 8393344.9601, 8688530.993, 8990201.627, 9284107.395,
9571410.6772, 9849945.5329, 10121180.5245, 10376731.4677, 10629302.7393,
10872320.2844, 8010900.6306, 8292557.2766, 8593186.5077, 8895400.7788,
9204254.0428, 9505157.5709, 9799301.4089, 10084468.0455, 10362161.0109,
10623796.504, 10882381.3729, 11131185.0534, 8197200.6444, 8485407.4453,
8793028.0567, 9102270.5614, 9418306.465, 9726207.7477, 10027192.1371,
10318990.5604, 10603141.4991, 10870861.5368, 11135460.012, 11390049.8203,
8383500.6593, 8678257.6128, 8992869.6005, 9309140.3471, 9632358.8856,
9947257.9225, 10255082.8657, 10553513.0731, 10844121.988, 11117926.5692,
11388538.6484, 11648914.5899, 8569800.6735, 8871107.7799, 9192711.1487,
9516010.1349, 9846411.3046, 10168308.0987, 10482973.5998, 10788035.5824,
11085102.4738, 11364991.6051, 11641617.2821, 11907779.3576, 8756100.6891,
9063957.9534, 9392552.6936, 9722879.9204, 10060463.7195, 10389358.2746,
10710864.3279, 11022558.0956, 11326082.9668, 11612056.6477, 11894695.9209,
12166644.1258, 8942400.705, 9256808.1204, 9592394.2396, 9929749.7037,
10274516.1413, 10610408.4494, 10938755.0613, 11257080.6099, 11567063.455,
11859121.6771, 12147774.5594, 12425508.8956, 9128700.7203, 9449658.2901,
9792235.786, 10136619.4908, 10488568.5635, 10831458.6249, 11166645.792,
11491603.1215, 11808043.9427, 12106186.7106, 12400853.1962, 12684373.664,
9315000.7353, 9642508.4595, 9992077.3349, 10343489.2758, 10702620.9802,
11052508.7998, 11394536.5185, 11726125.6341, 12049024.431, 12353251.7462,
12653931.8307, 12943238.4324, 9501300.7465, 9835358.6307, 10191918.8807,
10550359.0623, 10916673.4022, 11273558.9758, 11622427.2509, 11960648.1479,
12290004.92, 12600316.7835, 12907010.4688, 13202103.2002, 9687600.7612,
10028208.7979, 10391760.4269, 10757228.8467, 11130725.8198, 11494609.1532,
11850317.9787, 12195170.6632, 12530985.4111, 12847381.8161, 13160089.1051,
13460967.9711), t = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L), .Label = c("t_m75_tc", "t_m5_tc", "t_m25_tc", "t_p0_tc",
"t_p25_tc", "t_p5_tc", "t_p75_tc", "t_p10_tc", "t_p125_tc", "t_p15_tc",
"t_p175_tc", "t_p20_tc"), class = "factor"), p = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 25L, 25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 27L), .Label = c("h_30", "h_27.5", "h_25", "h_22.5",
"h_20", "h_17.5", "h_15", "h_12.5", "h_10", "h_7.5", "h_5", "h_2.5",
"h_1", "h0", "h1", "h2.5", "h5", "h7.5", "h10", "h12.5", "h15",
"h17.5", "h20", "h22.5", "h25", "h27.5", "h30"), class = "factor"),
tt = c(-7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5,
20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20,
-7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5,
-5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5,
-2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5,
0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0,
2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5,
5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5,
7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5,
10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10,
12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5,
15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15,
17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5,
20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20,
-7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5,
-5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5,
-2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5,
0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0,
2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5,
5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5,
7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5,
10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10,
12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5,
15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15,
17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5,
20), hh = c(-30, -30, -30, -30, -30, -30, -30, -30, -30,
-30, -30, -30, -27.5, -27.5, -27.5, -27.5, -27.5, -27.5,
-27.5, -27.5, -27.5, -27.5, -27.5, -27.5, -25, -25, -25,
-25, -25, -25, -25, -25, -25, -25, -25, -25, -22.5, -22.5,
-22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5,
-22.5, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20,
-20, -20, -17.5, -17.5, -17.5, -17.5, -17.5, -17.5, -17.5,
-17.5, -17.5, -17.5, -17.5, -17.5, -15, -15, -15, -15, -15,
-15, -15, -15, -15, -15, -15, -15, -12.5, -12.5, -12.5, -12.5,
-12.5, -12.5, -12.5, -12.5, -12.5, -12.5, -12.5, -12.5, -10,
-10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -7.5,
-7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5,
-7.5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -2.5,
-2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5,
-2.5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5,
2.5, 2.5, 2.5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7.5, 7.5,
7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 12.5, 12.5, 12.5,
12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 17.5, 17.5, 17.5,
17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 22.5, 22.5, 22.5,
22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 27.5, 27.5, 27.5,
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), pChange = c(-36.9603395985828,
-34.7439172960321, -32.3781951432773, -30.0000000392759,
-27.5695610939649, -25.2016804880133, -22.8869934306931,
-20.6429501564101, -18.4577189167867, -16.3988477104845,
-14.3639825485445, -12.4060879418008, -34.7089231777299,
-32.4133428932464, -29.9631306906258, -27.5000000028702,
-24.982759663634, -22.5303119363876, -20.1329575059591, -17.8087697620382,
-15.5454946011909, -13.4130922883195, -11.3055534045631,
-9.27773397066822, -32.4575067375413, -30.082768511005, -27.5480662597272,
-25.000000017221, -22.3959582743917, -19.8589433581751, -17.3789215558468,
-14.974589416006, -12.6332702807612, -10.4273368770308, -8.24712417357041,
-6.14937995482153, -30.2060902852677, -27.7521941094277,
-25.1330017720295, -22.5000000339888, -19.8091568633966,
-17.1875748440126, -14.6248855936495, -12.1404090458039,
-9.72104595791446, -7.44158137752243, -5.18869498850033,
-3.02102592568144, -27.9546738487045, -25.4216197283948,
-22.7179373121271, -20.0000000362547, -17.2223554922816,
-14.5162062585492, -11.8708496834174, -9.30622873602633,
-6.80882162781682, -4.45582599644603, -2.13026579255384,
0.107328085331291, -25.7032574363111, -23.0910453534044,
-20.3028728618926, -17.5000000385206, -14.6355541030393,
-11.8448376755028, -9.11681373693055, -6.47204837670069,
-3.89659728805126, -1.47007052110743, 0.928163403392649,
3.23568204558746, -23.4518409429489, -20.7604709324913, -17.8878084007817,
-15.0000000129913, -12.0487527282989, -9.17346911179217,
-6.36277777715033, -3.63786802462598, -0.984372983331903,
1.51568489622367, 3.98659261988347, 6.36403604209831, -21.2004245196791,
-18.4298965720027, -15.4727439674661, -12.5000000188826,
-9.46195132455473, -6.50210058796172, -3.6087418536248, -0.803687660466381,
1.92785137456101, 4.50144034135001, 7.04502179407714, 9.4923900700299,
-18.949008080699, -16.0993222115141, -13.0576794906449, -10.0000000416929,
-6.8751499159766, -3.83073199524737, -0.85470586242384, 2.03049268919135,
4.84007565390208, 7.48719577439149, 10.103451003317, 12.6207440750002,
-16.6975916489697, -13.7687478147708, -10.6426150174491,
-7.50000001737204, -4.28834857749088, -1.15936348108484,
1.8993300478083, 4.86467301588778, 7.75230001662893, 10.4729512074329,
13.1618801618003, 15.749098076345, -14.4461751785688, -11.4381734289039,
-8.2275505901759, -5.00000002447191, -1.70154714595145, 1.51200509229367,
4.65336604505171, 7.69885337279645, 10.6645243334332, 13.4587066573933,
16.2203093480789, 18.8774520776898, -12.1947587794688, -9.10759903820312,
-5.81248614477539, -2.49999998685766, 0.885254237248392,
4.18337365600429, 7.40740194561595, 10.53303374179, 13.5767486357356,
16.4444620880177, 19.2787385621527, 22.0058060645328, -10.8439088911858,
-7.70925439749847, -4.3634474625498, -1.00000001287041, 2.43733506716831,
5.78619475942614, 9.05982354879595, 12.2335419201639, 15.324083242737,
18.2359153430087, 21.1137960702944, 23.8828184428618, -9.94334233686319,
-6.77702465958718, -3.39742166553716, 0, 3.47205562528221,
6.85474217379226, 10.1614379102301, 13.3672140455251, 16.4889729851691,
19.4302175391865, 22.3371677435973, 25.1341600948813, -9.04277570278022,
-5.84479490959099, -2.43139589994524, 0.999999964530837,
4.50677619306401, 7.92328961474518, 11.2630522704558, 14.5008862385618,
17.65386274452, 20.6245196833994, 23.5605394277767, 26.3855016695576,
-7.69192585558588, -4.44645028701369, -0.982357198383821,
2.49999997960674, 6.05885704111126, 9.52611073025193, 12.9154738543,
16.20139443748, 19.4011973551469, 22.4159729565176, 25.3955969661306,
28.26251406118, -5.44050941298024, -2.11587588301946, 1.43270724943368,
4.99999997975781, 8.64565844485543, 12.1974793060474, 15.6695098080378,
19.0355747810953, 22.3134216501983, 25.4017284137289, 28.4540261536177,
31.390868095154, -3.18909298608498, 0.214698500430437, 3.84777171900399,
7.49999998232584, 11.2324597893836, 14.8688478588816, 18.4235457714435,
21.8697551319615, 25.2256459476667, 28.3874838685232, 31.5124553108925,
34.5192221013328, -0.93767655435575, 2.54527287783788, 6.26283620549316,
9.99999994622222, 13.8192612112551, 17.5402164225922, 21.177581692552,
24.703935510623, 28.1378702668879, 31.3732392810203, 34.5708845346342,
37.6475760821333, 1.31373989066688, 4.87584724074345, 8.67790062914087,
12.4999999475818, 16.4060626137908, 20.211584962133, 23.9316176184945,
27.5381158626977, 31.0500945945686, 34.3589946886835, 37.6293137257467,
40.775930095563, 3.56515632723005, 7.20642159881506, 11.0929651059621,
14.9999999743196, 18.9928639969906, 22.8829535185927, 26.685653610904,
30.3722961736837, 33.9623188847861, 37.3447501386438, 40.6877428842301,
43.9042840860314, 5.81657278071209, 9.53699603423, 13.5080295429032,
17.4999999732622, 21.5796653306424, 25.5543220714269, 29.439689530804,
33.2064765318009, 36.8745432620148, 40.3305056695729, 43.7461721043463,
47.0326380825422, 8.06798923781962, 11.8675703910931, 15.9230939931377,
19.999999945618, 24.16646674768, 28.2256906109677, 32.1937255147541,
36.0406569032114, 39.7867675812361, 43.3162610409814, 46.8046013208372,
50.1609920983889, 10.3194056876762, 14.1981447805855, 18.3381584482061,
22.4999999638964, 26.7532681695515, 30.8970591589679, 34.9477614660749,
38.8748372419928, 42.6989918944149, 46.302016461938, 49.8630305167836,
53.2893460973168, 12.5708221339073, 16.5287191664524, 20.7532229334868,
24.9999999567965, 29.3400695249561, 33.5684276997172, 37.7017973666391,
41.709017592859, 45.6112162148446, 49.2877719082728, 52.9214596849349,
56.4177000962446, 14.8222385342158, 18.859293574072, 23.1682873813043,
27.499999967824, 31.9268709444106, 36.2397962537599, 40.4558333385042,
44.5431979582271, 48.5234405437337, 52.273527375152, 55.9798888965917,
59.5460540879215, 17.0736549768214, 21.1898679333522, 25.5833518339557,
29.9999999534732, 34.5136723106916, 38.9111648247215, 43.2098692547788,
47.3773783417225, 51.4356648980012, 55.2592827852321, 59.0383180864958,
62.6744081170616)), class = "data.frame", .Names = c("W",
"t", "p", "tt", "hh", "pChange"), row.names = c(NA, -324L))
I am trying to plot a colored contour map with the columns tt, hh and W, W being the z axis.
This is the code I am using::
ggplot(df, aes(x = tt, y = hh, z = W)) +
stat_contour(geom = "polygon", aes(fill = ..level..) ) +
geom_tile(aes(fill = W)) +
stat_contour(bins = 10) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W"))
This is the result I got:
What I want is the color to be continuous, as seen here I am following this example for my work.
What is going wrong over here?
The grid is not evenly spaced. One way to make an evenly spaced grid is to use interpolate using loess on an evenly spaced grid:
model <- loess(W ~ tt + hh, data = df)
create an evenly spaced grid using expand.grid:
new.data <- expand.grid(tt = seq(from = min(df$tt), to = max(df$tt), length.out = 500),
hh = seq(from = min(df$hh), to = max(df$hh), length.out = 500))
predict on new data using the model:
gg <- predict(model, newdata = new.data)
combine prediction and new data:
new.data = data.frame(W = as.vector(gg),
new.data)
and now the plot looks like:
ggplot(new.data, aes(x = tt, y = hh, z = W)) +
stat_contour(geom = "polygon", aes(fill = ..level..) ) +
geom_tile(aes(fill = W)) +
stat_contour(bins = 10) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W"))
You might also want to check some goodness of fit metric for loess
caret::RMSE(model$fitted, df$W)
#output
7498.393
using a narrower span could provide a better fit, especially if the data is not smooth:
model2 <- loess(W ~ tt + hh, data = df, span = 0.1)
caret::RMSE(model2$fitted, df$W)
#output
964.7582
ggplot(new.data2, aes(x = tt, y = hh, z = W)) +
stat_contour(geom = "polygon", aes(fill = ..level..) ) +
geom_tile(aes(fill = W)) +
stat_contour(bins = 10) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W"))
The difference is ever so slight
ggplot(new.data, aes(x = tt, y = hh, z = W)) +
geom_tile(aes(fill = W)) +
geom_contour(aes(x = tt, y = hh, z = W),
color = "red")+
geom_contour(data = new.data2,
aes(x = tt, y = hh, z = W),
color = "white", inherit.aes = FALSE)
EDIT: also check the great post by #Henrik which is linked by him in the comment. Especially the ?akima::interp function.
EDIT2: answer to the questions in comments:
To specify a different fill one can use
scale_fill_gradient
scale_fill_gradient2
scale_fill_gradientn
Here is an example of using scale_fill_gradientn with 5 colors based on quantiles:
v <- ggplot(new.data2, aes(x = tt, y = hh, z = floor(W))) +
geom_tile(aes(fill = W), show.legend = FALSE) +
stat_contour(bins = 10, aes(colour = ..level..)) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W")) +
scale_fill_gradientn(values = scales::rescale(quantile(new.data2$W)),
colors = rainbow(5))
I removed the polygon thing since it was below the geom_tile layer and was not visible.
To add direct labels:
library(directlabels)
direct.label(v, list("far.from.others.borders", "calc.boxes", "enlarge.box",
box.color = NA, fill = "transparent", "draw.rects"))

Error while storing ggplots in list. Impossible to plot multiple ggplot, but possible to plot them separately

I'm currently working on the effects of environmental variables on the toxicity of a shellfish. This toxicity happens only on certain years. I would like to compare time series of 15 different environmental variables between toxic years and non toxic years. My data or on 10 years and 6 locations.
I would like to have 1 window / site, each window containing 10 ggplots representing the 10 annual time series of one parameter
here are the data i give for a reproducible example, on one location, for one parameter (Temperature): (corrected to be reproducible)
structure(list(Date = structure(c(12065, 12065, 12079, 12079,
12088, 12095, 12095, 12104, 12115, 12115, 12123, 12123, 12130,
12130, 12135, 12137, 12137, 12142, 12146, 12146, 12149, 12150,
12150, 12156, 12157, 12157, 12164, 12164, 12165, 12170, 12177,
12177, 12177, 12184, 12185, 12185, 12191, 12192, 12192, 12198,
12199, 12199, 12205, 12206, 12206, 12213, 12215, 12215, 12219,
12219, 12219, 12226, 12233, 12235, 12235, 12240, 12240, 12240,
12240, 12240, 12240, 12248, 12248, 12248, 12254, 12255, 12255,
12261, 12263, 12263, 12268, 12268, 12268, 12275, 12275, 12275,
12282, 12283, 12283, 12289, 12291, 12291, 12296, 12297, 12297,
12303, 12305, 12305, 12311, 12311, 12318, 12318, 12326, 12331,
12338, 12352, 12368, 12381, 12395, 12403, 12424, 12436, 12452,
12464, 12478, 12495, 12507, 12522, 12528, 12534, 12541, 12548,
12562, 12571, 12571, 12576, 12576, 12583, 12583, 12591, 12598,
12613, 12620, 12625, 12633, 12639, 12646, 12653, 12661, 12667,
12676, 12682, 12690, 12696, 12702, 12709, 12716, 12724, 12730,
12744, 12758, 12772, 12795, 12800, 12814, 12828, 12843, 12856,
12871, 12877, 12884, 12898, 12905, 12912, 12926, 12933, 12940,
12954, 12954, 12961, 12961, 12968, 12968, 12982, 12982, 13011,
13011, 13024, 13024, 13038, 13052, 13052, 13067, 13083, 13094,
13111, 13122, 13136, 13151, 13166, 13178, 13192, 13206, 13221,
13236, 13248, 13262, 13270, 13278, 13292, 13298, 13305, 13318,
13318, 13326, 13332, 13332, 13333, 13339, 13346, 13346, 13377,
13390, 13402, 13432, 13466, 13529, 13542, 13585, 13599, 13614,
13626, 13643, 13655, 13669, 13675, 13683, 13698, 13710, 13725,
13731, 13741, 13754, 13760, 13767, 13781, 13789, 13795, 13809,
13823, 13838, 13851, 13867, 13901, 13901, 13907, 13921, 13936,
13936, 13957, 13963, 13963, 13978, 13992, 13992, 14005, 14020,
14020, 14036, 14036, 14041, 14047, 14047, 14047, 14047, 14047,
14053, 14054, 14061, 14061, 14069, 14076, 14076, 14076, 14076,
14077, 14082, 14089, 14089, 14105, 14105, 14105, 14105, 14118,
14118, 14131, 14131, 14145, 14145, 14152, 14160, 14166, 14173,
14180, 14188, 14202, 14216, 14230, 14258, 14271, 14287, 14299,
14312, 14327, 14340, 14354, 14368, 14375, 14382, 14397, 14411,
14411, 14425, 14425, 14440, 14440, 14447, 14453, 14453, 14467,
14467, 14474, 14481, 14481, 14488, 14494, 14502, 14509, 14509,
14516, 14523, 14539, 14565, 14579, 14593, 14607, 14635, 14649,
14663, 14683, 14700, 14706, 14714, 14719, 14727, 14736, 14749,
14763, 14763, 14777, 14777, 14791, 14819, 14819, 14824, 14832,
14832, 14845, 14845, 14861, 14861, 14873, 14873, 14888, 14902,
14929, 14985, 14999, 15015, 15029, 15043, 15057, 15071, 15085,
15097, 15111, 15125, 15141, 15141, 15153, 15153, 15167, 15167,
15181, 15181, 15195, 15195, 15209, 15209, 15223, 15237, 15237,
15251, 15265, 15281, 15293, 15307, 15321, 15335, 15349, 15377,
15391, 15405, 15419, 15433, 15447, 15457, 15463, 15474, 15491,
15503, 15503, 15517, 15517, 15523, 15533, 15545, 15545, 15559,
15559, 15573, 15573, 15589, 15589, 15601, 15601, 15615, 15629,
15643, 15657, 15671, 15685, 15702), class = "Date"), Annee = structure(c(9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L), .Label = c("1995", "1996", "1997", "1998", "1999", "2000",
"2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008",
"2009", "2010", "2011", "2012", "2013"), class = "factor"), Mois = structure(c(1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
11L, 11L, 12L, 12L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 12L,
12L, 1L, 1L, 1L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 5L,
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L,
10L, 10L, 11L, 11L, 12L, 12L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 4L,
4L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 8L,
8L, 9L, 10L, 11L, 1L, 1L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 6L,
7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L,
12L, 12L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L,
9L, 9L, 10L, 10L, 10L, 10L, 11L, 11L, 12L, 12L, 1L, 1L, 2L, 2L,
3L, 3L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L,
7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 11L,
12L, 12L, 12L, 1L, 2L, 2L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 6L,
6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L,
10L, 11L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 5L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L,
11L, 11L, 11L, 12L, 12L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 5L,
5L, 5L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L,
9L, 9L, 10L, 10L, 10L, 11L, 11L, 12L, 12L), .Label = c("01",
"02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"
), class = "factor"), Jourannee = structure(c(12L, 12L, 26L,
26L, 35L, 42L, 42L, 51L, 62L, 62L, 70L, 70L, 77L, 77L, 82L, 84L,
84L, 89L, 93L, 93L, 96L, 97L, 97L, 103L, 104L, 104L, 111L, 111L,
112L, 117L, 124L, 124L, 124L, 131L, 132L, 132L, 138L, 139L, 139L,
145L, 146L, 146L, 152L, 153L, 153L, 160L, 162L, 162L, 166L, 166L,
166L, 173L, 180L, 182L, 182L, 187L, 187L, 187L, 187L, 187L, 187L,
195L, 195L, 195L, 201L, 202L, 202L, 208L, 210L, 210L, 215L, 215L,
215L, 222L, 222L, 222L, 229L, 230L, 230L, 236L, 238L, 238L, 243L,
244L, 244L, 250L, 252L, 252L, 258L, 258L, 265L, 265L, 273L, 278L,
285L, 299L, 314L, 327L, 341L, 349L, 6L, 18L, 34L, 46L, 60L, 77L,
89L, 104L, 110L, 116L, 123L, 130L, 144L, 153L, 153L, 158L, 158L,
165L, 165L, 173L, 180L, 195L, 202L, 207L, 215L, 221L, 228L, 235L,
243L, 249L, 258L, 264L, 272L, 278L, 284L, 291L, 298L, 306L, 312L,
325L, 339L, 353L, 11L, 16L, 30L, 44L, 59L, 72L, 87L, 93L, 100L,
114L, 121L, 128L, 142L, 149L, 156L, 170L, 170L, 177L, 177L, 184L,
184L, 198L, 198L, 227L, 227L, 240L, 240L, 254L, 268L, 268L, 283L,
299L, 310L, 326L, 337L, 351L, 2L, 17L, 29L, 43L, 57L, 72L, 87L,
99L, 113L, 121L, 129L, 143L, 149L, 156L, 169L, 169L, 177L, 183L,
183L, 184L, 190L, 197L, 197L, 228L, 241L, 253L, 283L, 316L, 15L,
28L, 71L, 85L, 100L, 112L, 129L, 141L, 155L, 161L, 169L, 184L,
196L, 211L, 217L, 227L, 240L, 246L, 253L, 267L, 275L, 281L, 295L,
309L, 323L, 336L, 352L, 22L, 22L, 28L, 42L, 57L, 57L, 78L, 84L,
84L, 99L, 113L, 113L, 126L, 141L, 141L, 157L, 157L, 162L, 168L,
168L, 168L, 168L, 168L, 174L, 175L, 182L, 182L, 190L, 197L, 197L,
197L, 197L, 198L, 203L, 210L, 210L, 226L, 226L, 226L, 226L, 239L,
239L, 252L, 252L, 266L, 266L, 273L, 281L, 287L, 294L, 301L, 309L,
322L, 336L, 350L, 13L, 26L, 42L, 54L, 67L, 82L, 95L, 109L, 123L,
130L, 137L, 152L, 166L, 166L, 180L, 180L, 195L, 195L, 202L, 208L,
208L, 222L, 222L, 229L, 236L, 236L, 243L, 249L, 257L, 264L, 264L,
271L, 278L, 294L, 319L, 333L, 347L, 360L, 25L, 39L, 53L, 73L,
90L, 96L, 104L, 109L, 117L, 126L, 139L, 153L, 153L, 167L, 167L,
181L, 209L, 209L, 214L, 222L, 222L, 235L, 235L, 251L, 251L, 263L,
263L, 278L, 292L, 318L, 10L, 24L, 40L, 54L, 68L, 82L, 96L, 110L,
122L, 136L, 150L, 166L, 166L, 178L, 178L, 192L, 192L, 206L, 206L,
220L, 220L, 234L, 234L, 248L, 262L, 262L, 276L, 290L, 306L, 317L,
331L, 345L, 358L, 9L, 37L, 51L, 65L, 79L, 93L, 107L, 117L, 123L,
134L, 151L, 163L, 163L, 177L, 177L, 183L, 193L, 205L, 205L, 219L,
219L, 233L, 233L, 249L, 249L, 261L, 261L, 275L, 289L, 303L, 316L,
330L, 344L, 360L), .Label = c("002", "003", "004", "005", "006",
"007", "008", "009", "010", "011", "012", "013", "014", "015",
"016", "017", "018", "019", "020", "021", "022", "023", "024",
"025", "026", "027", "028", "029", "030", "031", "032", "033",
"034", "035", "036", "037", "038", "039", "040", "041", "042",
"043", "044", "045", "046", "047", "048", "049", "050", "051",
"052", "053", "054", "055", "056", "057", "058", "059", "060",
"061", "062", "063", "064", "065", "066", "067", "068", "069",
"070", "071", "072", "073", "074", "075", "076", "077", "078",
"079", "080", "081", "082", "083", "084", "085", "086", "087",
"088", "089", "090", "091", "092", "093", "094", "095", "096",
"097", "098", "099", "100", "101", "102", "103", "104", "105",
"106", "107", "108", "109", "110", "111", "112", "113", "114",
"115", "116", "117", "118", "119", "120", "121", "122", "123",
"124", "125", "126", "127", "128", "129", "130", "131", "132",
"133", "134", "135", "136", "137", "138", "139", "140", "141",
"142", "143", "144", "145", "146", "147", "148", "149", "150",
"151", "152", "153", "154", "155", "156", "157", "158", "159",
"160", "161", "162", "163", "164", "165", "166", "167", "168",
"169", "170", "171", "172", "173", "174", "175", "176", "177",
"178", "179", "180", "181", "182", "183", "184", "185", "186",
"187", "188", "189", "190", "191", "192", "193", "194", "195",
"196", "197", "198", "199", "200", "201", "202", "203", "204",
"205", "206", "207", "208", "209", "210", "211", "212", "213",
"214", "215", "216", "217", "218", "219", "220", "221", "222",
"223", "224", "225", "226", "227", "228", "229", "230", "231",
"232", "233", "234", "235", "236", "237", "238", "239", "240",
"241", "242", "243", "244", "245", "246", "247", "248", "249",
"250", "251", "252", "253", "254", "255", "256", "257", "258",
"259", "260", "261", "262", "263", "264", "265", "266", "267",
"268", "269", "270", "271", "272", "273", "274", "275", "276",
"277", "278", "279", "280", "281", "282", "283", "284", "285",
"286", "287", "288", "289", "290", "291", "292", "293", "294",
"295", "296", "297", "298", "299", "300", "301", "302", "303",
"304", "305", "306", "307", "308", "309", "310", "311", "312",
"313", "314", "316", "317", "318", "319", "320", "321", "322",
"323", "324", "325", "326", "327", "328", "329", "330", "331",
"332", "333", "334", "335", "336", "337", "338", "339", "340",
"341", "342", "343", "344", "345", "346", "347", "348", "349",
"350", "351", "352", "353", "354", "355", "356", "357", "358",
"360", "361", "362", "363", "364", "365"), class = "factor"),
Mesure = c(8, 8, 9.5, 10, 9.5, 10.7, 10.7, 8.5, 9.8, 9.8,
10.3, 10.5, 10.4, 10.5, 11.7, 10.6, 10.6, 13.6, 11.1, 11.1,
11.4, 11, 11, 13, 11.3, 11.3, 12.8, 13.8, 14.4, 14.5, 13.5,
13.9, 15.1, 13.8, 12.5, 12.6, 13.4, 12.6, 12.6, 15, 14.1,
14.3, 17.1, 14.7, 14.9, 18.6, 19, 20, 18.8, 19.2, 19.3, 18.9,
17.7, 15.9, 16.2, 14.2, 14.7, 14.9, 15.3, 15.3, 16, 18.4,
18.4, 20, 20.4, 17.8, 17.8, 19.2, 17.5, 17.7, 17.6, 17.7,
21.3, 22.2, 22.2, 22.6, 20.9, 19.2, 20.2, 21.1, 19.7, 19.7,
18, 17.6, 18.9, 18.7, 16.9, 17.8, 17.2, 18.1, 17.6, 18.9,
17, 16.9, 15, 14.1, 13, 12.6, 11.7, 11, 10.7, 10.3, 10.4,
9.5, 8.2, 8.9, 10.1, 10.8, 10.9, 12.8, 13.1, 12.1, 14.8,
14.2, 17, 17.6, 17.8, 14.1, 17.7, 14.7, 14.7, 14.2, 15.3,
17.8, 18, 19.8, 18.3, 19.4, 16.9, 19, 17.6, 17.4, 16.4, 16.4,
15.8, 15.1, 14.8, 14.1, 14.2, 12.8, 12, 10.3, 10.7, 10.2,
9.7, 9.4, 7.7, 8, 11, 11.4, 10.7, 12, 13.1, 12.7, 14.3, 15.6,
14.7, 15, 18.5, 17.2, 19.3, 12.8, 15, 15, 17.7, 14.9, 17.3,
15.6, 16.6, 18.5, 16.4, 17.3, 16.4, 16.2, 15.1, 12.7, 10,
8.3, 7.3, 7, 8, 7.4, 7.4, 8.4, 9.2, 9.4, 12.7, 11.5, 14.2,
12.7, 12.5, 15.7, 17.8, 18.9, 17.4, 16.6, 18.7, 20.7, 20,
18, 18.9, 15.7, 16.1, 18.1, 17.6, 14.7, 12.1, 11, 11.8, 11,
12.4, 14.5, 12.7, 12.6, 14.4, 17.9, 16.6, 14.5, 16.2, 17.1,
18.7, 17.9, 17.4, 17.2, 18, 16.4, 14.4, 15.5, 14.2, 13.8,
12.1, 11.3, 8.9, 9.8, 9.8, 8.9, 8.4, 8.9, 8.9, 10.6, 10.2,
10.2, 10.8, 11.7, 11.7, 14, 16.2, 16.2, 14, 15, 15.6, 12.9,
12.9, 15, 15.7, 15.7, 16.6, 17.4, 12.9, 16.9, 15.5, 13.9,
13.9, 16.1, 16.1, 14.6, 14.1, 18, 18.6, 12.4, 12.4, 15.4,
15.4, 15.8, 17.2, 16.5, 16.5, 16.7, 16.8, 15.9, 14.3, 15.4,
15, 13.3, 13.2, 12.7, 11.4, 9.4, 6.9, 8.2, 8.4, 8.2, 9.5,
11.1, 11, 12.8, 12, 12.3, 13, 16.6, 13.5, 16.7, 14.2, 19.3,
13.7, 16.1, 14.2, 14.1, 17.2, 15, 17.3, 19.5, 16.2, 18.1,
17.4, 15.4, 16.9, 14.7, 16.6, 17.2, 16.6, 15.4, 11.8, 11.8,
10.2, 10, 7.1, 8.3, 8.2, 8, 9.8, 10.2, 12.1, 11.7, 13.4,
11.2, 13.1, 10.6, 13.2, 12.9, 14.6, 18, 12.7, 15.1, 16.3,
11.9, 15.7, 14.6, 17, 15.2, 17.5, 15, 16.3, 15.5, 15.7, 13,
7.7, 7.9, 8.4, 9.2, 8.7, 10, 12.1, 13.6, 15.3, 14.89, 13.05,
13.8, 14.89, 14.9, 16.41, 16.1, 16.39, 11.7, 14.8, 15.56,
16.72, 17, 18.07, 17.4, 15, 16.79, 18.27, 16.39, 15.6, 14.75,
13.87, 12.2, 11, 11.8, 9.71, 9.52, 10.47, 11.44, 12.05, 11.49,
11.6, 12.83, 14.05, 17.14, 12.6, 14.8, 12.6, 15.16, 16.1,
15.32, 16.8, 18.01, 15.5, 16.65, 18.8, 20.36, 16.8, 17.52,
15.6, 17.35, 15.8, 15.62, 14.86, 13.2, 12.11, 11.65, 12)), .Names = c("Date",
"Annee", "Mois", "Jourannee", "Mesure"), class = "data.frame", row.names = c("7413",
"7440", "16263", "19364", "16266", "22684", "22705", "9711",
"18115", "18133", "20630", "21431", "21054", "21437", "26379",
"22192", "22243", "34022", "24087", "24124", "25291", "23623",
"23663", "31760", "24950", "24959", "31098", "34997", "37850",
"38311", "33673", "35459", "40853", "34839", "29922", "30310",
"33231", "30314", "30326", "40496", "36427", "37419", "53855",
"39326", "40145", "64409", "69950", "81748", "66481", "72995",
"74404", "68002", "58822", "45098", "47124", "36883", "39239",
"40140", "41558", "41600", "45858", "63000", "63005", "81502",
"84446", "59280", "59288", "72676", "57414", "58961", "58115",
"58991", "89667", "91764", "91768", "92261", "87505", "72951",
"83212", "88778", "78851", "78893", "60137", "58123", "68201",
"65525", "52759", "59289", "55419", "61881", "58154", "68003",
"53356", "52695", "40657", "36449", "31885", "30332", "26459",
"23669", "22574", "20511", "20903", "16118", "8086", "12079",
"19751", "22853", "23163", "30939", "32157", "27887", "39661",
"36753", "53067", "57893", "59172", "36321", "58700", "39167",
"39170", "36734", "41402", "59170", "59903", "79538", "62765",
"75136", "52653", "69435", "57897", "56565", "48945", "48951",
"44503", "40840", "39670", "36315", "36742", "30945", "27506",
"20514", "22577", "20126", "17341", "15719", "6445", "7337",
"23464", "25247", "22580", "27509", "32163", "30559", "37312",
"43405", "39176", "40414", "63157", "54854", "74032", "30952",
"40404", "40417", "58699", "40005", "56083", "43409", "51235",
"63154", "49001", "56088", "48939", "46903", "40834", "30548",
"19184", "8756", "4488", "3263", "7334", "5070", "5079", "9252",
"14404", "15713", "30545", "25632", "36722", "30554", "29683",
"44042", "59178", "67753", "56643", "51255", "65461", "86321",
"81509", "59912", "67781", "44028", "46318", "61761", "57905",
"39173", "27890", "23455", "26624", "23461", "29204", "38270",
"30556", "30171", "37778", "59417", "51253", "38275", "46909",
"53720", "65458", "59418", "56588", "55061", "59906", "48962",
"37783", "42312", "36729", "34791", "27881", "24836", "12045",
"17979", "17984", "12054", "9250", "12064", "12072", "22002",
"20109", "20110", "22851", "26337", "26343", "35822", "46898",
"46901", "35832", "40398", "43545", "31363", "31366", "40409",
"44036", "44039", "51229", "56644", "31360", "52652", "42381",
"35285", "35288", "46301", "46304", "38784", "36367", "59915",
"64162", "29209", "29214", "41856", "41859", "44511", "54826",
"50116", "50123", "51750", "52291", "45044", "37307", "41911",
"40401", "32853", "32456", "30551", "25244", "15716", "3183",
"8084", "9255", "8088", "16121", "24000", "23451", "30942", "27499",
"28718", "31659", "51239", "33546", "51749", "36763", "74022",
"34331", "46314", "36739", "36327", "54836", "40426", "56091",
"76239", "46918", "61765", "56576", "41862", "52655", "39178",
"51245", "54846", "51252", "41865", "26627", "26633", "20111",
"19192", "3458", "8753", "8082", "7331", "18038", "20116", "27951",
"26348", "33149", "24365", "32151", "22014", "32459", "31371",
"38781", "59900", "30563", "40837", "47885", "27080", "44045",
"38786", "53065", "41042", "57129", "40420", "47846", "42315",
"44048", "31656", "6442", "7052", "9258", "14410", "10555", "19188",
"27884", "33979", "41399", "39928", "32069", "34796", "39931",
"40008", "49774", "46321", "48767", "26353", "39665", "43246",
"52091", "53071", "61427", "56562", "40428", "52180", "62728",
"48774", "43399", "39575", "35204", "28221", "23458", "26637",
"17853", "16513", "21209", "25556", "27842", "25597", "25991",
"31297", "36208", "54390", "30174", "39673", "30177", "41010",
"46309", "41781", "52294", "61206", "42318", "51654", "66398",
"84164", "52298", "57710", "43416", "56444", "44500", "43880",
"39901", "32468", "28144", "26261", "27515"))
here is an extract of my program
p<-list()
#Creating the graphs year by year
for(a in 1: 10){
#selecting the year
An<-baie[baie$Annee==unique(baie$Annee)[a],]
moyparam<-ddply(An, .(Date, Annee, Mois, Jourannee), function(x) data.frame(Mesure=mean(x$Mesure)))
p[[a]]<-ggplot(data=moyparam, aes(x=moyparam$Date, y=moyparam$Mesure))+geom_point()+theme_bw()
}
grid.arrange(p)
#or
multiplot(plotlist=p, layout=matrix(c(1:10),nrow=2,ncol=5, byrow=TRUE))
I manage to plot each graphs separately, they are even stored in a list, but when i display the list or when i try to do the multiple plot, i get a message:
Error in data.frame(x = c(15349, 15365, 15377, 15392, 15411, 15412,
15419, : arguments imply differing number of rows
Where am I wrong? Maybe the answer is simple, but i think i could use a new point of view on the problem.
Thank you for any help you can give me.
As an update:
thank you to Roland and noah for pointing my errors and helping me so quickly! but here's a precision:
I did not mention it previously, but my code is a bit more complicated than what is written here. In reality, i add a partially colored background on a "risk period" only on years where toxicity of the shelfish is observed (so that i can compare parameters on toxic years (precisely: on risk period) and non toxic years (on the entire year).
so my code is testing if the year is toxic, and if so, it add a color background on the risk period. I did not put it before because my error occurs even without this test, and i mention it now because it explains why i can't use facets grid (or can i? is there a way i can add partially colored background only on some facets?)
If you correct your misuse of $ in aes() the code works as expected,
p[[a]] <- ggplot(data=moyparam, aes(x=Date, y=Mesure)) +
geom_point()+theme_bw()
And here's a more concise way to do the processing:
baie2 = plyr::ddply(baie, .(Date, Annee, Mois, Jourannee),
summarise, Mesure = mean(Mesure))
base_plot = ggplot(baie2, aes(x=Date, y=Mesure)) + geom_point()+theme_bw()
lp = plyr::dlply(baie2, "Annee", `%+%`, e1 = base_plot)
from which you can arrange all plots in a page:
gridExtra::grid.arrange(grobs = lp)
Now, for the broader question, you have two options:
use facetting for the year, and a loop / **ply to open a new page for each site
base_plot + facet_wrap(~Annee, scales="free")
use gridExtra::marrangeGrob, like grid.arrange above but automatically splits the layout into multiple pages if necessary. It also works with ggsave.

Resources