Create monthly averages and then plot them using ggplot - r

I am stuck with a seemingly simple question. I have a dataframe with monthly values from several weather stations.
here is a sample of the raw data, called "cn":
months station temp_davg_c temp_dmax_c temp_dmin_c rain_mm snow_cm precip_mm date
1 Jan courtney 3.0 5.6 0.3 216.0 15.9 231.8 2010-01-01
2 Feb courtney 3.6 7.1 0.0 134.8 9.3 144.1 2010-02-01
3 Mar courtney 5.7 10.0 1.3 127.0 11.3 138.3 2010-03-01
4 Apr courtney 9.1 14.3 3.9 90.7 0.1 90.7 2010-04-01
5 May courtney 12.5 18.1 6.8 53.0 0.0 53.0 2010-05-01
6 Jun courtney 15.5 21.0 9.9 53.0 0.0 53.0 2010-06-01
I can plot all stations, just based on the raw data by doing this. However, what's the point of 14 different lines...
ggplot(data = cn,
aes(x = factor(months), y = temp_davg_c, colour = station))
geom_line(aes(group = station)) +
xlab("Months")+ ylab("Temperature [°C]")+
scale_x_discrete(limits=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"))
So I'd like to obtain monthly averages/mmin/max across the stations. This is where I ran into the first problem:
When I use aggregate to obtain the monthly averages, how do I then save the data in a new df that I can then us for plotting, without having df within dfs?
To get monthly averages/min/max I did this:
stats <- aggregate(cn[,3], list(cn$months), FUN=mean,na.rm=TRUE)
names(stats)[1] <- "months" # rename
names(stats)[2] <- "avg" #rename
stats$max <- (aggregate(cn[,4], list(cn$months), FUN=max,na.rm=TRUE)[2])
stats$min <- (aggregate(cn[,5], list(cn$months), FUN=min,na.rm=TRUE)[2])
Assuming that wasn't a problem, how could I reorder the df so that I have the months in order? I know that I can change the order of a factor by doing this:
factor(stats$months, levels=month.name)
But then how do I do it in the df that I created with all the stats in it?
Since I couldn't figure this out I used "scale_x_discrete" in the ggplot function later, but I was wondering how I would do this.
Now to the final question, how would I plot the monthly overall avg/min/max of all the stations so that for temperature I only end up with three lines?
Assuming that the df within df wasn't an issue, I tried this, assuming I would get my df to look like this:
months avg max min
1 Apr 8.561538 14.3 2.6
2 Aug 17.453846 26.1 10.9
3 Dec 3.075000 6.4 -0.8
4 Feb 3.892308 7.8 -0.7
5 Jan 3.269231 6.8 -0.8
6 Jul 17.446154 25.6 10.8
7 Jun 15.069231 21.9 9.0
8 Mar 5.876923 10.6 0.7
9 May 12.076923 18.6 6.0
10 Nov 5.215385 9.0 0.6
11 Oct 9.230769 14.4 3.8
12 Sep 14.100000 22.4 7.4
ggplot(stats,aes(months))+
geom_line(aes(y=avg)) +
geom_line(aes(y=min)) +
geom_line(aes(y=max)) +
xlab("Months")+ ylab("Temperature [°C]")+
scale_x_discrete(limits=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"))
What am I missing here? Any help is appreciated..
cheers
Sandra
PS: here is a dput of my cn
structure(list(months = structure(c(5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L,
4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L,
9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L,
4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L,
9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L,
4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L), .Label = c("Apr",
"Aug", "Dec", "Feb", "Jan", "Jul", "Jun", "Mar", "May", "Nov",
"Oct", "Sep"), class = "factor"), station = structure(c(7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 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, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L), .Label = c("albernirob", "blackcreeek",
"campbellrivairp", "campbellrivsurf", "capemudge", "comoxairp",
"courtney", "mudbay", "oysterriver", "powriv", "powrivairp",
"qualicumhatch", "qualicumriverres", "stillwater"), class = "factor"),
temp_davg_c = c(3, 3.6, 5.7, 9.1, 12.5, 15.5, 17.9, 17.6,
14.2, 9, 5.1, 3.1, 2.8, 3.4, 5.4, 8.5, 11.7, 14.8, 17.1,
16.9, 13.6, 8.6, 5, 2.8, 2.4, 3.2, 5.2, 8, 11.6, 14.7, 17.3,
17.2, 13.7, 8.6, 4.4, 2.1, 2.6, 3.8, 5.9, 7.4, 11.5, 14.3,
16.2, 17.2, 12.7, 8.1, 4.1, NA, 4.1, 4.6, 6.3, 8.8, 12.1,
14.9, 17.2, 17.1, 14.2, 9.6, 5.8, 3.8, 3.9, 4.3, 6.1, 8.8,
12.4, 15.5, 18, 17.9, 14.5, 9.5, 5.7, 3.5, 3.5, 4, 5.9, 8.6,
12.1, 15.1, 17.5, 17.4, 14.1, 9.3, 5.3, 3.1, 3.3, 3.8, 5.6,
8.3, 12, 15.1, 17.3, 17.2, 13.6, 8.9, 5.2, 3.2, 3.9, 4.2,
5.9, 8.6, 12, 14.9, 17.1, 16.7, 13.6, 9.2, 5.6, 3.5, 2.8,
3.7, 5.8, 8.5, 11.9, 14.9, 17.3, 17.4, 14.1, 9.2, 4.9, 2.6,
2, 3, 5.7, 8.5, 12.3, 15.5, 18.3, 18.5, 15.3, 9.8, 4.6, 1.8,
4.6, 5.1, 7, 9.6, 13, 15.8, 18.4, 18.6, 15.6, 10.8, 6.8,
4.3, 3.6, 3.9, 5.9, 8.6, 11.9, 14.9, 17.2, 17.2, 14.1, 9.4,
5.3, 3.1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
temp_dmax_c = c(5.6, 7.1, 10, 14.3, 18.1, 21, 23.8, 23.7,
20.1, 13, 8, 5.4, 5.8, 7.4, 10, 13.9, 17.3, 20.2, 22.9, 22.8,
19.6, 13, 8.4, 5.4, 5.5, 7.2, 9.7, 13.2, 17, 20.1, 23, 23.3,
19.8, 13.1, 7.7, 4.9, 5.6, 7.5, 10.6, 12.2, 16.7, 19.5, 21.6,
23.2, 18, 12.3, 7.5, NA, 6.6, 7.6, 9.8, 12.9, 16.5, 19.5,
22.1, 22, 18.6, 12.8, 8.5, 6.2, 6.4, 7.4, 9.6, 12.9, 16.6,
19.8, 22.8, 22.7, 19, 12.9, 8.5, 5.9, 6.2, 7.5, 10.1, 13.5,
17.2, 20.3, 23.1, 23.1, 19.5, 13.4, 8.3, 5.6, 6.2, 7.4, 9.8,
13.2, 17.1, 20.2, 22.6, 22.5, 18.9, 12.8, 8.3, 5.8, 6.5,
7.5, 9.9, 12.9, 16.7, 19.6, 22.3, 22.1, 18.7, 13, 8.5, 5.9,
5.5, 7.4, 10.1, 13.5, 17.2, 20.3, 23.1, 23.5, 20, 13.3, 7.8,
5, 4.3, 6.6, 10.5, 14.2, 18.6, 21.9, 25.6, 26.1, 22.4, 14.4,
7.3, 3.8, 6.8, 7.8, 10.4, 13.5, 17.1, 19.8, 22.7, 22.9, 19.5,
13.6, 9, 6.4, 5.8, 6.9, 9.4, 12.8, 16.5, 19.4, 22.1, 22.3,
18.7, 12.6, 7.7, 5.3, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), temp_dmin_c = c(0.3, 0, 1.3, 3.9, 6.8, 9.9,
11.9, 11.5, 8.2, 5, 2.1, 0.7, -0.3, -0.6, 0.9, 3.1, 6.1,
9.3, 11.3, 10.9, 7.5, 4.2, 1.6, 0.2, -0.8, -0.7, 0.7, 2.8,
6.2, 9.3, 11.5, 11.1, 7.6, 4, 1, -0.8, -0.5, 0, 1.3, 2.6,
6.2, 9, 10.8, 11.1, 7.4, 3.8, 0.6, NA, 1.6, 1.5, 2.8, 4.7,
7.7, 10.3, 12.2, 12.2, 9.7, 6.4, 3.1, 1.4, 1.4, 1.2, 2.5,
4.6, 8, 11.1, 13.3, 13, 9.9, 6, 2.9, 0.9, 0.7, 0.5, 1.7,
3.7, 6.9, 9.8, 11.8, 11.7, 8.6, 5.3, 2.3, 0.5, 0.3, 0.1,
1.5, 3.4, 6.9, 9.8, 11.7, 11.7, 8.2, 5, 2, 0.5, 1.2, 0.8,
2, 4.1, 7.3, 10.1, 11.8, 11.3, 8.4, 5.3, 2.7, 0.9, 0.1, 0.1,
1.4, 3.5, 6.6, 9.4, 11.5, 11.2, 8.2, 5, 1.9, 0.2, -0.3, -0.6,
0.7, 2.7, 6, 9, 10.9, 10.9, 8, 5, 1.8, -0.3, 2.3, 2.4, 3.6,
5.6, 8.8, 11.8, 14, 14.3, 11.6, 8, 4.6, 2.2, 1.2, 0.9, 2.3,
4.3, 7.3, 10.4, 12.3, 12.1, 9.4, 6.1, 2.8, 0.9, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), rain_mm = c(216, 134.8,
127, 90.7, 53, 53, 29.9, 35.4, 45.7, 146.9, 232.3, 236.4,
216, 166.8, 149.3, 105, 72.8, 63.2, 42.3, 43.1, 54, 171.8,
256.2, 247.3, 194.6, 135.5, 128.4, 91.6, 68.4, 62.9, 39.4,
44.6, 55.2, 161, 222.1, 204.2, 186, 140.2, 120.3, 87.2, 58.2,
51.3, 35.1, 39, 52.9, 154.8, 228.4, 218.5, 215.2, 135.1,
130.8, 93.6, 70.2, 61.1, 39.5, 45.6, 58.7, 168.6, 241, 220.8,
159.1, 107.8, 95.7, 64.4, 45.6, 42.8, 26.7, 29.2, 41.8, 122.7,
191.9, 168.9, 256.9, 174.1, 151.6, 98, 56.6, 45.2, 26, 37.6,
53.6, 189.7, 285.2, 256.7, 182, 144.2, 139.3, 87.2, 64.6,
54.7, 36.4, 39, 48.9, 152.9, 228.4, 215.9, 200.6, 131.1,
116.3, 79.4, 51.3, 45.3, 26, 34.6, 46.3, 146.8, 214, 180.7,
219.3, 150.4, 141, 101.1, 72.1, 62.8, 41.9, 49.5, 59.3, 180.8,
249.9, 234.2, 317, 222.7, 215.6, 143.6, 87.8, 62.2, 31, 46.4,
61.4, 218.3, 345.2, 323.2, 132, 88.4, 92.4, 70.8, 70.9, 57.4,
36.5, 42.3, 51.4, 117.5, 154.9, 134.5, 145.7, 101.9, 104.2,
83.2, 76.6, 67.6, 37.5, 45.3, 54.7, 125.5, 171.6, 146.5,
185.2, 125.5, 127.8, 99.6, 92.4, 73.7, 46, 50.7, 64.6, 152.1,
212.6, 178.5), snow_cm = c(15.9, 9.3, 11.3, 0.1, 0, 0, 0,
0, 0, 0.2, 6, 12.1, 17.3, 10, 6.7, 0.2, 0, 0, 0, 0, 0, 1.1,
6.4, 16, 23.3, 14.4, 11.7, 0.5, 0, 0, 0, 0, 0, 1.2, 10.5,
22.6, 13.2, 8.4, 7.6, 0, 0, 0, 0, 0, 0, 0.8, 7.3, 14.3, 13.8,
6.4, 6.3, 0.2, 0, 0, 0, 0, 0, 0.6, 6, 14.7, 11.9, 6, 9.9,
0.2, 0, 0, 0, 0, 0, 0.1, 8.2, 18.7, 12.9, 13.3, 8.2, 0, 0,
0, 0, 0, 0, 1.1, 4.8, 15.2, 14.9, 7.8, 4.6, 0, 0, 0, 0, 0,
0, 0.9, 4.1, 8.6, 10.4, 8.8, 4.3, 0, 0, 0, 0, 0, 0, 0.4,
4.2, 9.2, 14.8, 10.1, 7.1, 0.1, 0, 0, 0, 0, 0, 0.5, 7.2,
16.5, 22.6, 16.9, 8.2, 0.6, 0, 0, 0, 0, 0, 1.6, 8, 21.4,
6.1, 4.6, 3.8, 0, 0, 0, 0, 0, 0, 0.2, 3.4, 4.2, 13.6, 7.8,
6.8, 0.1, 0, 0, 0, 0, 0, 0.3, 6.5, 11.5, 8.1, 4.8, 2.7, 0,
0, 0, 0, 0, 0, 0.2, 4.4, 9), precip_mm = c(231.8, 144.1,
138.3, 90.7, 53, 53, 29.9, 35.4, 45.7, 147.1, 238.3, 248.5,
233.3, 176.8, 155.9, 105.2, 72.8, 63.2, 42.3, 43.1, 54, 172.9,
262.6, 263.3, 217.5, 149.5, 140, 92.1, 68.4, 62.9, 39.4,
44.6, 55.2, 162.2, 231.9, 225.7, 198.9, 148.6, 127.9, 87.2,
58.2, 51.3, 35.1, 39, 52.9, 155.6, 235.7, 232.8, 229.1, 141.4,
137.1, 93.8, 70.2, 61.1, 39.5, 45.6, 58.7, 169.2, 246.9,
235.5, 171.9, 114.3, 105.7, 64.6, 45.6, 42.8, 26.7, 29.2,
41.8, 122.8, 200.5, 187.9, 269.9, 187.4, 159.8, 98, 56.6,
45.2, 26, 37.6, 53.6, 190.8, 290, 272, 196.9, 151.9, 143.9,
87.2, 64.6, 54.7, 36.4, 39, 48.9, 153.8, 232.6, 224.5, 211,
139.9, 120.6, 79.4, 51.3, 45.3, 26, 34.6, 46.3, 147.2, 218.1,
189.8, 234.1, 160.4, 148, 101.2, 72.1, 62.8, 41.9, 49.5,
59.3, 181.3, 257.1, 250.7, 339.5, 239.6, 223.8, 144.2, 87.8,
62.2, 31, 46.4, 61.4, 219.8, 353.2, 344.6, 138.1, 93.1, 96.1,
70.8, 70.9, 57.4, 36.5, 42.3, 51.4, 117.7, 158.3, 138.7,
158.9, 109.4, 110.7, 83.3, 76.6, 67.6, 37.5, 45.3, 54.7,
125.8, 178, 157.8, 193.3, 130.3, 130.6, 99.6, 92.4, 73.7,
46, 50.7, 64.6, 152.3, 216.9, 187.5), date = structure(c(14610,
14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
14914, 14944, 14610, 14641, 14669, 14700, 14730, 14761, 14791,
14822, 14853, 14883, 14914, 14944, 14610, 14641, 14669, 14700,
14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14610,
14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
14914, 14944, 14610, 14641, 14669, 14700, 14730, 14761, 14791,
14822, 14853, 14883, 14914, 14944, 14610, 14641, 14669, 14700,
14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14610,
14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
14914, 14944, 14610, 14641, 14669, 14700, 14730, 14761, 14791,
14822, 14853, 14883, 14914, 14944, 14610, 14641, 14669, 14700,
14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14610,
14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
14914, 14944, 14610, 14641, 14669, 14700, 14730, 14761, 14791,
14822, 14853, 14883, 14914, 14944, 14610, 14641, 14669, 14700,
14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14610,
14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
14914, 14944, 14610, 14641, 14669, 14700, 14730, 14761, 14791,
14822, 14853, 14883, 14914, 14944), class = "Date")), .Names = c("months",
"station", "temp_davg_c", "temp_dmax_c", "temp_dmin_c", "rain_mm",
"snow_cm", "precip_mm", "date"), row.names = c(NA, -168L), class = "data.frame")

You can do this in one shot, if you pipe your data directly into ggplot() using dplyr and tidyr :
library(dplyr)
library(tidyr)
library(ggplot2)
correct_order <- c("Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec")
cn %>% group_by(months) %>%
summarise(min = min(temp_dmin_c, na.rm = TRUE),
max = max(temp_dmax_c, na.rm = TRUE),
avg = mean(temp_davg_c,na.rm = TRUE)) %>%
gather(metric, value, -months) %>%
ggplot(.,aes(x = months, y = value,
group = metric, color = metric)) +
scale_x_discrete(limits=correct_order) +
geom_line()

Related

How to include Pairwise wilcox test in tbl_summary()?

I really cannot find anything usefull online for this problem. I've got dataset where one variable has been measured on 4 different occasion (CRP1, CRP4, CRP7, CRP10) and I've run pairwise Wilcoxon test to compare CRP drop trend between 2 groups. I've also made ggplot to show the significant p values.
Now I want to present my p values through the tbl_summary() function, but it seems that I cannot accomplish that. Wilcoxon test that I've run to get my p values was performed on the long format of my dataset.
structure(list(LEK = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), levels = c("Lek +", "Lek -"), class = "factor", label = "Terapija"),
CRP1 = c(103.9, 155.6, 102.2, 89.2, 32.3, 258.8, 58.5, 196.7,
89.3, 175, 170.9, 204.3, 82.2, 196.9, 220.4, 92, 37.1, 34,
223.6, 261.5, 82, 37.4, 112, 81.8, 47.5, 70.1, 137.2, 84.7,
151.9, 159.8, 149.7, 140, 37.9, 143, 130.6, 110.7, 112.9,
48.1, 115.5, 43, 102.1, 35.9, 120.3, 40.9, 169.9, 105.6,
90, 139.6, 246.4, 146.9, 13.9, 60.9, 123.1, 187.3, 23.6,
112.9, 17.5, 9.9, 120.4, 103.7, 12.4, 96.7, 144.9, 54.1,
186.6, 143.6, 30.6, 41.8, 146.4, 94.9, 144.2, 98.5, 63.3,
137.1, 81.1, 14.1, 117.3, 55.4, 92.7, 40.3, 189.7, 77.2,
36.7, 73.7, 19.8, 39.1, 119, 60.6, 110.6, 63.2, 135.1, 131.6,
206.9, 117.1, 92.6, 123.3, 297, 153.3, 210.4, 116.1, 59.3,
177.3, 37.1, 101.2, 87.8, 138.6, 88.9, 95.6, 71.6, 81.1,
394.3, 4.8, 36.4, 229.3, 108.4, 404.1, 259.5, 292.9, 134.1,
127.5, 33.2, 29.2, 346.3, 116, 302), CRP4 = c(21, 74.7, 26.3,
48.1, 23.7, 86, 15.1, 33.7, 29.3, 16.9, 115, 79.5, 48, 58.5,
332.2, 153, 28.2, 11.6, 94.4, 50.2, 85.8, 48.7, 25, 14.3,
150.2, 145.5, 64.2, 28, 143.9, 57.6, 11, 132.9, 96.9, 44.4,
200.2, 45.2, 7.4, 95, 38.1, 12.5, 29.2, 6.8, 104.5, 15.3,
32.9, 26.5, 25, 49.7, 142.5, 37.5, 6.4, 32.1, 44.3, 70.9,
22.2, 72.2, 40.2, 3.5, 9.3, 72.5, 36, 17.9, 161.8, 18.5,
48.2, 198.7, 52.8, 77.6, 93.7, 162.7, 45.6, 206.4, 269.9,
21.1, 14.1, 22, 97.3, 52.2, 61, 34.8, 45.9, 43.6, 16.4, 203.6,
35.5, 28.1, 87.7, 23.2, 35.8, 44.2, 104.4, 83.7, 49.2, 23.5,
21.7, 118.6, 78.8, 101.6, 162.5, 23.9, 21.6, 109.3, 62.8,
146.1, 84.6, 57.6, 225.3, 143.4, 104.1, 29.7, 319.5, 104.5,
110.2, 120.3, 99.7, 172.1, 293.3, 262.9, 190.2, 82.4, 129.1,
5.5, 75.2, 36.8, 69.2), CRP7 = c(2.8, 110.8, 63.4, 51.3,
20.8, 27.8, 2.2, 194.6, 24.2, 8.3, 70.7, 93.3, 6.4, 38.3,
188.3, 75.1, 49.4, 5, 107.2, 37.8, 246.3, 26.4, 4.2, 4.3,
28.2, 22.4, 9.1, 195.9, 150.7, 67.4, 8.6, 283.6, 63.1, 100.9,
82.7, 9.9, 7.6, 207, 6.7, 9.2, 245.8, 42.5, 179.8, 12.3,
4.2, 6, 8.8, 5.9, 28.8, 27, 3.5, 24.8, 14.4, 55.5, 3.9, 106.7,
49.8, 11.1, 3.77, 68, 52.4, 32.7, 223.6, 12.3, 117.7, 66.1,
184.5, 29.3, 174.7, 119.3, 80.2, 87.9, 135.6, 22.8, 12.2,
82.7, 9.1, 32.3, 21.3, 82, 12.1, 37.8, 48.2, 56.6, 6.5, 37,
112.9, 11, 142.8, 18.4, 71.5, 91.1, 8.9, 7, 166.7, 55.4,
123.8, 46.8, 64.5, 5, 10.5, 201.7, 188.5, 198.7, 271.8, 276,
181.8, 190.2, 164.6, 65.1, 322.8, 61.9, 195.7, 225.5, 66.6,
119.4, 268.5, 350.3, 223.2, 161, 34.3, 22.4, 243.8, 62, 39.8
), CRP10 = c(NA, NA, NA, 184.3, 4.4, 7.7, NA, NA, 1.2, 1,
1, 12.3, 2.5, 62.2, 43.4, 57.7, 100.3, 15.6, 4.2, 11.5, NA,
8.3, 1, 1.3, 11.9, 63, NA, 71.4, 60.3, 54.6, 6.7, 313.8,
37, NA, 123.7, 2.5, 2.2, NA, 252.4, 9.7, NA, 82.2, 230.8,
5.8, 1, NA, 3.9, 1, 6.9, 34.7, 2.6, NA, 15.2, 6.4, 6.1, NA,
214.7, NA, 22.5, 86.5, 13.9, 41, 246.5, 9, 26.5, 270, 270.8,
7.6, 65.7, 90.5, 202.3, 288.6, 464, 92.1, 19.7, 307.5, 10.6,
71.2, 80.6, 159.8, 4.1, 103.7, 80.6, 324, 9.5, 6.3, 9.6,
4.1, 151.5, 20.3, 63.6, 311, 2.8, 52.8, 62.3, 13.5, 248,
72.2, 83.5, 13.8, 37.8, 179, 72.4, 206.4, 76.4, 210.6, 69.5,
87.9, 303.3, 59.1, 174.5, 211, 211.2, 240.8, 38.6, 109.6,
251.7, 328.9, 87.1, 113.5, 48.9, 16.4, 277, 25.7, 122)), row.names = c(NA,
-125L), class = c("tbl_df", "tbl", "data.frame"))
LongCRP <- Statistika %>%
select(LEK, CRP1, CRP4, CRP7, CRP10) %>%
filter(complete.cases(.)) %>%
gather("Vreme", "CRP", CRP1, CRP4, CRP7, CRP10) %>%
mutate(Vreme = factor(Vreme, levels = c("CRP1", "CRP4", "CRP7", "CRP10"))) #Long format
pairwise.wilcox.CRP <- LongCRP %>%
group_by(LEK) %>%
pairwise_wilcox_test(
CRP ~ Vreme
)
pairwise.wilcox.CRP <- pairwise.wilcox.CRP %>% add_xy_position(x = "LEK")
ggplot(LongCRP, aes(x = LEK, y = CRP, color = Vreme)) +
geom_boxplot() +
stat_pvalue_manual(pairwise.wilcox.CRP, label = "p.adj.signif",
step.increase = 0.03,
bracket.nudge.y = 20) +
theme_minimal() +
ggtitle("Trend opadanja vrednosti CRP-a kod lečenih i nelečenih bolesnika") +
labs(x = "Terapija") +
scale_color_discrete(name = "Vreme\nmerenja CRP-a")
GGplot
Now I know that I can extract p values from my ggplot graph, but I would really want to use tbl_summary() for this.
Thank you for the help!

Getting different ggplot when using purrr::map

I am trying to use map() to generate a list of ggplot objects that I will combine with ggpubr::ggarrange after.
The stock ggplot call works fine:
# library(tidyverse)
ggplot(Tile,aes(x = Site, y = Volume))+
geom_point(aes(color = Event))+
geom_line(aes(x = Site, y = Volume, group = Event))+
ggtitle(paste("Tile", 'Volume'))
output (desired plot):
Now I use map() with the slightly modified but overall same ggplot call (should be):
x<-map(names(Tile)[-(1:2)], ~
ggplot(Tile,aes(x = Site, y = .x))+
geom_point(aes(color = Event))+
geom_line(aes(x = Site, y = .x, group = Event))+
ggtitle(paste("Tile", as.character(.x)))
)
which gives:
x[[5]]
What am I missing? Thanks.
data:
Tile<-structure(list(Event = c("10/17/2019", "10/17/2019", "10/23/2019",
"10/23/2019", "10/27/2019", "10/27/2019", "10/31/2019", "10/31/2019",
"11/24/2019", "11/24/2019", "11/28/2019", "11/28/2019", "12/10/2019",
"12/10/2019", "12/15/2019", "12/15/2019", "12/28/2019", "12/28/2019",
"12/30/2019", "12/30/2019", "1/3/2020", "1/3/2020", "1/12/2020",
"1/12/2020", "1/26/2020", "1/26/2020", "3/3/2020", "3/3/2020",
"3/8/2020", "3/8/2020", "3/13/2020", "3/13/2020", "5/12/2020",
"5/12/2020", "8/5/2020", "8/5/2020", "9/30/2020", "9/30/2020",
"12/1/2020", "12/1/2020", "12/25/2020", "12/25/2020", "1/17/2021",
"1/17/2021", "3/11/2021", "3/11/2021", "4/16/2021", "4/16/2021",
"4/22/2021", "4/22/2021", "4/30/2021", "4/30/2021", "5/6/2021",
"5/6/2021", "7/2/2021", "7/2/2021", "7/3/2021", "7/3/2021", "7/9/2021",
"7/9/2021", "7/13/2021", "7/13/2021", "7/14/2021", "7/14/2021",
"7/18/2021", "7/18/2021", "7/19/2021", "7/19/2021", "7/21/2021",
"7/21/2021", "7/31/2021", "7/31/2021", "8/2/2021", "8/2/2021",
"8/20/2021", "8/20/2021", "9/9/2021", "9/9/2021", "9/24/2021",
"9/24/2021", "10/17/2021", "10/17/2021", "10/22/2021", "10/22/2021",
"10/25/2021", "10/25/2021", "10/27/2021", "10/27/2021", "11/1/2021",
"11/1/2021"), Site = structure(c(3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L,
1L, 3L, 1L), .Label = c("DCNSUB", "DCNSUR", "DCSSUB", "DCSSUR"
), class = "factor"), TP.conc = c(1550, 2770, NA, NA, 1650, NA,
1810, NA, 666, 468, 1190, 1120, 574, 538, 487, 580, NA, 238,
610, 378, 398, 306, 744, 766, 447, 468, 504, 413, 384, 377, 714,
542, 927.2, 1000.1, 265.4, 285.1, 1527, 1764.5, NA, 460.9, NA,
469.8, NA, 172.8, 454, 432.8, 524.4, 476.4, 300, 303.6, 588.7,
598.1, 852.5, 797.6, 144.4, 122.1, 170.6, 110.1, 301.8, 328.8,
363.3, 498.5, 283.7, 104.9, 314.1, 327.6, 436.6, 262.1, 398.1,
358, 312, 251, 598, 831, 348, 456, 345, 240, 648, 949, 852, 1260,
643, 549, 712, 999, 982, 1100, 1240, 1555), TP.load = c(180.4,
NA, NA, NA, 67.6, NA, 201.5, NA, NA, NA, 53.3, 131.7, 12.1, 38.1,
7, 21.6, NA, NA, 21.2, 44.4, 6.1, 10.9, 79.7, 189.9, 10.5, 27.9,
84.2, 178.7, 13.6, 46.3, 14.4, 26.2, 11.4, 35, 4.2, 10.1, 4.6,
18.9, NA, 58.3, NA, 140.9, NA, 7.6, 181.8, 238.2, 72.5, 97.7,
18.5, 30.6, 121.4, 177.7, 114.1, 166.3, 22.8, 21.9, 15.1, 9.2,
25.8, 29.4, 7.6, 12.3, 3, 1.7, 58.5, 71, 23.3, 15.7, 32.7, 39.7,
3.1, 3.4, 67.2, 126, 49.1, 79.1, 8.6, 5.8, 8.7, 15.3, 38.62755,
62.40857143, NA, NA, NA, NA, NA, NA, NA, NA), SRP.conc = c(NA,
NA, NA, NA, 403, NA, NA, NA, NA, NA, NA, NA, 245, 234, 238, 197,
NA, 118, NA, NA, NA, NA, NA, 270, 121, 135, NA, NA, NA, NA, NA,
NA, 596.7, 635.6, 48, 85.9, 514.8, 572.7, NA, 161.5, NA, 163.3,
NA, 46.4, 96.9, 127, 83.1, 92.3, 53.5, 60.7, 111.7, 133.7, 132.2,
164.1, 50.1, 49.1, 54, 42.5, 122.5, 131.9, 104.2, 194.5, 84.6,
34.8, 90.2, 106.6, NA, NA, 129.9, 118.2, 62.2, 84.7, 105, 152,
92.6, 66, 45.9, 50.5, 66.2, 167, 264, 412, 203, 175, 352, 560,
503, 625, 621, 836), SRP.load = c(NA, NA, NA, NA, 16.5, NA, NA,
NA, NA, NA, NA, NA, 5.2, 16.6, 3.4, 7.3, NA, NA, NA, NA, NA,
NA, NA, 66.9, 2.8, 8, NA, NA, NA, NA, NA, NA, 7.3, 22.2, 0.8,
3.1, 1.6, 6.1, NA, 20.4, NA, 49, NA, 2, 38.8, 69.9, 11.5, 18.9,
3.3, 6.1, 23, 39.7, 17.7, 34.2, 7.9, 8.8, 4.8, 3.6, 10.5, 11.8,
2.2, 4.8, 0.9, 0.6, 16.8, 23.1, NA, NA, 10.7, 13.1, 0.6, 1.2,
11.8, 23, 13.1, 11.4, 1.1, 1.2, 0.9, 2.7, 12, 20.4, NA, NA, NA,
NA, NA, NA, NA, NA), Volume = c(11.64, NA, 1.87, 4.5, 4.1, 9.69,
11.13, 34, NA, NA, 4.48, 11.76, 2.1, 7.08, 1.45, 3.73, NA, NA,
3.47, 11.74, 1.52, 3.56, 10.71, 24.79, 2.34, 5.96, 16.71, 43.28,
3.54, 12.29, 2.02, 4.84, 1.22, 3.5, 1.59, 3.56, 0.3, 1.07, NA,
12.66, NA, 29.99, NA, 4.37, 40.04, 55.03, 13.82, 20.51, 6.18,
10.07, 20.62, 29.72, 13.38, 20.85, 15.76, 17.96, 8.82, 8.36,
8.56, 8.94, 2.1, 2.46, 1.07, 1.58, 18.64, 21.67, 5.33, 6, 8.22,
11.09, 0.99, 1.36, 11.23, 15.16, 14.11, 17.34, 2.48, 2.4, 1.34,
1.61, 4.53, 4.95, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c(1L,
4L, 5L, 8L, 9L, 12L, 13L, 16L, 17L, 20L, 21L, 24L, 25L, 28L,
29L, 32L, 33L, 36L, 37L, 40L, 41L, 44L, 45L, 48L, 49L, 52L, 53L,
56L, 57L, 60L, 61L, 64L, 65L, 68L, 69L, 72L, 73L, 76L, 77L, 80L,
81L, 84L, 85L, 88L, 89L, 92L, 93L, 96L, 97L, 100L, 101L, 104L,
105L, 108L, 109L, 112L, 113L, 116L, 117L, 120L, 121L, 124L, 125L,
128L, 129L, 132L, 133L, 136L, 137L, 140L, 141L, 144L, 145L, 148L,
149L, 152L, 153L, 156L, 157L, 160L, 161L, 164L, 165L, 168L, 169L,
172L, 173L, 176L, 177L, 180L), class = "data.frame")
The .x is the individual column names, as aes can take only symbol or unquoted column names, use .data to subset the data column or another option is to convert to symbol and evaluate (!!). But, it is better to use .data
library(purrr)
library(ggplot2)
x <- map(names(Tile)[-(1:2)], ~
ggplot(Tile,aes(x = Site, y = .data[[.x]]))+
geom_point(aes(color = Event))+
geom_line(aes(x = Site, y = .data[[.x]], group = Event))+
ggtitle(paste("Surface", .x))
)
-output
x[[5]]

Creating a plot for each column of a dataframe and create a list of plots

I want to create a QQplot for each column in a dataframe then return as a list of plots
A small section of my df
structure(list(LAB.ID = c(4L, 3L, 8L, 7L, 4L, 5L, 2L, 2L, 3L,
10L, 5L, 12L, 7L, 12L, 7L, 10L, 2L, 8L, 5L, 12L, 4L, 8L, 10L,
3L, 4L, 5L, 10L, 3L, 7L, 5L, 8L, 3L, 12L, 4L, 2L, 2L, 10L, 3L,
4L, 8L, 2L, 5L, 10L, 12L, 7L, 7L, 8L, 12L), Fe = c(56.39, 56.83,
56.382, 56.48, 56.43, 56.32, 55.94, 55.7, 56.54, 56.3, 56.29,
56.11, 56.4, 56.46, 56.54, 56.5, 56.59, 56.258, 56.31, 56.1,
56.53, 56.442, 56.2, 56.18, 56.31, 56.32, 56.5, 56.5, 56.43,
56.39, 56.258, 56.51, 56.35, 56.47, 56.5, 55.98, 56.7, 56.34,
56.35, 56.532, 55.93, 56.32, 56.5, 56.36, 56.73, 56.62, 56.264,
56.37), SiO2 = c(7.67, 7.84, 7.936, 7.77, 7.74, 7.91, 7.63, 7.65,
7.69, 7.872684992, 7.84, 7.64, 7.83, 7.71, 7.76, 7.851291827,
7.73, 7.685, 7.96, 7.71, 7.62, 7.863, 7.872684992, 7.59, 7.81,
7.87, 7.722932832, 7.77, 7.78, 7.84, 7.838, 7.74, 7.65, 7.66,
7.67, 7.67, 7.680146501, 7.64, 7.8, 7.828, 7.67, 7.92, 7.615967003,
7.82, 7.65, 7.74, 7.767, 7.68), Al2O3 = c(2, 2.01, 2.053, 1.88,
2.03, 2.02, 2.01, 2.02, 2.01, 2.002830415, 2.02, 2.09, 1.9, 2.05,
1.89, 2.021725042, 2.03, 2.044, 2.05, 1.96, 1.99, 2.041, 2.021725042,
2, 2.01, 2.03, 1.983935789, 2.02, 1.88, 2.02, 2.038, 2.02, 2.09,
2.01, 2.01, 2.02, 2.002830415, 2.03, 2.01, 2.008, 2, 2.03, 2.021725042,
2.06, 1.88, 1.87, 2.02, 2.02)), row.names = c(NA, -48L), class = "data.frame")
I have the following code
library(purr)
qqplots <- imap(df[-1], ~{
ggplot(df[-1], aes(sample = .y)) + # Create QQplot with ggplot2 package
ggtitle(paste0(.y, " Q-Q Plot")) +
theme(plot.title = element_text(hjust = 0.5)) +
ylab('Grade %')+
stat_qq() +
stat_qq_line(col = "red", lwd = 0.5)
})
which produces many plots like below
but what I am expecting is something like this
what am I doing wrong?
You can use :
library(ggplot2)
qqplots <- purrr::imap(df[-1], ~{
ggplot(df, aes(sample = .data[[.y]])) + # Create QQplot with ggplot2 package
ggtitle(paste0(.y, " Q-Q Plot")) +
theme(plot.title = element_text(hjust = 0.5)) +
ylab('Grade %')+
stat_qq() +
stat_qq_line(col = "red", lwd = 0.5)
})
Or with lapply :
qqplots <- lapply(names(df)[-1], function(x) {
ggplot(df, aes(sample = .data[[x]])) +
ggtitle(paste0(x, " Q-Q Plot")) +
theme(plot.title = element_text(hjust = 0.5)) +
ylab('Grade %')+
stat_qq() +
stat_qq_line(col = "red", lwd = 0.5)
})

Stacked barplot in ggplot of numeric values

I am trying to plot monthly average precipitation broken down into snow and rain in a stacked barplot. From searching around on this site I found some code that does what I want, however, since I am not fully understanding the code I am not able to change the aesthetics of it.
Below is the code that creates the plot that I want, but it looks..well .. a little "ugly". Usually when working with ggplot I save the plot to a variable and then keep adding and changing things. Since in this code the plot function is embedded I don't know how to save the plot output to a variable.
correct_order <- c("Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec")
cn %>% group_by(Months) %>%
summarise(Rain = mean(rain_mm,na.rm = TRUE),Snow = mean(snow_cm,na.rm = TRUE)) %>%
gather(Legend, Precipitation, -Months) %>%
ggplot(.,aes(x = Months, y = Precipitation,
group = Legend, color = Legend)) +
scale_x_discrete(limits=correct_order) +
geom_bar(stat="identity")
Below is a dput of my dataset.
structure(list(Months = structure(c(5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L,
4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L,
9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L,
4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L,
9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L,
2L, 12L, 11L, 10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L,
10L, 3L, 5L, 4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L, 5L,
4L, 8L, 1L, 9L, 7L, 6L, 2L, 12L, 11L, 10L, 3L), .Label = c("Apr",
"Aug", "Dec", "Feb", "Jan", "Jul", "Jun", "Mar", "May", "Nov",
"Oct", "Sep"), class = "factor"), station = structure(c(7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 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, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 13L,
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L), .Label = c("albernirob", "blackcreeek",
"campbellrivairp", "campbellrivsurf", "capemudge", "comoxairp",
"courtney", "mudbay", "oysterriver", "powriv", "powrivairp",
"qualicumhatch", "qualicumriverres", "stillwater"), class = "factor"),
temp_davg_c = c(3, 3.6, 5.7, 9.1, 12.5, 15.5, 17.9, 17.6,
14.2, 9, 5.1, 3.1, 2.8, 3.4, 5.4, 8.5, 11.7, 14.8, 17.1,
16.9, 13.6, 8.6, 5, 2.8, 2.4, 3.2, 5.2, 8, 11.6, 14.7, 17.3,
17.2, 13.7, 8.6, 4.4, 2.1, 2.6, 3.8, 5.9, 7.4, 11.5, 14.3,
16.2, 17.2, 12.7, 8.1, 4.1, NA, 4.1, 4.6, 6.3, 8.8, 12.1,
14.9, 17.2, 17.1, 14.2, 9.6, 5.8, 3.8, 3.9, 4.3, 6.1, 8.8,
12.4, 15.5, 18, 17.9, 14.5, 9.5, 5.7, 3.5, 3.5, 4, 5.9, 8.6,
12.1, 15.1, 17.5, 17.4, 14.1, 9.3, 5.3, 3.1, 3.3, 3.8, 5.6,
8.3, 12, 15.1, 17.3, 17.2, 13.6, 8.9, 5.2, 3.2, 3.9, 4.2,
5.9, 8.6, 12, 14.9, 17.1, 16.7, 13.6, 9.2, 5.6, 3.5, 2.8,
3.7, 5.8, 8.5, 11.9, 14.9, 17.3, 17.4, 14.1, 9.2, 4.9, 2.6,
2, 3, 5.7, 8.5, 12.3, 15.5, 18.3, 18.5, 15.3, 9.8, 4.6, 1.8,
4.6, 5.1, 7, 9.6, 13, 15.8, 18.4, 18.6, 15.6, 10.8, 6.8,
4.3, 3.6, 3.9, 5.9, 8.6, 11.9, 14.9, 17.2, 17.2, 14.1, 9.4,
5.3, 3.1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
temp_dmax_c = c(5.6, 7.1, 10, 14.3, 18.1, 21, 23.8, 23.7,
20.1, 13, 8, 5.4, 5.8, 7.4, 10, 13.9, 17.3, 20.2, 22.9, 22.8,
19.6, 13, 8.4, 5.4, 5.5, 7.2, 9.7, 13.2, 17, 20.1, 23, 23.3,
19.8, 13.1, 7.7, 4.9, 5.6, 7.5, 10.6, 12.2, 16.7, 19.5, 21.6,
23.2, 18, 12.3, 7.5, NA, 6.6, 7.6, 9.8, 12.9, 16.5, 19.5,
22.1, 22, 18.6, 12.8, 8.5, 6.2, 6.4, 7.4, 9.6, 12.9, 16.6,
19.8, 22.8, 22.7, 19, 12.9, 8.5, 5.9, 6.2, 7.5, 10.1, 13.5,
17.2, 20.3, 23.1, 23.1, 19.5, 13.4, 8.3, 5.6, 6.2, 7.4, 9.8,
13.2, 17.1, 20.2, 22.6, 22.5, 18.9, 12.8, 8.3, 5.8, 6.5,
7.5, 9.9, 12.9, 16.7, 19.6, 22.3, 22.1, 18.7, 13, 8.5, 5.9,
5.5, 7.4, 10.1, 13.5, 17.2, 20.3, 23.1, 23.5, 20, 13.3, 7.8,
5, 4.3, 6.6, 10.5, 14.2, 18.6, 21.9, 25.6, 26.1, 22.4, 14.4,
7.3, 3.8, 6.8, 7.8, 10.4, 13.5, 17.1, 19.8, 22.7, 22.9, 19.5,
13.6, 9, 6.4, 5.8, 6.9, 9.4, 12.8, 16.5, 19.4, 22.1, 22.3,
18.7, 12.6, 7.7, 5.3, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA), temp_dmin_c = c(0.3, 0, 1.3, 3.9, 6.8, 9.9,
11.9, 11.5, 8.2, 5, 2.1, 0.7, -0.3, -0.6, 0.9, 3.1, 6.1,
9.3, 11.3, 10.9, 7.5, 4.2, 1.6, 0.2, -0.8, -0.7, 0.7, 2.8,
6.2, 9.3, 11.5, 11.1, 7.6, 4, 1, -0.8, -0.5, 0, 1.3, 2.6,
6.2, 9, 10.8, 11.1, 7.4, 3.8, 0.6, NA, 1.6, 1.5, 2.8, 4.7,
7.7, 10.3, 12.2, 12.2, 9.7, 6.4, 3.1, 1.4, 1.4, 1.2, 2.5,
4.6, 8, 11.1, 13.3, 13, 9.9, 6, 2.9, 0.9, 0.7, 0.5, 1.7,
3.7, 6.9, 9.8, 11.8, 11.7, 8.6, 5.3, 2.3, 0.5, 0.3, 0.1,
1.5, 3.4, 6.9, 9.8, 11.7, 11.7, 8.2, 5, 2, 0.5, 1.2, 0.8,
2, 4.1, 7.3, 10.1, 11.8, 11.3, 8.4, 5.3, 2.7, 0.9, 0.1, 0.1,
1.4, 3.5, 6.6, 9.4, 11.5, 11.2, 8.2, 5, 1.9, 0.2, -0.3, -0.6,
0.7, 2.7, 6, 9, 10.9, 10.9, 8, 5, 1.8, -0.3, 2.3, 2.4, 3.6,
5.6, 8.8, 11.8, 14, 14.3, 11.6, 8, 4.6, 2.2, 1.2, 0.9, 2.3,
4.3, 7.3, 10.4, 12.3, 12.1, 9.4, 6.1, 2.8, 0.9, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), rain_mm = c(216, 134.8,
127, 90.7, 53, 53, 29.9, 35.4, 45.7, 146.9, 232.3, 236.4,
216, 166.8, 149.3, 105, 72.8, 63.2, 42.3, 43.1, 54, 171.8,
256.2, 247.3, 194.6, 135.5, 128.4, 91.6, 68.4, 62.9, 39.4,
44.6, 55.2, 161, 222.1, 204.2, 186, 140.2, 120.3, 87.2, 58.2,
51.3, 35.1, 39, 52.9, 154.8, 228.4, 218.5, 215.2, 135.1,
130.8, 93.6, 70.2, 61.1, 39.5, 45.6, 58.7, 168.6, 241, 220.8,
159.1, 107.8, 95.7, 64.4, 45.6, 42.8, 26.7, 29.2, 41.8, 122.7,
191.9, 168.9, 256.9, 174.1, 151.6, 98, 56.6, 45.2, 26, 37.6,
53.6, 189.7, 285.2, 256.7, 182, 144.2, 139.3, 87.2, 64.6,
54.7, 36.4, 39, 48.9, 152.9, 228.4, 215.9, 200.6, 131.1,
116.3, 79.4, 51.3, 45.3, 26, 34.6, 46.3, 146.8, 214, 180.7,
219.3, 150.4, 141, 101.1, 72.1, 62.8, 41.9, 49.5, 59.3, 180.8,
249.9, 234.2, 317, 222.7, 215.6, 143.6, 87.8, 62.2, 31, 46.4,
61.4, 218.3, 345.2, 323.2, 132, 88.4, 92.4, 70.8, 70.9, 57.4,
36.5, 42.3, 51.4, 117.5, 154.9, 134.5, 145.7, 101.9, 104.2,
83.2, 76.6, 67.6, 37.5, 45.3, 54.7, 125.5, 171.6, 146.5,
185.2, 125.5, 127.8, 99.6, 92.4, 73.7, 46, 50.7, 64.6, 152.1,
212.6, 178.5), snow_cm = c(15.9, 9.3, 11.3, 0.1, 0, 0, 0,
0, 0, 0.2, 6, 12.1, 17.3, 10, 6.7, 0.2, 0, 0, 0, 0, 0, 1.1,
6.4, 16, 23.3, 14.4, 11.7, 0.5, 0, 0, 0, 0, 0, 1.2, 10.5,
22.6, 13.2, 8.4, 7.6, 0, 0, 0, 0, 0, 0, 0.8, 7.3, 14.3, 13.8,
6.4, 6.3, 0.2, 0, 0, 0, 0, 0, 0.6, 6, 14.7, 11.9, 6, 9.9,
0.2, 0, 0, 0, 0, 0, 0.1, 8.2, 18.7, 12.9, 13.3, 8.2, 0, 0,
0, 0, 0, 0, 1.1, 4.8, 15.2, 14.9, 7.8, 4.6, 0, 0, 0, 0, 0,
0, 0.9, 4.1, 8.6, 10.4, 8.8, 4.3, 0, 0, 0, 0, 0, 0, 0.4,
4.2, 9.2, 14.8, 10.1, 7.1, 0.1, 0, 0, 0, 0, 0, 0.5, 7.2,
16.5, 22.6, 16.9, 8.2, 0.6, 0, 0, 0, 0, 0, 1.6, 8, 21.4,
6.1, 4.6, 3.8, 0, 0, 0, 0, 0, 0, 0.2, 3.4, 4.2, 13.6, 7.8,
6.8, 0.1, 0, 0, 0, 0, 0, 0.3, 6.5, 11.5, 8.1, 4.8, 2.7, 0,
0, 0, 0, 0, 0, 0.2, 4.4, 9), precip_mm = c(231.8, 144.1,
138.3, 90.7, 53, 53, 29.9, 35.4, 45.7, 147.1, 238.3, 248.5,
233.3, 176.8, 155.9, 105.2, 72.8, 63.2, 42.3, 43.1, 54, 172.9,
262.6, 263.3, 217.5, 149.5, 140, 92.1, 68.4, 62.9, 39.4,
44.6, 55.2, 162.2, 231.9, 225.7, 198.9, 148.6, 127.9, 87.2,
58.2, 51.3, 35.1, 39, 52.9, 155.6, 235.7, 232.8, 229.1, 141.4,
137.1, 93.8, 70.2, 61.1, 39.5, 45.6, 58.7, 169.2, 246.9,
235.5, 171.9, 114.3, 105.7, 64.6, 45.6, 42.8, 26.7, 29.2,
41.8, 122.8, 200.5, 187.9, 269.9, 187.4, 159.8, 98, 56.6,
45.2, 26, 37.6, 53.6, 190.8, 290, 272, 196.9, 151.9, 143.9,
87.2, 64.6, 54.7, 36.4, 39, 48.9, 153.8, 232.6, 224.5, 211,
139.9, 120.6, 79.4, 51.3, 45.3, 26, 34.6, 46.3, 147.2, 218.1,
189.8, 234.1, 160.4, 148, 101.2, 72.1, 62.8, 41.9, 49.5,
59.3, 181.3, 257.1, 250.7, 339.5, 239.6, 223.8, 144.2, 87.8,
62.2, 31, 46.4, 61.4, 219.8, 353.2, 344.6, 138.1, 93.1, 96.1,
70.8, 70.9, 57.4, 36.5, 42.3, 51.4, 117.7, 158.3, 138.7,
158.9, 109.4, 110.7, 83.3, 76.6, 67.6, 37.5, 45.3, 54.7,
125.8, 178, 157.8, 193.3, 130.3, 130.6, 99.6, 92.4, 73.7,
46, 50.7, 64.6, 152.3, 216.9, 187.5), date = structure(c(14610,
14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
14914, 14944, 14610, 14641, 14669, 14700, 14730, 14761, 14791,
14822, 14853, 14883, 14914, 14944, 14610, 14641, 14669, 14700,
14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14610,
14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
14914, 14944, 14610, 14641, 14669, 14700, 14730, 14761, 14791,
14822, 14853, 14883, 14914, 14944, 14610, 14641, 14669, 14700,
14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14610,
14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
14914, 14944, 14610, 14641, 14669, 14700, 14730, 14761, 14791,
14822, 14853, 14883, 14914, 14944, 14610, 14641, 14669, 14700,
14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14610,
14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
14914, 14944, 14610, 14641, 14669, 14700, 14730, 14761, 14791,
14822, 14853, 14883, 14914, 14944, 14610, 14641, 14669, 14700,
14730, 14761, 14791, 14822, 14853, 14883, 14914, 14944, 14610,
14641, 14669, 14700, 14730, 14761, 14791, 14822, 14853, 14883,
14914, 14944, 14610, 14641, 14669, 14700, 14730, 14761, 14791,
14822, 14853, 14883, 14914, 14944), class = "Date")), .Names = c("Months",
"station", "temp_davg_c", "temp_dmax_c", "temp_dmin_c", "rain_mm",
"snow_cm", "precip_mm", "date"), row.names = c(NA, -168L), class = "data.frame")
You can separate the dplyr part of your code from the gg-plotting part:
correct_order <- c("Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec")
weather_data <-
dtt %>%
group_by(Months) %>%
summarise(Rain = mean(rain_mm,na.rm = TRUE),Snow = mean(snow_cm,na.rm = TRUE)) %>%
gather(Legend, Precipitation, -Months)
ggplot(weather_data, aes(x = Months, y = Precipitation, fill = Legend)) +
scale_x_discrete(limits=correct_order) +
geom_col()
As it has been mentioned before, fill= instead of color= is probably what you are looking for:

Random effects model in R - error

I am running econometric model with panel data in R. I am using plm package and pooled model and fixed effects model works great. But I get this error when trying to do random effects model and I don't know how to fix it.
There is my whole dataset and code:
auto <- structure(list(Country = structure(c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 10L,
10L, 10L, 10L, 11L, 11L, 11L, 11L), .Label = c("Bahrain", "Cuba",
"China", "Kuwait", "Lao PDR", "Qatar", "Saudi Arabia", "Swaziland",
"Syria", "United Arab Emirates", "Vietnam"), class = "factor"),
Year = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L,
3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L,
1L, 2L, 3L, 4L), .Label = c("1971", "1981", "1991", "2001"
), class = "factor"), AVG_GR_. = c(2.44, -2.93, 1.77, -1.04,
3.17, 3.5, -1.59, 5.13, 4.29, 7.51, 9.42, 9.83, -7.39, -5.52,
10.72, -0.14, 1.77, 3.38, 3.68, 5.33, -1.55, -5.72, 4.64,
1.5, 6.06, -5.25, 0.54, 2.28, 6.99, 2.82, 0.82, 1.12, 6.72,
-2, 3.09, 2.15, -1.06, -4.88, 0.2, -6.04, 1.61, 3.21, 5.88,
6.24), GDP_PC = c(17444.65, 19550.76, 15970.05, 18212.71,
2067.93, 3127.98, 3221.25, 3081.73, 153.5, 231.14, 491.26,
1207.52, 70184.35, 23911.92, 9559.35, 27681.03, 162.06, 212.46,
261.98, 386.38, 72617.74, 55370.39, 31970, 51090.02, 13752.55,
21124.79, 12891.51, 12446.49, 881.75, 1595.82, 1995.8, 2191.36,
738.63, 1349.2, 1057.84, 1380.2, 88377.72, 75348.77, 43306.13,
45038.43, 164.15, 194.45, 267.17, 481.92), POP_. = c(5.39,
3.26, 3.03, 6.49, 1.22, 0.75, 0.5, 0.13, 1.91, 1.71, 0.95,
0.6, 6.22, 4.16, -0.66, 4.61, 1.93, 2.7, 2.42, 1.73, 7.44,
7.9, 2.23, 11.57, 5.43, 5.12, 2.2, 3.08, 3.07, 3.64, 2.12,
1.16, 3.45, 3.35, 2.77, 2.78, 15.96, 5.94, 5.3, 10.95, 2.29,
2.3, 1.62, 0.97), CONSUMP_. = c(64.21, 52.81, 51.47, 40.51,
54.58, 54.96, 62.74, 54.02, 51.72, 51.01, 45.63, 39, 27.44,
48.61, 49.76, 35.74, 90.19, 90.65, 89.15, 70.38, 21.33, 26.27,
26.84, 16.81, 22.96, 46.85, 44.2, 31.61, 54.77, 74.9, 80.42,
79.36, 67.09, 69.71, 69.92, 61.26, 15.28, 33.07, 46.79, 59.97,
90, 89.89, 73.9, 65.33), GOV_CON_. = c(11.1, 19.55, 19.21,
14.27, 31.67, 31.66, 29.47, 34.91, 12.99, 14.11, 14.53, 14.1,
12.04, 23.7, 48.98, 18.45, 8.05, 8.29, 7.21, 8.96, 20.47,
36.49, 31.09, 14.5, 16.02, 30.12, 26.94, 22.53, 19.07, 17.11,
17.65, 14.76, 19.93, 19.6, 12.75, 12.67, 10.87, 19.27, 16.99,
7.66, 6.73, 6.85, 7.46, 6.19), CAP_FORM_. = c(34.15, 32.51,
24.24, 26.56, 25.94, 25.49, 10.76, 10.7, 34.57, 35.19, 37.79,
42.21, 13.55, 18.68, 17.9, 17.28, 7.57, 10.24, 16.68, 30.28,
22.49, 18.37, 26.13, 36.58, 22.59, 22.7, 20.49, 23.68, 30.77,
21.42, 17.65, 14.55, 25.34, 20.68, 22.53, 23.48, 29.93, 26.28,
27.29, 22.63, 14.45, 14.46, 25.22, 36.44), NAT_RES_. = c(27.42,
20.18, 17.52, 23.34, 1.81, 1.87, 2.5, 3.42, 41.09, 38.83,
40.09, 17.91, 66.53, 41.25, 35.94, 48.41, 5.28, 4.2, 3.01,
10.15, 63.5, 40.84, 39.7, 54.17, 57.89, 31.24, 32.74, 42.77,
6.47, 3.64, 2.25, 1.32, 9.55, 9.14, 14.19, 22.92, 51.04,
37.08, 27.99, 31.36, 3.95, 4.17, 8.39, 13.57), TRADE = c(1.69,
1.48, 1.37, 1.34, 0.77, 0.76, 0.33, 0.34, 0.11, 0.21, 0.35,
0.58, 1.03, 0.99, 1.09, 0.9, 0.15, 0.23, 0.63, 0.57, 0.95,
0.82, 0.85, 0.91, 0.89, 0.76, 0.66, 0.8, 1.47, 1.54, 1.42,
1.62, 0.51, 0.44, 0.66, 0.71, 1.1, 0.97, 1.37, 1.23, 0.62,
0.62, 0.86, 1.43), INFL_. = c(13.26, 3.24, 1.64, 5.65, 5.22,
0.11, 5.49, 2.44, 1.17, 5.72, 6.85, 4.2, 31.52, -0.47, 3.25,
7.29, 43.86, 56.9, 32.37, 7.95, 20.84, -1.59, 3.18, 8.65,
26.67, -1.16, 2.4, 5.73, 10.71, 11.36, 10.97, 8.04, 11.62,
17.43, 6.74, 6.78, 28.31, 1.25, 2.03, 6.94, 7.05, 156.6,
18.99, 9.45), LIFE_EXP = c(67.39, 71.47, 73.66, 75.55, 72.28,
74.46, 75.6, 77.81, 65.7, 68.43, 70.64, 73.99, 68.17, 71.25,
72.92, 73.79, 47.79, 51.39, 58.38, 64.68, 71.16, 74.31, 76.18,
77.53, 58.65, 66.77, 71.16, 74.03, 51.33, 57.45, 54.96, 46.81,
63.01, 68.42, 72.03, 74.56, 65.49, 70.19, 73.24, 75.66, 62.69,
69.09, 72.28, 74.66), EDU_T = c(0.68, 1.59, 2.63, 3.14, 0.75,
1.46, 2.81, 3.84, 0.37, 0.62, 1.08, 1.71, 1.41, 2.71, 3.53,
3.54, 0.16, 0.35, 0.65, 1, 1.61, 2.11, 2.5, 3.06, 1.06, 1.44,
2.13, 2.66, 0.35, 0.74, 1.07, 0.91, 0.34, 0.74, 1.27, 1.3,
1.14, 1.65, 2.61, 3.85, 0.67, 1.21, 0.67, 1.54)), .Names = c("Country",
"Year", "AVG_GR_.", "GDP_PC", "POP_.", "CONSUMP_.", "GOV_CON_.",
"CAP_FORM_.", "NAT_RES_.", "TRADE", "INFL_.", "LIFE_EXP", "EDU_T"
), row.names = c(1L, 2L, 3L, 4L, 9L, 10L, 11L, 12L, 5L, 6L, 7L,
8L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L), class = c("plm.dim", "data.frame"
))
Y <- cbind(auto$AVG_GR_.)
X <- cbind(auto$GDP_PC, auto$POP_., auto$CONSUMP_., auto$GOV_CON_.,
auto$CAP_FORM_., auto$NAT_RES_., auto$TRADE, auto$INFL_.,
auto$LIFE_EXP, auto$EDU_T)
pdata <- plm.data(auto, c("Country", "Year"))
random <- plm(Y~X, data=pdata, model="random")
Everything is OK until the last row. I get this error:
Error in if (sigma2$id < 0) stop(paste("the estimated variance of the", :
missing value where TRUE/FALSE needed
Thanks for your help :)
I am looking for help, but solved your problem. The first column has row. Names automatically filled in. You need to delete first column.
This worked:
> pdata <- pdata[,2:13];
> random <- plm(Y~X, data=pdata, model="random")
Just replace last row of your code with the above two lines.

Resources