combine facet_grid (ggplot2) with denscomp (fitdistrplus) - r

First off, I am an R newbie. I am trying to apply density plots to various groups within my data. Using fitdistrplus, I have created a single distribution density plot for all of my data.
plot(my_data, pch=20)
plotdist(my_data$Capture_Rate, histo = TRUE, demp = TRUE)
fit_w <- fitdist(my_data$Capture_Rate, "weibull")
fit_g <- fitdist(my_data$Capture_Rate, "gamma")
fit_ln <- fitdist(my_data$Capture_Rate, "lnorm")
par(mfrow=c(2,2))
plot.legend <- c("Weibull", "lognormal", "gamma")
denscomp(list(fit_w, fit_ln, fit_g), legendtext = plot.legend)
Using facet_grid in ggplot, I have created a grid of histograms for each grouping of my data.
df_data <- data.frame(my_data)
cdat <- ddply(df_data, c("sYear", "Season"), summarise, Capture_Rate.mean=mean(Capture_Rate))
ggplot(df_data, aes(x=Capture_Rate, fill=sYear))+
geom_histogram(binwidth = .025,
alpha = .5,
position = "identity")+
#geom_density(alpha=.2, fill="#FF6666")+
geom_vline(data=cdat, aes(xintercept=Capture_Rate.mean),
color="red", linetype="dashed", size=1)+
facet_grid(Season ~ sYear)
What I am looking for is to combine the two results where I get a density plot for each histogram in my grouping grid. Thank you for the assistance.
Sample Data:
a <- dput(my_data)
structure(list(Schedule_Name = 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, 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, 1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, 17683, 17684,
17685, 17686, 17687, 17688, 17689, 17690, 17691, 17692, 17693,
17694, 17696, 17697, 17698, 17699, 17700, 17701, 17702, 17703,
17704, 17705, 17706, 17707, 17708, 17710, 17711, 17712, 17713,
17714, 17715, 17716, 17717, 17718, 17719, 17720, 17721, 17722,
17723, 17724, 17725, 17728, 17729, 17730, 17731, 17732, 17733,
17734, 17735, 17736, 17737, 17738, 17739, 17740, 17741, 17742,
17743, 17744, 17745, 17746, 17747, 17748, 17749, 17750, 17751,
17753, 17754, 17755, 17758, 17759, 17761, 17762, 17763, 17764,
17765, 17766, 17767, 17768, 17769, 17770, 17771, 17772, 17773,
17774, 17775, 17776, 17777, 17778, 17779, 17781, 17782, 17783,
17784, 17785, 17786, 17787, 17788, 17789, 17790, 17791, 17792,
17793, 17794, 17795, 17796, 17797, 17798, 17799, 17800, 17801,
17802, 17803, 17804, 17805, 17806, 17807, 17808, 17809, 17810,
17811, 17812, 17813, 17814, 17815, 17816, 17817, 17818, 17819,
17820, 17821, 17822, 17823, 17824, 17825, 17826, 17827, 17828,
17829, 17830, 17831, 17832, 17833, 17834, 17835, 17836, 17837,
17838, 17839, 17840, 17841, 17842, 17843, 17844, 17845, 17846,
17847, 17848, 17849, 17850, 17851, 17852, 17853, 17854, 17855,
17856, 17857, 17858, 17859, 17860, 17861, 17862, 17863, 17864,
17865, 17866, 17867, 17868, 17869, 17870, 17871, 17872, 17873,
17874, 17875, 17876, 17877, 17878, 17879, 17880, 17881, 17882,
17883, 17884, 17885, 17886, 17887, 17888, 17889, 17890, 17891,
17892, 17893, 17894, 17895, 17896, 17897, 17898, 17899, 17900,
17901, 17902, 17903, 17904, 17905, 17906, 17907, 17908, 17909,
17910, 17911, 17912, 17913, 17914, 17915, 17916, 17917, 17918,
17919, 17920, 17921, 17922, 17923, 17924, 17925, 17926, 17927,
17928, 17929, 17930, 17931, 17932, 17933, 17934, 17935, 17936,
17937, 17938, 17939, 17940, 17941, 17942, 17943, 17944, 17945,
17946, 17947, 17948, 17949, 17950, 17951, 17952, 17953, 17954,
17955, 17956, 17957, 17958, 17959, 17960, 17961, 17962, 17963,
17964, 17965, 17966, 17967, 17968, 17969, 17970, 17971, 17972,
17973, 17974, 17975, 17976, 17977, 17978, 17979, 17980, 17981,
17982, 17983, 17984, 17985, 17986, 17987, 17988, 17989, 17990,
17991, 17992, 17993, 17994, 17995, 17996, 17997, 17998, 17999,
18000, 18001, 18002, 18003, 18004, 18005, 18006, 18007, 18008,
18009, 18010, 18011, 18012, 18013, 18014, 18015, 18016, 18017,
18018, 18019, 18020, 18021, 18022, 18023, 18024, 18025, 18026,
18027, 18028, 18029, 18030, 18031, 18032, 18033, 18034, 18035,
18036, 18037, 18038, 18039, 18040, 18041, 18042, 18043, 18044,
18045, 18046, 18047, 18048, 18049, 18050, 18051, 18052, 18053,
18054, 18055, 18056, 18057, 18058, 18059, 18060, 18061, 18062,
18063, 18064, 18065, 18066, 18067, 18068, 18069, 18070, 18071,
18072, 18073, 18074, 18075, 18076, 18077, 18078, 18079, 18080,
18081, 18082, 18083, 18084, 18085, 18086, 18087, 18088, 18089,
18090, 18091, 18092), class = "Date"), Active_Tails = 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, 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, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 8L, 10L,
10L, 10L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L,
12L, 13L, 13L, 14L, 14L, 14L, 14L, 15L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 17L, 18L, 18L, 19L, 19L, 19L, 20L, 21L, 21L,
21L, 22L, 22L, 23L, 24L, 25L, 26L, 26L, 26L, 26L, 25L, 26L,
26L, 27L, 27L, 28L, 28L, 28L, 28L, 28L, 29L, 30L, 30L, 31L,
32L, 33L, 33L, 34L, 34L, 34L, 35L, 35L, 36L, 36L, 36L, 37L,
37L, 37L, 37L, 38L, 40L, 41L, 41L, 41L, 41L, 41L, 41L, 41L,
41L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 43L, 45L, 46L,
46L, 46L, 46L, 46L, 46L, 47L, 48L, 48L, 49L, 49L, 49L, 49L,
50L, 51L, 51L, 52L, 52L, 52L, 52L, 53L, 53L, 54L, 55L, 55L,
55L, 55L, 56L, 56L, 56L, 58L, 58L, 58L, 58L, 60L, 59L, 59L,
60L, 60L, 60L, 60L, 61L, 62L, 63L, 63L, 63L, 63L, 65L, 65L,
65L, 66L, 66L, 66L, 66L, 66L, 66L, 66L, 67L, 67L, 67L, 67L,
67L, 68L, 68L, 68L, 68L, 69L, 69L, 69L, 69L, 69L, 69L, 69L,
69L, 69L, 69L, 69L, 69L, 69L, 69L, 69L, 69L, 69L, 69L, 69L,
69L, 69L, 69L, 69L, 69L, 69L, 69L, 70L, 70L, 70L, 69L, 70L,
70L, 71L, 71L, 70L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L,
71L, 71L, 70L, 70L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L,
71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L,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 14L, 22L, 19L, 21L,
23L, 16L, 19L, 23L, 16L, 22L, 17L, 17L, 15L, 22L, 21L, 16L,
19L, 19L, 18L, 14L, 23L, 23L, 25L, 17L, 15L, 22L, 21L, 17L,
19L, 17L, 20L, 23L, 22L, 22L, 22L, 19L, 19L, 25L, 22L, 25L,
25L, 21L, 22L, 24L, 24L, 22L, 20L, 26L, 22L, 22L, 26L, 25L,
24L, 27L, 27L, 26L, 24L, 28L, 23L, 27L, 25L, 25L, 27L, 27L,
23L, 28L, 23L, 23L, 29L, 32L, 23L, 19L, 30L, 27L, 30L, 29L,
25L, 29L, 26L, 24L, 30L, 30L, 33L, 24L, 31L, 30L, 28L, 28L,
29L, 35L, 33L, 30L, 33L, 35L, 37L, 32L, 32L, 36L, 30L, 31L,
33L, 33L, 31L, 33L, 33L, 37L, 33L, 33L, 38L, 37L, 37L, 38L,
34L, 36L, 38L, 28L, 35L, 30L, 33L, 38L, 39L, 30L, 34L, 32L,
28L, 37L, 33L, 36L, 39L, 33L, 36L, 34L, 39L, 28L, 39L, 39L,
32L, 30L, 35L, 33L, 37L, 25L, 32L, 30L, 28L, 39L, 36L, 33L,
38L, 40L, 37L, 33L, 35L, 43L, 30L, 32L, 40L, 36L, 30L, 31L,
41L, 29L, 31L, 38L, 41L, 34L, 35L, 42L, 34L, 33L, 40L, 33L,
31L, 38L, 37L, 29L, 33L, 35L, 38L, 34L, 33L, 36L, 39L, 33L,
33L, 31L, 33L, 36L, 33L, 38L, 33L, 30L, 28L, 30L, 28L, 37L,
34L, 33L, 33L, 34L, 35L, 31L, 38L, 30L, 35L, 30L, 45L, 35L,
31L, 30L, 26L, 26L, 35L, 34L, 26L, 34L, 36L, 31L, 31L), Capture_Rate = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.5, 0.5,
1, 1, 0.5, 1, 0.5, 1, 1, 1, 1, 1, 0.5, 0.5, 1, 1, 0.5, 1,
0.33, 1, 1, 0.75, 0.5, 1, 1, 0.25, 0.6, 0.4, 0.8, 0.8, 0.6,
0.6, 0.8, 0.4, 1, 1, 0.67, 0.67, 1, 1, 0.25, 0.4, 0.6, 0.4,
0.64, 0.82, 0.55, 0.36, 0.64, 0.25, 0.75, 0.5, 0.75, 0.58,
0.58, 0.62, 0.54, 0.36, 0.57, 0.71, 0.79, 0.6, 0.38, 0.5,
0.31, 0.44, 0.38, 0.56, 0.63, 0.47, 0.56, 0.39, 0.47, 0.58,
0.47, 0.5, 0.52, 0.38, 0.48, 0.5, 0.5, 0.39, 0.33, 0.36,
0.5, 0.5, 0.62, 0.58, 0.4, 0.5, 0.62, 0.44, 0.37, 0.5, 0.61,
0.43, 0.43, 0.46, 0.52, 0.6, 0.47, 0.77, 0.47, 0.61, 0.52,
0.5, 0.41, 0.65, 0.54, 0.6, 0.64, 0.44, 0.53, 0.62, 0.43,
0.59, 0.46, 0.45, 0.38, 0.54, 0.51, 0.39, 0.46, 0.46, 0.44,
0.34, 0.56, 0.53, 0.58, 0.4, 0.35, 0.51, 0.49, 0.4, 0.44,
0.4, 0.44, 0.5, 0.48, 0.48, 0.48, 0.41, 0.41, 0.53, 0.46,
0.52, 0.51, 0.43, 0.45, 0.49, 0.48, 0.43, 0.39, 0.5, 0.42,
0.42, 0.5, 0.47, 0.45, 0.5, 0.49, 0.47, 0.44, 0.51, 0.41,
0.48, 0.45, 0.43, 0.47, 0.47, 0.4, 0.47, 0.39, 0.39, 0.48,
0.53, 0.38, 0.32, 0.49, 0.44, 0.48, 0.46, 0.4, 0.46, 0.4,
0.37, 0.46, 0.45, 0.5, 0.36, 0.47, 0.45, 0.42, 0.42, 0.43,
0.52, 0.49, 0.45, 0.49, 0.51, 0.54, 0.47, 0.47, 0.52, 0.43,
0.45, 0.48, 0.48, 0.45, 0.48, 0.48, 0.54, 0.48, 0.48, 0.55,
0.54, 0.54, 0.55, 0.49, 0.52, 0.55, 0.41, 0.51, 0.43, 0.48,
0.55, 0.57, 0.43, 0.49, 0.46, 0.4, 0.53, 0.48, 0.51, 0.56,
0.46, 0.51, 0.49, 0.55, 0.39, 0.55, 0.55, 0.45, 0.42, 0.49,
0.46, 0.52, 0.35, 0.46, 0.43, 0.39, 0.55, 0.51, 0.46, 0.54,
0.56, 0.52, 0.46, 0.49, 0.61, 0.42, 0.45, 0.56, 0.51, 0.42,
0.44, 0.58, 0.41, 0.44, 0.54, 0.58, 0.48, 0.49, 0.59, 0.48,
0.46, 0.56, 0.46, 0.44, 0.54, 0.52, 0.41, 0.46, 0.49, 0.54,
0.48, 0.46, 0.51, 0.55, 0.46, 0.46, 0.44, 0.46, 0.51, 0.46,
0.54, 0.46, 0.42, 0.39, 0.42, 0.39, 0.52, 0.48, 0.46, 0.46,
0.48, 0.49, 0.44, 0.54, 0.42, 0.49, 0.42, 0.63, 0.49, 0.44,
0.42, 0.37, 0.37, 0.49, 0.48, 0.37, 0.48, 0.51, 0.44, 0.44
), Total_SPR_IML = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 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0.5, 0.62,
0.58, 0.4, 0.5, 0.62, 0.44, 0.37, 0.5, 0.61, 0.43, 0.43,
0.46, 0.52, 0.6, 0.47, 0.77, 0.47, 0.61, 0.52, 0.5, 0.41,
0.65, 0.54, 0.6, 0.64, 0.44, 0.53, 0.62, 0.43, 0.59, 0.46,
0.45, 0.38, 0.54, 0.51, 0.39, 0.46, 0.46, 0.44, 0.34, 0.56,
0.53, 0.58, 0.4, 0.35, 0.51, 0.49, 0.4, 0.44, 0.4, 0.44,
0.5, 0.48, 0.48, 0.48, 0.41, 0.41, 0.53, 0.46, 0.52, 0.51,
0.43, 0.45, 0.49, 0.48, 0.43, 0.39, 0.5, 0.42, 0.42, 0.5,
0.47, 0.45, 0.5, 0.49, 0.47, 0.44, 0.51, 0.41, 0.48, 0.45,
0.43, 0.47, 0.47, 0.4, 0.47, 0.39, 0.39, 0.48, 0.53, 0.38,
0.32, 0.49, 0.44, 0.48, 0.46, 0.4, 0.46, 0.4, 0.37, 0.46,
0.45, 0.5, 0.36, 0.47, 0.45, 0.42, 0.42, 0.43, 0.52, 0.49,
0.45, 0.49, 0.51, 0.54, 0.47, 0.47, 0.52, 0.43, 0.45, 0.48,
0.48, 0.45, 0.48, 0.48, 0.54, 0.48, 0.48, 0.55, 0.54, 0.54,
0.55, 0.49, 0.52, 0.55, 0.41, 0.51, 0.43, 0.48, 0.55, 0.57,
0.43, 0.49, 0.46, 0.4, 0.53, 0.48, 0.51, 0.56, 0.46, 0.51,
0.49, 0.55, 0.39, 0.55, 0.55, 0.45, 0.42, 0.49, 0.46, 0.52,
0.35, 0.46, 0.43, 0.39, 0.55, 0.51, 0.46, 0.54, 0.56, 0.52,
0.46, 0.49, 0.61, 0.42, 0.45, 0.56, 0.51, 0.42, 0.44, 0.58,
0.41, 0.44, 0.54, 0.58, 0.48, 0.49, 0.59, 0.48, 0.46, 0.56,
0.46, 0.44, 0.54, 0.52, 0.41, 0.46, 0.49, 0.54, 0.48, 0.46,
0.51, 0.55, 0.46,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 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, 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, 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, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L), .Label = c("2018-05",
"2018-06", "2018-07", "2018-08", "2018-09", "2018-10", "2018-11",
"2018-12", "2019-01", "2019-02", "2019-03", "2019-04", "2019-05",
"2019-06", "2019-07"), class = "factor"), Season = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L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er"), class = "factor")), row.names = c(NA, 418L
), class = "data.frame")

So, the solution for the empirical density is going to slightly easier than do the theoretical distributions. First, let's setup some dummy data, since we don't have any of yours to play around with.
set.seed(123)
# Setup some facets
idx <- expand.grid(c("A", "B"), c("C", "D"))
# For each facet, generate some numbers
df <- apply(idx, 1, function(x){
data.frame(row = x[[1]],
col = x[[2]],
# chose 10 as mean, since Weibull can't be negative
x = rnorm(100, 10))
})
df <- do.call(rbind, df)
Now for the empirical case, we can simply take the density in each facet. We can do this, because ggplot has included kernel density estimates as a stat function.
ggplot(df, aes(x)) +
geom_histogram(binwidth = 0.1) +
# To line up the histogram with KDE, we multiply y-values by binwidth
geom_line(aes(y = ..count..*0.1, colour = "empirical"), stat = "density") +
facet_grid(row ~ col)
Which looks like this:
Because we don't have any ggplot stat functions for the theoretical densities -at least not ones that are panel specific- we would have to pre-compute the xy-coordinates for the theoretical distributions in a separate data.frame:
# Loop over facets
dists <- apply(idx, 1, function(i){
# Grab data belonging to facet
dat <- df$x[df$row == i[[1]] & df$col == i[[2]]]
# Setup x-values
xseq <- seq(min(dat), max(dat), length.out = 100)
# Specify distributions of interest
dists <- c("weibull", "lnorm", "gamma")
# Loop over distributions
fits <- lapply(setNames(dists, dists), function(dist) {
# Estimate parameters
ests <- fitdist(dat, dist)$estimate
# Get y-values
y <- do.call(paste0("d", dist), c(list(x = xseq), as.list(ests)))
# Multiplied by length(dat) to match absolute counts
y * length(dat)
})
# Format everything neatly in a data.frame
out <- lapply(dists, function(j) {
data.frame(row = i[[1]],
col = i[[2]],
x = xseq,
y = fits[[j]],
distr = j)
})
# Combine all distributions
do.call(rbind, out)
})
# Combine all facets
dists <- do.call(rbind, dists)
Now that we've done that tedious work, we can finally plot it:
ggplot(df, aes(x)) +
geom_histogram(binwidth = 0.1) +
geom_line(data = dists, aes(y = y * 0.1, colour = distr)) +
facet_grid(row ~ col)
Adapt as necessary for your own data. Good luck!
EDIT: Now with example data
Assume df is the data.frame from which you've posted the dput() output. I've included a condition that checks if the length of the facet data is longer than 2 and wether the variance is non-zero, so as to skip data from which we wouldn't be able to make any estimates anyway. Furthermore, I've converted variable names to be compatible with how you named them in your data.frame.
idx <- expand.grid(levels(df$Season), levels(df$sYear))
# Loop over facets
dists <- apply(idx, 1, function(i){
dat <- df$Capture_Rate[df$Season == i[[1]] & df$sYear == i[[2]]]
print(length(dat))
if (length(dat) < 2 | var(dat) == 0) {
return(NULL)
}
xseq <- seq(min(dat), max(dat), length.out = 100)
dists <- c("weibull", "lnorm", "gamma")
fits <- lapply(setNames(dists, dists), function(dist) {
ests <- fitdist(dat, dist)$estimate
y <- do.call(paste0("d", dist), c(list(x = xseq), as.list(ests)))
y * length(dat)
})
out <- lapply(dists, function(j) {
data.frame(Season = i[[1]],
sYear = i[[2]],
x = xseq,
y = fits[[j]],
distr = j)
})
do.call(rbind, out)
})
dists <- do.call(rbind, dists)
ggplot(df, aes(x=Capture_Rate, fill=sYear))+
geom_histogram(binwidth = .025,
alpha = .5,
position = "identity") +
geom_line(data = dists, aes(x, y * .025, colour = distr), inherit.aes = FALSE) +
facet_grid(Season ~ sYear)

Related

How do I create a ggplot in R from a non-linear model using the mgcv package?

I have a non-linear survival model which I have coded using the mgcv package. I can produce a regular plot, but I would like to be able to do code a ggplot2 instead. How do I go about this?
Here is my code:
df <- structure(list(SurvYear =c(3L, 2L, 3L, 6L, 8L, 3L, 5L, 2L, 9L,
8L, 1L, 7L, 1L, 4L, 6L, 8L, 2L, 5L, 1L, 1L, 7L, 1L, 5L, 3L, 2L,
1L, 9L, 1L, 5L, 2L, 2L, 1L, 2L, 3L, 4L, 8L, 7L, 2L, 2L, 6L, 9L,
7L, 3L, 9L, 6L, 8L, 2L, 8L, 2L, 1L, 1L, 6L, 5L, 3L, 3L, 7L, 2L,
4L, 5L, 2L, 3L, 7L, 4L, 1L, 2L, 2L, 3L, 5L, 1L, 9L, 2L, 2L, 3L,
9L, 6L, 2L, 2L, 4L, 3L, 1L, 9L, 7L, 3L, 1L, 2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0L, 1L, 0L,
1L, 1L), Age = c(63L, 66L, 34L, 43L, 63L, 21L, 24L, 44L, 52L,
59L, 27L, 32L, 30L, 20L, 56L, 55L, 35L, 26L, 53L, 43L, 39L, 19L,
34L, 28L, 19L, 24L, 50L, 22L, 58L, 24L, 50L, 25L, 37L, 30L, 51L,
69L, 23L, 49L, 22L, 46L, 58L, 31L, 23L, 53L, 59L, 25L, 38L, 44L,
34L, 49L, 19L, 39L, 24L, 51L, 29L, 27L, 48L, 77L, 22L, 43L, 59L,
49L, 60L, 51L, 49L, 47L, 50L, 44L, 41L, 44L, 50L, 42L, 46L, 54L,
35L, 21L, 26L, 26L, 40L, 21L, 48L, 49L, 20L, 20L, 32L, 37L, 22L,
36L, 46L, 28L, 39L, 35L, 51L, 39L, 49L, 57L, 46L, 18L, 52L, 47L,
27L, 32L, 23L, 43L, 42L, 57L, 22L, 40L, 19L, 58L, 71L, 55L, 42L,
20L, 51L, 21L, 20L, 61L, 36L, 54L, 19L, 35L, 38L, 41L, 34L, 22L,
41L, 42L, 56L, 50L, 53L, 53L, 48L, 22L, 59L, 27L, 28L, 32L, 37L,
68L, 24L, 26L, 61L, 21L, 20L, 20L, 50L, 62L, 61L, 29L, 18L, 40L,
67L, 43L, 25L, 43L, 22L, 56L, 47L, 41L, 40L, 43L, 27L, 37L, 61L,
35L, 23L, 54L, 38L, 38L, 39L, 45L, 49L, 63L, 49L, 44L, 44L, 23L,
37L, 58L, 61L, 25L, 18L, 59L, 25L, 51L, 40L, 27L, 42L, 22L, 38L,
22L, 45L, 33L, 32L, 36L, 53L, 52L, 19L, 45L, 53L, 27L, 65L, 25L,
53L, 57L, 29L, 23L, 62L, 36L, 56L, 59L, 41L, 61L, 44L, 24L, 21L,
38L, 29L, 55L, 33L, 18L, 21L, 19L, 65L, 24L, 59L, 34L, 25L, 45L,
48L, 18L, 41L, 61L, 32L, 37L, 21L, 20L, 57L, 25L, 65L, 50L, 61L,
32L, 27L, 19L, 50L, 63L, 19L, 45L, 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0L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L,
0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L,
0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L,
1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L,
1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L,
0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L,
1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 0L,
1L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L,
1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L)), class = "data.frame",
row.names = c(NA, -300L))
Here is the script:
library(mgcv)
library(ggplot2)
#Run the model
Model1 <- gam(SurvYear~
(Gender)+
s(Age, k=50)+
s(Highest_Educationmx, k=7),
weights=Censor, data=df, gamma=1.5, family=cox.ph())
summary(Model1)
#Build a perspective chart
vis.gam(Model1, view=c("Age","Highest_Educationmx"),
plot.type="persp", color="gray", se=-1, theta=45, phi=25,
xlab="Age", ylab= "Highest Education",
ticktype="detailed", zlim=c(-5.00, 2.00))
#Plot individual predictors using plot command from mgcv
plot(Model1, all.terms=T, rug=T, residuals=F, se=T, shade=T, seWithMean=T)
#Plot individual predictors using ggplot instead of plot command from mgcv
#UNSURE HOW DO TO THIS
I'm biased (I wrote it) but you can use the gratia package for this.
You can use the draw() function as a replacement for plot.gam(), and if you want total control, just use evaluate_smooth() to produce a tidy representation of the smooth which is then easily plotted using ggplot2.
Here is the script based on the suggestion from Gavin Simpson above:
library(gratia)
#Plot individual predictors using ggplot instead of the plot command from mgcv
sm <- gratia::evaluate_smooth(Model1, "Age")
ggplot(sm, aes(x=Age, y=est)) + geom_line(size=1.0) +
geom_ribbon(aes(ymax=est+se, ymin=est-se), alpha=0.20) +
coord_cartesian(xlim=c(20.00, 75.00), ylim=c(-2.00, 1.00)) +
scale_x_continuous(breaks=seq(20.00, 75.00, 5.00)) +
scale_y_continuous(breaks=seq(-2.00, 1.00, 1.00)) +
labs(title="Age") +
xlab("Age") +
ylab("Linear Risk Score") +
theme(plot.title=element_text(size=10)) +
geom_hline(yintercept=0, linetype="dashed", size=0.5) +
geom_vline(xintercept=mean(df$Age), linetype="dashed", size=0.5)

reshape data frame and concatenating columns in R

I tried to reshape my data frame from wide to long format. At the moment the data frame looks like this:
structure(list(study_site = structure(c(5L, 5L, 5L, 5L, 5L, 5L,
5L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 4L, 4L, 1L, 2L, 3L, 1L, 4L, 1L,
4L, 3L, 3L, 3L, 1L, 3L, 5L, 4L, 4L, 4L, 3L, 3L, 5L, 5L, 4L, 4L,
4L, 1L, 4L, 3L, 5L, 5L, 5L, 1L, 3L, 5L, 3L, 3L, 3L, 5L, 5L, 3L,
4L, 2L), .Label = c("N", "no_nest", "O", "S", "W"), class = "factor"),
coords.N = structure(c(54L, 54L, 40L, 40L, 40L, 40L, 39L,
67L, 67L, 55L, 55L, 64L, 64L, 64L, 78L, 81L, 47L, 80L, 83L,
60L, 46L, 46L, 76L, 88L, 88L, 88L, 84L, 84L, 30L, 58L, 58L,
58L, 25L, 25L, 19L, 19L, 42L, 42L, 42L, 29L, 45L, 90L, 91L,
91L, 91L, 91L, 89L, 89L, 87L, 87L, 87L, 56L, 56L, 61L, 35L,
36L), .Label = c("40.40463", "48.40168", "48.40178", "48.40215",
"48.40235", "48.40309", "48.40390", "48.40393", "48.40396",
"48.40405", "48.40410", "48.40411", "48.40415", "48.40416",
"48.40424", "48.40425", "48.40430", "48.40435", "48.40436 ",
"48.40438", "48.40443", "48.40450", "48.40451", "48.40454",
"48.40455", "48.40459", "48.40460", "48.40461", "48.40466",
"48.40466 ", "48.40467", "48.40469", "48.40471", "48.40477",
"48.40479 ", "48.40481", "48.40482", "48.40483", "48.40488 ",
"48.40491", "48.40493", "48.40504 ", "48.40508", "48.40513",
"48.40515", "48.40519 ", "48.40522 ", "48.40523", "48.40525",
"48.40526", "48.40529", "48.40532", "48.40537", "48.40537 ",
"48.40538 ", "48.40543 ", "48.40549", "48.40549 ", "48.40557",
"48.40557 ", "48.40558", "48.40565", "48.40571", "48.40575",
"48.40580", "48.40584", "48.40586 ", "48.40591", "48.40596",
"48.40598", "48.40599", "48.40611", "48.40612", "48.40617",
"48.40626", "48.40632 ", "48.40633", "48.40635 ", "48.40636",
"48.40637", "48.40638 ", "48.40639", "48.40639 ", "48.40641 ",
"48.40652", "48.40655", "48.40656 ", "48.40657 ", "48.40687 ",
"48.40690 ", "48.40703", "48.40718", "48.40719", "48.40726",
"48.40742", "48.40748", "NO_DATA"), class = "factor"), coords.E = structure(c(67L,
67L, 49L, 49L, 49L, 49L, 27L, 67L, 67L, 70L, 70L, 68L, 68L,
68L, 87L, 94L, 68L, 83L, 90L, 73L, 52L, 52L, 2L, 95L, 95L,
95L, 93L, 93L, 32L, 69L, 69L, 69L, 55L, 55L, 24L, 24L, 29L,
29L, 29L, 30L, 48L, 85L, 1L, 1L, 1L, 1L, 78L, 78L, 79L, 79L,
79L, 64L, 64L, 63L, 66L, 45L), .Label = c(" 015.82024", " 015.82164",
"015.80237", "015.80263", "015.80309", "015.80341", "015.80369",
"015.80388", "015.80394", "015.80399", "015.80406", "015.80435",
"015.80436", "015.80466", "015.80512", "015.80517", "015.80548",
"015.80551", "015.80572", "015.80583", "015.80609", "015.80636",
"015.80659", "015.80703", "015.80723", "015.80779", "015.80795",
"015.80803", "015.80821", "015.80843", "015.80871", "015.80875",
"015.80888", "015.80897", "015.80901", "015.80903", "015.80905",
"015.80906", "015.80908", "015.80909", "015.80921", "015.80923",
"015.80929", "015.80939", "015.80993", "015.81007", "015.81018",
"015.81087", "015.81113", "015.81132", "015.81151", "015.81180",
"015.81241", "015.81273", "015.81305", "015.81406", "015.81422",
"015.81522", "015.81526", "015.81543", "015.81546", "015.81564",
"015.81628", "015.81632", "015.81678", "015.81682", "015.81700",
"015.81703", "015.81735", "015.81739", "015.81770", "015.81783",
"015.81784", "015.81800", "015.81849", "015.81992", "015.82012",
"015.82029", "015.82039", "015.82083", "015.82099", "015.82126",
"015.82180", "015.82230", "015.82232", "015.82255", "015.82265",
"015.82290", "015.82303", "015.82304", "015.82346", "015.82362",
"015.82376", "015.82398", "015.82451", "015.82500", "015.82519",
"015.82555", "015.82579", "015.82634", "NO_DATA"), class = "factor"),
study_ID = c(120L, 120L, 1L, 1L, 1L, 1L, 9L, 39L, 39L, 109L,
109L, 110L, 110L, 110L, 45L, 58L, 121L, 96L, 97L, 40L, 43L,
43L, 47L, 57L, 57L, 57L, 114L, 114L, 67L, 71L, 71L, 71L,
83L, 83L, 4L, 4L, 10L, 10L, 10L, 106L, 108L, 46L, 115L, 115L,
115L, 115L, 116L, 116L, 117L, 117L, 117L, 70L, 70L, 119L,
95L, 3L), species = 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, 2L), .Label = c("barn swallow", "no_nest"), class = "factor"),
first_visit = c(1L, 2L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 5L, 0L,
1L, 0L, 2L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L,
1L, 0L, 1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L, 0L, 0L, 0L), used_3rd_visit = c(0L, 0L, 1L, 0L, 0L, 0L,
1L, 0L, 1L, 0L, 4L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L,
1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 2L, 0L, 1L, 0L, 2L,
0L, 0L, 6L, 1L, 0L, 1L, 0L, 0L, 2L, 0L, 0L, 1L, 0L, 0L, 2L,
0L, 1L, 0L, 0L, 0L), nest_condition = structure(c(3L, 5L,
5L, 2L, 5L, 5L, 5L, 5L, 3L, 5L, 5L, 5L, 5L, 2L, 5L, 3L, 2L,
4L, 5L, 5L, 5L, 5L, 3L, 2L, 5L, 5L, 2L, 2L, 5L, 1L, 5L, 5L,
5L, 5L, 5L, 5L, 3L, 5L, 5L, 5L, 2L, 3L, 5L, 5L, 5L, 2L, 5L,
3L, 5L, 5L, 5L, 2L, 5L, 3L, 5L, 4L), .Label = c(" ready ",
"damaged", "in_progress", "no_nest", "ready"), class = "factor"),
nesting_site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 3L, 1L,
1L, 1L, 1L, 3L, 3L, 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), .Label = c("inside", "no_nest", "outside"), class = "factor"),
distance = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 3L, 4L, 3L, 6L, 4L, 4L, 2L, 2L, 4L, 2L,
2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 2L, 4L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
4L, 6L), .Label = c("1", "2", "3", "4", "no_data", "no_nest"
), class = "factor"), material = structure(c(5L, 5L, 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, 5L, 5L, 1L, 1L, 2L), .Label = c("fine", "fine plaster",
"medium fine plaster", "no_data", "rough", "rough plaster",
"smooth plaster", "under construction", "wood"), class = "factor"),
housetype = structure(c(4L, 4L, 4L, 4L, 4L, 4L, 5L, 4L, 4L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L,
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 5L, 5L, 5L,
4L, 4L, 5L, 3L, 3L, 3L, 3L, 5L, 5L, 3L, 3L, 3L, 4L, 4L, 5L,
5L, 4L), .Label = c("auto repair shop", "barn ", "hall",
"residence", "stable"), class = "factor"), usage_house = structure(c(5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 3L), .Label = c("auto_repair",
"barn", "inhabited", "under construction", "used"), class = "factor"),
age = c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L)), row.names = c(1L,
2L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 21L, 22L, 23L, 24L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 40L, 41L, 89L, 90L, 91L, 92L, 93L, 94L,
95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L,
106L, 107L, 108L, 109L, 111L), class = "data.frame")
used_1st..2nd... means that the birds have used this certain number of nests at the first, second,... control.
I would like to have that each row in my data frame to always represent a used/ unused nest as well as no_nest:
ID species `1st_visit` `2nd_visit` `3rd_visit` used_1st_visit used_2nd_visit used_3rd_visit
<dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 120 barn s~ 1 1 0 0 0 0
2 120 barn s~ 1 1 0 1 1 0
3 120 barn s~ 1 1 0 1 1 0
4 39 barn s~ 1 1 1 1 1 1
5 8 barn s~ 1 1 1 1 0 0
6 8 barn s~ 1 1 1 0 0 0
Unfortunately I have no idea how to concatenate the columns to get the final data frame.
Does anybody has an idea?
I'm not completely sure what you are asking for, but this is what I understood: In the long data frame...
if all visits (coulmns used_first_visit, used_sec_visit etc.) are 0, combine them to one row, marking it 0
if any visits are not 0, keep as many rows as there are non-zero visits and mark them with 1
This is my dplyr-solution (it's not very pretty, but it works):
# create data
dat <- data.frame("visits" = c("first", "first", "second", "second", "third", "third"), "study_id" = rep(120, 6), "used_first_visit" = c(0, 2, 0, 2, 0, 2), "used_sec_visit" = c(0, 2, 0, 2, 0, 2), "used_thrd_visit" = rep(0, 6), "nest_cond" = c("damaged", "ready", "damaged", "ready", "damaged", "ready"))
# make long data frame and filter values
dat_long <- dat %>%
pivot_longer(c(3:5),names_to = "whatever", values_to = "used") %>% # make long data frame
select(-c(whatever)) %>% # get rid of name column
group_by(visits, nest_cond) %>% # group data
mutate(used = ifelse(all(used == 0) & row_number() == 1, 10, used)) %>% # if the whole group is 0, mark one row for later filtering
filter(used > 0 ) %>% # filter
mutate(used = ifelse(used == 10, 0, 1)) # change to correct numbers
Let me know if this is not what you are looking for!

ggplot2 select categories for bar chart and create labels

I am trying make bar chart with ggplot2 with the dataset below. When I use the code
ggplot(p.data, aes(x = `Period Number`, y = `Total Jumps`)) +
stat_summary(data = subset(p.data, Status = "Starter"), fun ="mean", geom = "bar")
I get this graph:
The most concerning aspect is the for period 2, 3, 4, and 5 the bars should be taller (period 2 should be around 9.9). Additionally, I would like to remove period 0 and period 1 and add bar labels with the raw data and without creating an additional data frame.
p.data <- structure(list(`Period Number` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L),
`Total Jumps` = c(112L, 97L, 28L, 132L, 162L, 19L, 92L, 112L,
97L, 141L, 68L, 86L, 76L, 26L, 105L, 125L, 19L, 92L, 112L,
64L, 101L, 68L, 4L, 8L, 0L, 8L, 12L, 0L, 0L, 0L, 13L, 8L,
0L, 8L, 2L, 2L, 5L, 12L, 0L, 0L, 0L, 5L, 11L, 0L, 0L, 6L,
0L, 9L, 8L, 0L, 0L, 0L, 7L, 10L, 0L, 14L, 5L, 0L, 5L, 5L,
0L, 0L, 0L, 8L, 11L, 0L, 108L, 131L, 47L, 136L, 159L, 35L,
114L, 116L, 111L, 190L, 64L, 75L, 95L, 47L, 116L, 123L, 27L,
103L, 108L, 70L, 152L, 64L, 4L, 7L, 0L, 14L, 10L, 0L, 0L,
0L, 15L, 10L, 0L, 4L, 0L, 0L, 3L, 7L, 7L, 8L, 8L, 5L, 10L,
0L, 7L, 14L, 0L, 3L, 10L, 1L, 0L, 0L, 11L, 7L, 0L, 18L, 15L,
0L, 0L, 9L, 0L, 3L, 0L, 10L, 11L, 0L, 118L, 96L, 48L, 143L,
170L, 37L, 118L, 117L, 116L, 165L, 56L, 80L, 68L, 48L, 114L,
130L, 36L, 114L, 107L, 80L, 123L, 56L, 2L, 10L, 0L, 8L, 11L,
0L, 0L, 0L, 5L, 9L, 0L, 4L, 12L, 0L, 6L, 5L, 0L, 4L, 8L,
12L, 8L, 0L, 7L, 4L, 0L, 10L, 10L, 0L, 0L, 0L, 12L, 13L,
0L, 25L, 2L, 0L, 5L, 14L, 1L, 0L, 2L, 7L, 12L, 0L), Status = structure(c(1L,
2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L,
1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L,
1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L,
1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L,
1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L,
1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L,
1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 1L), .Label = c("Bench", "Starter"), class = "factor")), row.names = c(NA,
198L), class = "data.frame")
Thank you for your help!
It's best to pass that data you actually want to plot to the plotting function, rather than trying to coerce it within the plotting function. In this case you were trying to subset a different data frame from the one you passed to ggplot inside stat_summary. The call to ggplot had already set up the aesthetics you wanted mapped, then in your only geom layer, you were telling ggplot you wanted a completely different set of aesthetics.
You don't need to create another data frame to reshape your data. Here's how you could do it using dplyr:
library(dplyr)
library(ggplot2)
p.data %>%
filter(Status == "Starter") %>%
group_by(`Period Number`) %>%
summarise(`Total Jumps` = mean(`Total Jumps`)) %>%
filter(`Period Number` > 1) %>%
ggplot(aes(x = `Period Number`, y = `Total Jumps`)) +
geom_col(fill = "dodgerblue", colour = "black") +
geom_text(aes(y = `Total Jumps` + 1, label = signif(`Total Jumps`, 2)))

Linear function for condition1 and cubic function for condition2 in one plot

I have data of participants that had numerous trials, where certain trials had one condition, and other trials were another.
My analyses show that for condition 1, there is a linear null effect (flat line), while for condition 2 there is a cubic effect. I want to plot them together.
The code below creates a plot that gives the cubic function for both groups:
ggplot(dat, aes(x=trial, y=y, group=condition, colour=condition)) +
geom_point() + geom_jitter(height=0.2) +
geom_smooth(alpha=0.1, method="lm", formula = y ~ poly(x,3, raw=TRUE)) +
labs(x="Trial", y="y") +
scale_x_discrete(breaks=c(1,9,18,27,36,45,54,63))
What I want is to not have the cubic function for condition 2, but have a linear function. I tried to force this through aes() calls within geom_smooth(), but this seems to give me a much flatter cubic function for condition 1:
ggplot(dat, aes(x=trial, y=y)) +
geom_point(aes(group=condition, colour=condition)) + geom_jitter(height=0.2, aes(group=condition, colour=condition)) +
geom_smooth(alpha=0.1, method="lm", formula = y ~ poly(x,3, raw=TRUE), aes(group=(condition="1"), colour=(condition="1"))) +
geom_smooth(alpha=0.1, method="lm", aes(group=(condition="2"), colour=(condition="2"))) +
labs(x="Trial", y="y") +
scale_x_discrete(breaks=c(1,9,18,27,36,45,54,63))
Obviously this is not the way to go. How would I accomplish this? Script for reproducible example (first 250 lines of the total dataset, so your figures will be different) below:
structure(list(id = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 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 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L,
32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L,
45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L,
58L, 59L, 60L, 61L, 62L, 63L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L,
22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L,
35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L,
48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L,
61L), condition = 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, 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,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, 1L, 1L, 0L,
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, 0L, 1L, 0L, 0L, 1L,
0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, NA, 0L, 0L, 1L, 0L, 0L, 1L,
1L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, NA, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, NA,
0L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, NA, 1L, NA, NA, 1L, 1L,
1L, 1L, NA, 1L, 1L, 1L, 1L, NA, 1L, 0L, 1L, 1L, 1L, 0L, 1L,
0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L,
1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L)), .Names = c("id",
"trial", "condition", "y"), row.names = c(NA, 250L), class = "data.frame")
Edit: The reason I'm not using geom_smooth() using gam or loess, is because there are multiple polynomials in condition 1, so it will show more than just the cubic function if I use that solution. I wish to show the cubic function, not the composite of multiple polynomials.
You could filter your data inside geom_smooth.
library(tidyverse)
ggplot(dat, aes(x=trial, y=y, colour=as.factor(condition))) +
geom_point() + geom_jitter(height=0.2) +
geom_smooth(data = filter(dat, condition == 2), alpha=0.1, method="lm", formula = y ~ poly(x,3, raw=TRUE)) +
geom_smooth(data = filter(dat, condition == 1), alpha=0.1, method="lm", formula = y ~ 1) +
labs(x="Trial", y="y") +
scale_x_continuous(breaks=c(1,9,18,27,36,45,54,63))
Which gives you this plot

Stat_function range truncated

I am trying to use ggplot2 to plot some data and fitted non-linear curves. I want to use stat_function with nls objects that I have already defined, but the result produces a truncated curve. I have looked at the following pages but so far I haven't found a solution:
R - ggplot2 extrapolated regression lines in linear region
Plotting a large number of custom functions in ggplot in R using stat_function()
http://docs.ggplot2.org/0.9.3/stat_function.html
Equivalent of curve() for ggplot
I am not very experienced with ggplot2, so I apologize if I am missing something simple and I'm grateful for any help. Here my example data:
df=structure(list(Spp = 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, 1L), .Label = c("Dugentia", "Eugenia",
"Faramea", "Licania", "Mouriri"), class = "factor"), Tx = 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, 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), .Label = c("Control",
"Drought"), class = "factor"), no. = structure(c(1L, 3L, 4L,
5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L,
6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L, 1L, 3L, 4L, 5L, 6L,
5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L,
5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L, 5L, 6L, 7L, 8L), .Label = c("1",
"101", "2", "3", "4", "5", "6", "7", "7A", "9"), class = "factor"),
Fv.Fm = c(0.74, 0.702, 0.797, 0.782, 0.769, 0.759, 0.701,
0.805, 0.79, 0.775, 0.763, 0.725, 0.8, 0.786, 0.774, 0.759,
0.664, 0.791, 0.776, 0.758, 0.729, 0.592, 0.757, 0.722, 0.681,
0.66, 0.084, 0.652, 0.633, 0.63, 0.569, 0.259, 0.424, 0.376,
0.432, 0.771, 0.696, 0.685, 0.761, 0.782, 0.772, 0.736, 0.775,
0.784, 0.755, 0.707, 0.746, 0.777, 0.765, 0.705, 0.744, 0.706,
0.55, 0.582, 0.635, 0.615, 0.384, 0.504, 0.513, 0.584, 0.378,
0.328, 0.302), Temp. = c(27L, 27L, 27L, 27L, 27L, 30L, 30L,
30L, 30L, 30L, 35L, 35L, 35L, 35L, 35L, 40L, 40L, 40L, 40L,
40L, 45L, 45L, 45L, 45L, 45L, 48L, 48L, 48L, 48L, 48L, 50L,
50L, 50L, 50L, 50L, 27L, 27L, 27L, 27L, 30L, 30L, 30L, 30L,
35L, 35L, 35L, 35L, 40L, 40L, 40L, 40L, 45L, 45L, 45L, 45L,
48L, 48L, 48L, 48L, 50L, 50L, 50L, 50L)), .Names = c("Spp",
"Tx", "no.", "Fv.Fm", "Temp."), class = "data.frame", row.names = c(NA,
63L))
Here is what I have so far for making the plot with ggplot2:
library(ggplot2)
f1 = ggplot(data = df, aes(x = Temp., y = Fv.Fm, group = Tx) )
f2<-f1+
geom_point(aes(shape=Tx, fill=Tx), size=4)
f3 <- f2 + scale_x_continuous("Temperature (°C)", limits=c(25,55)) +
scale_y_continuous("Fv/Fm", limits = c(0, 1)) +
scale_shape_manual(values=c(24,21)) +
scale_fill_manual(values=c("#4D4D4D","#E6E6E6")) +
theme_bw()
d4 <- f3 + theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x = element_text(size = 11),
axis.text.y = element_text(size = 11),
legend.title = element_blank(),
legend.text = element_text(size=12))
All of that works well, but then when I make an nls object and try to use it with stat_function I get a curve that is truncated. I would like to extend the curve down to the x-axis. There is also an error generated.
my.nls<-nls(Fv.Fm~a*(-exp(Temp.)+b), data = df, start=list(a=1*10^-20, b=5*10^21))
new<-d4+stat_function(fun=function(x){coef(my.nls)[1]*(-exp(x)+coef(my.nls)[2])})
new
What I want is something more like the following using basic r functionality, but with all the bells and whistles offered by ggplot2, and ultimately one nls curve for each treatment (Tx).
x<-seq(27, 55, length.out = 200)
y <- predict(my.nls,list(Temp. = x))
plot(df$Temp., df$Fv.Fm)
lines(x,y)
Try this:
df.new=data.frame(x=seq(20,60,0.1),
y=coef(my.nls)[1]*(-exp(seq(20,60,0.1))+coef(my.nls)[2]))
d4+geom_line(aes(Temp.,y,group=NULL),data=df.new)+
xlim(20,60)+ylim(-.1,1)
The problem is that stat_function(...) evaluates the function for x in the range given in your default dataset, df. Since max(df$temp.) = 50, the function is only evaluated out to x=50.
Not a great fit by the way. I'd definitely be looking for a different model.

Resources