Stat_function range truncated - r

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.

Related

combine facet_grid (ggplot2) with denscomp (fitdistrplus)

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, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 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 = "38K", class = "factor"), sDate = structure(c(17664,
17665, 17666, 17667, 17668, 17669, 17670, 17672, 17674, 17675,
17676, 17677, 17678, 17679, 17680, 17681, 17682, 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,
71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L,
71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L,
71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L,
71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L, 71L,
71L, 71L, 71L, 71L, 71L, 71L, 71L), MX_Credits = 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, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 2L, 1L, 3L, 4L, 3L, 2L, 4L, 4L, 1L, 3L, 2L, 4L,
4L, 3L, 3L, 4L, 2L, 5L, 5L, 4L, 4L, 6L, 7L, 2L, 4L, 6L, 4L,
7L, 9L, 6L, 4L, 7L, 3L, 9L, 6L, 9L, 7L, 7L, 8L, 7L, 5L, 8L,
10L, 11L, 9L, 6L, 8L, 5L, 7L, 6L, 9L, 10L, 8L, 10L, 7L, 9L,
11L, 9L, 10L, 11L, 8L, 10L, 11L, 11L, 9L, 8L, 9L, 13L, 13L,
16L, 15L, 10L, 13L, 16L, 12L, 10L, 14L, 17L, 12L, 12L, 13L,
15L, 18L, 14L, 24L, 15L, 20L, 17L, 17L, 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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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), sYear = 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, 1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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, 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, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 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, 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, 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, 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, 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, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,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L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 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, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L), .Label = c("2018-0.Winter 1H", "2018-2.Spring",
"2018-3.Summer", "2018-4.Fall", "2019-1.Winter 2H", "2019-2.Spring",
"2019-3.Summer"), 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)

Label an axis generated by axis() function

I'm plotting a dual axis graph in r base and I can't add a label to the secondary axis. Here's what I'm working with:
dados = structure(list(IDADE = c(65L, 35L, 65L, 42L, 50L, 44L, 0L, 58L,
22L, 27L, 34L, 31L, 0L, 24L, 34L, 20L, 4L, 34L, 20L, 27L, 8L,
2L, 20L, 31L, 45L, 26L, 26L, 40L, 50L, 34L), ESTADO = c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L), TIPINT = c(3L,
1L, 3L, 2L, 3L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L,
3L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 1L, 3L, 2L, 3L, 1L), DIARIAS = c(2L,
2L, 4L, 1L, 1L, 1L, 10L, 1L, 2L, 2L, 2L, 3L, 3L, 1L, 2L, 3L,
6L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 6L, 1L, 1L, 2L)), .Names = c("IDADE",
"ESTADO", "TIPINT", "DIARIAS"), row.names = c(NA, 30L), class = "data.frame")
#Define boxplot base
bx = boxplot(dados$IDADE~dados$TIPINT,axes=F, xlab=NA,ylab=NA,col=heat.colors(3,0.6))
#Posiciona eixos
par(mar = c(5,5,2,5))
#Plota bar
barplot(table(dados$TIPINT),col=heat.colors(3,0.4),names=c("Tipo 1","Tipo 2","Tipo 3"),
ylab = "Quantidade de pacientes")
#Plota box
par(new=T)
bx2 = bxp(bx,boxwex = 0.50, at = c(0.9, 2,3.1),axes=F, xlab=NA, ylab="l",boxfill=heat.colors(3,0.7))
axis(side=4)
Which gets me the following graph
I'm trying to label the right axis, but axis(side=4,labels="labels") is for something else and axis(side=4,ylab="label") doesn't work.
Thanks
You can use the function mtext to add a label to a secondary axis, using the line=2 argument to move it the appropriate distance from the axis.
mtext("label", 4, line=2)
Alternatively you could use just text to have finer control, if say, you want to reorient the label. You can pull the plot dimensions from par("usr") and adjust accordingly.
p <- par("usr")
text(p[2]+0.4, mean(p[3:4]), labels = "label", xpd=NA, srt = -90)

Newbie attempting linear mixed effects model in R studio - TOTAL FAIL

After searching over an hour (this forum, Youtube, class notes, google) I've found no help for my question. I'm a complete newb who knows nothing about R or stats.
I'm attempting to create a linear mixed effects model in R. I'm measuring leaf width across three different locations (Jacksonville FL, Augusta GA, & Atlanta GA), and within those three locations there is a high-nitrogen and low-nitrogen plot. I have 150 leaf measurements from 50 trees.
My limited understanding tells me that the leaf width is the continuous response variable, and city and plot are the discrete explanatory variables. The random effect would be the individual trees, since the leaf width within a single tree is non-independent.
I've used "nlme" to make a model:
leaf.width.model <- lme(width ~ city*plot, (1|tree.id), data=leaf)
I then ran an ANOVA test, and it suggested there's something going on with city and the interaction between city and plot. This is where I'm stuck. I want to make a plot that has lines for all three cities, but I haven't a clue how to do that. When I try to use the plot function, I just get a boxplot.
I've literally tried for hours and am more lost and confused than before.
1) How can I make this graph?
2) What other tests should I do to analyze and/or visualize this data?
I am forever grateful for any help at all. I really want to learn R and stats very badly, but I'm getting discouraged.
Thank you,
Rich
P.S Here is the output of the dput function:
> dput(tree) structure(list(tree.id = structure(c(24L, 24L, 32L, 25L, 25L, 24L, 24L, 32L, 25L, 25L, 43L, 45L, 45L, 43L, 23L, 23L, 45L, 45L, 23L, 23L, 41L, 41L, 38L, 11L, 11L, 38L, 41L, 41L, 11L, 11L, 14L, 14L, 29L, 13L, 13L, 14L, 14L, 29L, 13L, 13L, 4L, 4L, 1L, 1L, 20L, 1L, 1L, 20L, 6L, 8L, 8L, 5L, 5L, 6L, 4L, 4L, 8L, 8L, 5L, 5L, 9L, 9L, 10L, 10L, 12L, 12L, 13L, 13L, 22L, 22L, 23L, 23L, 24L, 24L, 25L, 25L, 25L, 25L, 40L, 40L, 41L, 41L, 38L, 38L, 39L, 39L, 14L, 14L, 14L, 15L, 15L, 28L, 28L, 29L, 29L, 35L, 35L, 36L, 36L, 37L, 37L, 42L, 42L, 43L, 43L, 44L, 44L, 45L, 45L, 46L, 46L, 47L, 47L, 2L, 1L, 3L, 3L, 4L, 4L, 7L, 11L, 11L, 16L, 16L, 20L, 20L, 21L, 21L, 17L, 17L, 18L, 18L, 19L, 19L, 26L, 26L, 27L, 27L, 30L, 30L, 31L, 31L, 32L, 32L, 33L, 33L, 34L, 34L, 48L), .Label = c("Tree_112", "Tree_112 ", "Tree_115", "Tree_130", "Tree_137", "Tree_139", "Tree_140", "Tree_141", "Tree_153", "Tree_154", "Tree_156", "Tree_159", "Tree_166", "Tree_169", "Tree_171", "Tree_180", "Tree_182", "Tree_184", "Tree_185", "Tree_202", "Tree_213", "Tree_218", "Tree_222", "Tree_227", "Tree_239", "Tree_242", "Tree_246", "Tree_247", "Tree_252", "Tree_260", "Tree_267", "Tree_269", "Tree_271", "Tree_272", "Tree_291", "Tree_293", "Tree_298", "Tree_327", "Tree_329", "Tree_336", "Tree_350", "Tree_401", "Tree_403", "Tree_405", "Tree_407", "Tree_409", "Tree_420", "Tree_851"), class = "factor"), city = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Atlanta", "Augusta", "Jacksonville"), class = "factor"), plot = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 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, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("High-N", "Low-N"), class = "factor"), width = c(0.66, 0.716, 0.682, 0.645, 0.645, 0.696, 0.733,
0.707, 0.668, 0.686, 0.617, 0.733, 0.73, 0.615, 0.669, 0.746, 0.687, 0.682, 0.76, 0.713, 0.651, 0.664, 0.679, 0.729, 0.756,
0.669, 0.647, 0.713, 0.767, 0.685, 0.69, 0.731, 0.781, 0.729,
0.725, 0.739, 0.769, 0.791, 0.676, 0.688, 0.719, 0.753, 0.748,
0.791, 0.785, 0.78, 0.723, 0.756, 0.664, 0.645, 0.653, 0.615,
0.591, 0.642, 0.693, 0.716, 0.694, 0.676, 0.662, 0.629, 0.665,
0.748, 0.726, 0.693, 0.715, 0.714, 0.764, 0.732, 0.61, 0.721,
0.703, 0.713, 0.746, 0.752, 0.662, 0.733, 0.707, 0.674, 0.734,
0.79, 0.732, 0.794, 0.703, 0.712, 0.737, 0.731, 0.747, 0.746,
0.787, 0.709, 0.716, 0.764, 0.77, 0.764, 0.802, 0.663, 0.777,
0.642, 0.779, 0.81, 0.724, 0.645, 0.68, 0.637, 0.695, 0.768,
0.761, 0.7, 0.759, 0.726, 0.696, 0.794, 0.774, 0.799, 0.747,
0.606, 0.691, 0.733, 0.707, 0.698, 0.706, 0.72, 0.694, 0.697,
0.737, 0.716, 0.73, 0.706, 0.667, 0.734, 0.528, 0.695, 0.684,
0.763, 0.733, 0.809, 0.6, 0.676, 0.718, 0.759, 0.609, 0.665,
0.667, 0.647, 0.701, 0.663, 0.688, 0.693, 0.899)), .Names = c("tree.id", "city", "plot", "width"), class = "data.frame", row.names = c(NA, -149L))
Thank you all so much for your comments, I sincerely appreciate everyone's help!
As suggested in comments, a line plot might not make sense for your data, as you are studying how width varies in discrete categories (in separate cities and separate plots). Boxplots would make sense as you can make them for each of the interactions of city and plot. To give you a sense of what you can do I generated some fake data and made an example of the sort of plot that might be helpful to you:
# fake data
leaf <- data.frame(tree.id = rep(1:50, each = 3),
city = rep(c("Jackson", "Augusta", "Atlanta"), each = 50),
plot = rep(1:6, each = 25))
# I'll make the average of width different for each plot
leaf$width <- rnorm(nrow(leaf), leaf$plot, 1)
# plotting the data
library(ggplot2) # this is a great library for plotting in R
ggplot(leaf, aes(x = factor(plot), y = width, color = factor(plot))) +
facet_grid(~city, scales = 'free_x') + # This creates a subplot for each city
geom_boxplot() +
geom_point(position = "jitter") +
theme_bw()
In this plot I added the points (the leaf widths for each individual tree) but I 'jittered' them, meaning perturbing their position slightly so that they do not pile up on top of each other and are all visible. You could remove this if you liked.
Exploratory data analysis should be fun! And I think visualization is a good place to start when beginning in statistics. Hopefully this will prove helpful to you.
leaf.width.model <- lme(width ~ city*plot, (1|tree.id), data=leaf)
In this model if you want to plot something, you are probably trying to answer:
How much is the average leaf width for all trees in each city for each type of plot.
To show this information in a figure, you need to plot width on y axis plot plot(high and low nitrogen) on x axis and group the data by city. Then you will get the 3 lines you are taking about. However, you need to get the average width in each group as you only want to show city variation.
To get this plot from raw data: (Using fake data provided by gfgm)
set.seed(100)
leaf <- data.frame(tree.id = rep(1:50, each = 3),
city = rep(c("Jackson", "Augusta", "Atlanta"), each = 50),
plot = rep(c(1, 0), each = 25))
# I'll make the average of width different for each plot
leaf$width <- rnorm(nrow(leaf), leaf$plot, 1)
library(plotly)
library(tidyverse)
leaf %>%
group_by(city,plot) %>%
summarise(avwidth = mean(width, na.rm=T),
avsd = 1.96*sd(width, na.rm=T)/sqrt(25)) %>%
plot_ly(x = ~plot, y = ~avwidth, color= ~city,
type="scatter", mode="markers+lines",
error_y = ~list(array=avsd)
)

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, 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, 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, 5L,
5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L
), trial = c(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, 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, 2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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

plot area truncated when using geom_dotplot

consider the following example data:
ex = structure(list(group = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L,
2L, 3L, 4L, 6L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L, 1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 6L,
1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L,
5L, 6L, 1L, 2L, 1L, 2L, 3L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 1L, 1L, 1L,
4L), .Label = c("A", "B", "C", "D", "E", "F"), class = "factor"),
ID = structure(c(35L, 35L, 35L, 35L, 35L, 35L, 1L, 1L, 1L,
1L, 1L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 9L, 9L,
9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L,
13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 21L, 21L, 22L, 22L,
22L, 22L, 2L, 3L, 4L, 5L, 8L, 15L, 16L, 17L, 18L, 19L, 19L,
20L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 30L, 31L, 32L,
33L, 34L), .Label = c("10", "107", "108", "109", "124", "17",
"18", "187", "19", "21", "24", "26", "27", "28", "335", "336",
"339", "340", "341", "342", "38", "39", "576", "577", "578",
"579", "580", "581", "582", "583", "584", "585", "586", "592",
"6"), class = "factor"), value = c(1L, 7L, 4L, 4L, 3L, 9L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 5L, 1L, 2L, 1L, 2L, 2L,
2L, 1L, 33L, 27L, 28L, 21L, 28L, 1L, 3L, 1L, 1L, 1L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 3L, 3L, 2L, 4L, 1L, 1L, 1L, 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, 1L, 1L, 1L, 1L)), class = "data.frame",
row.names = c(NA, -88L), .Names = c("group", "ID", "value")
)
Note that in group A, value = 1 for every ID. I use ggplot2 to create dot plot based on counts of the value variable using geom_dotplot and faceting by group:
ggplot(ex) + aes(x = value) +
geom_dotplot(binwidth = 1, method = "histodot") +
facet_wrap(~ group)
The dot stack in the first facet is cut off, even when exported using ggsave. Changing the y-axis limits has no effect, but changing the aspect ratio so that H >= W seems to fix the issue (usually by adding way more space to the top than necessary). Is this a bug, or am I specifying my dot plot incorrectly?
EDIT
One workaround is to flip my dotplot and bin by the y variable:
ggplot(ex) + aes(x = group, y = value) +
geom_dotplot(binwidth = 1, method = "histodot",
binaxis = "y", stackdir = "centerwhole") +
facet_wrap(~ group, scales = "free_x")
Two other parameters that can help you are stackratio and dotsize. For example:
ggplot(ex) + aes(x = value) +
geom_dotplot(binwidth = 1, method = "histodot", stackratio = 0.9, dotsize = .75) +
facet_wrap(~ group) +
scale_y_continuous(NULL, breaks = NULL)
You would need to tweak the numbers until you got the layout you wanted.
I found an interesting workaround using geom_bar that achieves the same structure as a dot plot but with rectangles:
ggplot(ex) + aes(x = value, group = ID) +
geom_bar(color = "black", fill = "white", width = 1) +
facet_wrap(~ group)
Although it results in rectangles (rather than dots) and you can't control the stack spacing. The rectangles get resized according to the plot window, which would be equivalent to tweaking the dot size in geom_dotplot. Also, it begs the question "why not just use a regular bar plot?"

Resources