How to generate borders in the ggplot - r

I have a data set that look like this. There are 5 plots that are spatialy distributed. I want to draw a distribution map that will show the variation of a variable over the experiment. I use geom_tile to do that.
ggplot(aes(x = x, y = y), data = check) + geom_tile(aes(fill = HJD_6))
Is there any way to generate the borders around each plot. I used manualy geom_vline and geom_hline but there are problems whan I have bigger design that is not as regular as in the example.
check <- structure(list(Yta = c(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, 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, 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, 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, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5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L, 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, 8L, 8L, 8L, 8L), x = c(1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20), y = c(31, 31, 31, 31, 31, 31,
31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33,
33, 33, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34, 34, 34, 34, 34,
34, 34, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36,
36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39,
39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 31, 31,
31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 34, 34, 34, 34,
34, 34, 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35,
36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, 37, 37, 37,
37, 37, 37, 37, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 39, 39,
39, 39, 39, 39, 39, 39, 39, 39, 40, 40, 40, 40, 40, 40, 40, 40,
40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 42, 42, 42, 42,
42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43, 43, 43, 43, 43,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45,
45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 47, 47,
47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48, 48, 48, 48, 48,
48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 50, 50, 50, 50,
50, 50, 50, 50, 50, 50, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43, 43, 43, 43,
43, 43, 43, 43, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46, 46, 46, 46, 46, 46,
46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48, 48,
48, 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 53, 53,
53, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54, 54, 54, 54, 54, 54,
54, 54, 55, 55, 55, 55, 55, 55, 55, 55, 55, 55, 56, 56, 56, 56,
56, 56, 56, 56, 56, 56, 57, 57, 57, 57, 57, 57, 57, 57, 57, 57,
58, 58, 58, 58, 58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(136L, NA,
NA, 170L, 133L, 55L, NA, 105L, 120L, 130L, 85L, 95L, NA, NA,
185L, 200L, 85L, 153L, 82L, 80L, 80L, NA, 110L, 130L, 222L, 150L,
NA, 70L, 90L, 172L, NA, 177L, NA, 97L, 65L, 133L, 62L, 52L, 95L,
190L, 154L, 55L, NA, NA, 180L, 130L, 90L, NA, NA, NA, NA, NA,
NA, NA, 148L, NA, NA, NA, 244L, 158L, NA, 164L, NA, NA, 224L,
NA, NA, 139L, 140L, NA, NA, 155L, 135L, 76L, 80L, 130L, NA, NA,
145L, NA, 75L, NA, 105L, 70L, 95L, NA, 95L, 115L, 140L, NA, NA,
NA, 135L, NA, 75L, 98L, 132L, 100L, 105L, 112L, NA, NA, 125L,
105L, 87L, 79L, NA, NA, NA, 165L, NA, 110L, 110L, 133L, 75L,
52L, 117L, 70L, 155L, 130L, 180L, 187L, 110L, 90L, 60L, 120L,
195L, 90L, 100L, 88L, NA, 90L, NA, 112L, 130L, 155L, 152L, 130L,
73L, 122L, 142L, 130L, 150L, 108L, NA, 86L, 125L, 90L, 119L,
125L, 206L, 100L, 95L, 40L, 160L, 222L, NA, 100L, 112L, NA, NA,
NA, 105L, 150L, 185L, NA, NA, 163L, 135L, 115L, NA, 155L, 183L,
NA, 126L, 122L, 150L, 140L, 134L, 80L, 213L, 152L, 63L, 75L,
70L, NA, 115L, 98L, 106L, 130L, NA, 123L, NA, 114L, 65L, 144L,
115L, 60L, NA, 100L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 174L,
50L, 102L, 153L, NA, NA, 85L, 132L, 85L, NA, 72L, 177L, 115L,
141L, 157L, 77L, 70L, NA, 115L, 90L, NA, NA, NA, 40L, NA, 115L,
145L, 100L, 70L, 80L, 151L, 120L, NA, 55L, 200L, NA, 120L, 170L,
185L, NA, NA, NA, 120L, 60L, NA, NA, 95L, 172L, 60L, 155L, NA,
191L, 85L, NA, 65L, 115L, 115L, 175L, 30L, 66L, 195L, 161L, 132L,
NA, 80L, 75L, 115L, NA, NA, 115L, 95L, 151L, 140L, 114L, 140L,
165L, 124L, 168L, 90L, 50L, 160L, NA, 81L, 142L, 135L, 42L, 160L,
NA, 130L, 50L, 172L, 94L, 120L, NA, 140L, NA, 145L, 120L, NA,
NA, 170L, 187L, NA, 141L, 200L, 102L, NA, NA, 136L, NA, NA, 121L,
NA, 60L, 175L, 140L, 175L, 195L, NA, 216L, 77L, 231L, 175L, 210L,
180L, 175L, 260L, NA, 160L, 172L, NA, 135L, 122L, 193L, 115L,
175L, 60L, 85L, 202L, 164L, 159L, 95L, 169L, 190L, 80L, 80L,
120L, NA, 115L, 130L, 172L, 155L, 75L, 72L, 170L, NA, NA, 65L,
75L, NA, NA, 190L, NA, NA, NA, NA, NA, NA, NA, 75L, NA, NA, 90L,
NA, 190L, NA, NA, NA, NA, 52L, NA, NA, NA, 90L, NA, NA, NA, NA,
NA, NA, 93L, 130L, 109L, NA, NA, NA, 100L, NA, NA, NA, NA, NA,
NA, 150L, NA, 202L, 161L, NA, NA, 120L, 50L, NA, 164L, NA, NA,
120L, NA, 138L, NA, NA, 154L, 60L, 57L, 195L, 130L, 75L, NA,
NA, NA, 54L, 95L, 59L, 65L, 52L, 72L, NA, NA, NA, NA, NA, NA,
67L, NA, NA, NA, 168L, NA, NA, NA, 100L, 120L, NA, 195L, 40L,
NA, NA, NA, NA, NA)), row.names = c(NA, -500L), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("Yta", "x", "y", "RAD", "PLANTA",
"HJD_6"))

One idea is to find the convex hull and plot the geom_polygon. Drawing on How to draw neat polygons around scatterplot regions in ggplot2:
library(plyr)
find_hull <- function(df) df[chull(df$x, df$y), ]
hulls <- ddply(check, "Yta", find_hull)
ggplot(aes(x = x, y = y), data = d) +
geom_tile(aes(fill = HJD_6), colour = "white") +
geom_polygon(data = hulls, aes(x = x, y = y, group = Yta),
colour = "red", alpha = 0)
You can modify hulls to make the border prettier, but this solution is quite specific to this example:
hulls <- ddply(hulls, "Yta", function(df) {
df$x <- df$x + ifelse(df$x < mean(df$x), -.5, .5)
df$y <- df$y + ifelse(df$y < mean(df$y), -.5, .5)
df
})

Related

How to add trace to plotly scatter plot

p <- plot_ly(data = bData, x = ~`Maturity Date`, y = ~YVal, type = 'scatter', mode='markers',
symbol = ~Sym, symbols = c('circle-open','x-open','diamond-open','square-open') ,
text = ~paste(bData$Security,bData$Crncy, bData$YTM, bData$DM,sep = "<br>") ,hoverinfo = 'text'
)
Above code produces this plot.
Now to this chart I want to add a trace with scatter plot with color depending on Currency column.
I tried this but it produces combination of two field as the legend.
Basically I want to classify the plot based on currency type but also add overlay or trace based on column SYM as the symbol.
p <- plot_ly(data = bData, x = ~`Maturity Date`, y = ~YVal, type = 'scatter', mode='markers',
symbol = ~Sym, symbols = c('circle-open','x-open','diamond-open','square-open') ,
text = ~paste(bData$Security,bData$Crncy, bData$YTM, bData$DM,sep = "<br>") ,hoverinfo = 'text'
) %>%
add_trace(x = ~`Maturity Date`, y = ~YVal , color=~Crncy)
data:
bData <- structure(list(Crncy = structure(c(9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 3L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 5L, 9L, 9L, 9L, 9L, 9L, 9L,
5L, 9L, 9L, 9L, 9L, 6L, 5L, 9L, 9L, 3L, 9L, 5L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 5L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 7L, 6L, 7L, 6L, 9L,
7L, 7L, 3L, 2L, 7L, 9L, 9L, 9L, 9L, 8L, 9L, 9L, 9L, 10L, 9L,
9L, 4L, 4L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 4L, 9L, 9L,
9L, 5L, 9L, 9L, 9L, 9L, 5L, 9L, 5L, 9L, 2L, 9L, 5L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 2L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 1L, 5L, 1L, 9L, 9L, 9L,
9L, 9L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 6L, 9L, 9L,
9L, 9L, 1L, 1L, 9L, 9L, 9L, 9L, 9L, 1L, 9L, 9L), .Label = c("AUD",
"CAD", "CHF", "COP", "EUR", "GBP", "JPY", "PEN", "USD", "ZAR"
), class = "factor"), `Maturity Date` = structure(c(20772, 19689,
18969, 18969, 20815, 20119, 20865, 20864, 20134, 20873, 20873,
20887, 20011, 20897, 20162, 19797, 20908, 20908, 20923, 19841,
19107, 19107, 20941, 20935, 20936, 20936, 20953, 20049, 19138,
19860, 21005, 21027, 19562, 19562, 21014, 19222, 21047, 19950,
19264, 19285, 19292, 19292, 19323, 19382, 19381, 20000, 19404,
20176, 19437, 19875, 19875, 19508, 20635, 19555, 19555, 20658,
19038, 19628, 18946, 19745, 19746, 19021, 19042, 19042, 20545,
20623, 19047, 19412, 19415, 20178, 20178, 19611, 19807, 20168,
20551, 20640, 20957, 20223, 19858, 19692, 19158, 20258, 19720,
20269, 20999, 20999, 20290, 20278, 20300, 20300, 21029, 19753,
20318, 20328, 20423, 20120, 20223, 20240, 19335, 20594, 19510,
19905, 20073, 20347, 20392, 18897, 20962, 20994, 21009, 21043,
19287, 19505, 18899, 19006, 19081, 19323, 19373, 19203, 19417,
19415, 19430, 19469, 19492, 19527, 19599, 20344, 19638, 19655,
19675, 19688, 20068, 19711, 19780, 19803, 19838, 19865, 19892,
19890, 19940, 19962, 20706, 20011, 18927, 20041, 18949, 20777,
20116, 20145, 19041, 20156, 20177, 20174, 20173, 20205, 20208,
20235, 20248, 20249, 19523, 20521, 20588, 20574, 20465, 20482,
19400, 20588, 21021, 20649, 20389, 20409, 19950, 19600, 19601,
20346, 19658, 20747, 19657, 19656, 19657, 20307, 20347, 19259,
20087, 20810, 20077, 19349, 20118, 20483, 20112, 20109, 19392,
19594, 20144, 21056, 19407, 20749, 20573, 19296, 19300, 19300,
19310, 20041, 19346, 20907, 19976, 20744, 20202, 19132, 19132,
19132), class = "Date"), Sym = structure(c(4L, 3L, 4L, 1L, 2L,
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 4L, 1L, 4L, 3L, 2L, 1L, 4L, 1L, 2L, 1L, 2L,
1L, 2L, 3L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 4L, 3L, 2L,
1L, 4L, 1L, 4L, 1L, 2L, 1L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 1L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 2L, 1L, 2L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L,
3L, 4L, 3L, 4L, 3L, 2L, 1L, 2L, 3L, 4L, 3L, 4L, 3L, 2L, 3L, 4L,
3L, 4L, 1L, 2L, 1L, 2L, 1L, 2L, 3L, 4L, 4L, 4L, 4L), .Label = c("Axe",
"Axe, Owned", "None", "Owned"), class = "factor"), YVal = c(20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113,
114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126,
127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139,
140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152,
153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165,
166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178,
179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204,
205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217,
218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229)), class = "data.frame", row.names = c(NA,
-210L))
Maybe is this what you are looking for? (I have used split from plotly):
library(plotly)
#Code
plot_ly(data = bData, x = ~`Maturity Date`, y = ~YVal, type = 'scatter', mode='markers',
symbol = ~Sym, symbols = c('circle-open','x-open','diamond-open','square-open') ,
split = ~Crncy,
text = ~paste(bData$Security,bData$Crncy, bData$YTM, bData$DM,sep = "<br>") ,
hoverinfo = 'text')
Output:
Update: Here somo other options for OP:
#Option 1
plot_ly(data = bData, x = ~`Maturity Date`, y = ~YVal, type = 'scatter', mode='markers',
symbol = ~Sym, symbols = c('circle-open','x-open','diamond-open','square-open') ,
text = ~paste(bData$Security,bData$Crncy, bData$YTM, bData$DM,sep = "<br>") ,
hoverinfo = 'text',legendgroup = 'group1'
) %>%
add_trace(x = ~`Maturity Date`, y = ~YVal , symbol=~Crncy,legendgroup = 'group2')
Output:
Option 2:
#Option 2
plot_ly(bData, x = ~`Maturity Date`, y = ~YVal, type = 'scatter', mode='markers',
legendgroup = 'group1',color = ~Sym) %>%
add_trace(y = ~YVal, legendgroup = 'group2',type = 'scatter', mode='markers',
color=~Crncy)
Output:

how ggplot2 gradientn set custom colors and values?

I want to create a heatmap-like plot for my data with custom colors and values. I read some blogs and QAs on StackOverflow, I found that scale_colour_gradientn is the best way to do this. However, I tried my best but I cannot align the colors and the values and I still don't know why.
It seems there is something wrong in my key code:
sc <- scale_colour_gradientn(
colors = c("blue", "white", "yellow", "red"),
values = scales::rescale(c(
data$measure %>% min(na.rm = TRUE),
0,
60,
data$measure %>% max(na.rm = TRUE))
))
The following is my reproducible code.
library(dplyr)
library(ggplot2)
data <- structure(list(signature = structure(c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("expo1", "expo2",
"expo3", "expo4", "expo5", "expo6", "expo7", "expo8"
), class = "factor"), feature = structure(c(1L, 2L, 9L, 10L,
1L, 2L, 11L, 4L, 6L, 8L, 9L, 10L, 11L, 4L, 8L, 9L, 10L, 1L, 2L,
4L, 6L, 10L, 1L, 2L, 11L, 4L, 6L, 8L, 9L, 10L, 1L, 2L, 11L, 3L,
4L, 6L, 8L, 7L, 9L, 10L, 1L, 2L, 11L, 3L, 4L, 5L, 6L, 8L, 7L,
9L, 10L, 1L, 2L, 11L, 4L, 5L, 6L, 8L, 7L, 9L, 10L), .Label = c("Cell_Cycle",
"Hippo", "MYC", "NOTCH", "NRF2", "PI3K", "TGF-Beta", "RTK-RAS",
"TP53_pathway", "WNT", "HR_pathway"), class = "factor"), count = c(923,
923, 923, 923, 923, 923, 923, 923, 923, 923, 923, 923, 923, 923,
923, 923, 923, 923, 923, 923, 923, 923, 923, 923, 923, 923, 923,
923, 923, 923, 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
959, 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, 959,
959, 959, 959, 959, 959, 959, 959, 959), measure = c(3.35, 2.58,
4.5, 1.58, 25.7, 17.2, 12.1, 12.8, 4.93, 15, 16.8, 14.1, 7.83,
7.9, 8.62, 23.5, 8.74, 10.9, 5.44, 7.49, 9, 8.04, -3.51, -2.66,
-2.8, -2.85, -2, -2.48, -4.68, -3, 18.9, 18.6, 27, 39.4, 23.6,
13.3, 18.2, 35.1, 11.3, 18, 173, 60.8, 114, 323, 67.5, 531, 78.5,
56.7, 297, 39, 59.8, 144, 55, 73.4, 59.2, 262, 67.1, 51.2, 197,
25.9, 53.8), p = c(0.0228148148148148, 0.0379866666666667, 3.21391304347826e-07,
0.0083330612244898, 0.000378888888888889, 5.78285714285714e-06,
0.0270175438596491, 7.65333333333333e-05, 0.00438044444444444,
1.8392e-05, 5.56285714285714e-11, 0.000377142857142857, 0.0416918032786885,
0.00926588235294118, 0.000426461538461538, 1.6368e-17, 0.00550468085106383,
0.0359457627118644, 0.0286758620689655, 0.00438044444444444,
0.00421581395348837, 0.00760833333333333, 0.000426461538461538,
5.85655172413793e-06, 0.000426461538461538, 2.948e-06, 0.00156933333333333,
5.24740740740741e-06, 1.0956e-19, 1.1484e-07, 3.20774193548387e-05,
4.54892307692308e-11, 2.832e-11, 0.0106276923076923, 4.95e-15,
0.000106117647058824, 4.60533333333333e-12, 0.0240428571428571,
1.21691428571429e-12, 9.3808e-15, 1.11936e-06, 4.49533333333333e-11,
2.68631578947368e-08, 0.00127921951219512, 5.027e-10, 0.00550468085106383,
7.7e-07, 9.83529411764706e-10, 0.0126520754716981, 4.98666666666667e-14,
4.532e-12, 1.21104761904762e-07, 1.1792e-11, 0.02336, 3.21391304347826e-07,
0.0087648, 0.0010648, 7.97866666666667e-11, 4.7025e-05, 1.0956e-19,
3.92088888888889e-09), Samples = 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, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("(0,200]", "(200,400]",
"(400,600]", "(600,800]", "(800,1.02e+03]"), class = "factor")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -61L))
summary(data$measure)
sc <- scale_colour_gradientn(
colors = c("blue", "white", "yellow", "red"),
values = scales::rescale(c(
data$measure %>% min(na.rm = TRUE),
0,
60,
data$measure %>% max(na.rm = TRUE))
))
p <- ggplot2::ggplot(data, ggplot2::aes(
x = signature,
y = feature
))
p <- p + ggplot2::geom_point(ggplot2::aes_string(
colour = "measure",
size = "Samples"
)) + ggplot2::scale_size_discrete(drop = FALSE) +
ggplot2::scale_x_discrete(drop = FALSE)
p + sc

Geom_area order in ggplot

I have sample cohort data that is not color-coded or displayed in proper chronological order when plotted with ggplot(). The following code is used to generate the plot:
library(ggplot2)
blues <- colorRampPalette(c('lightblue', 'darkblue'))
p <- ggplot(cohort.chart, aes(x=month, y=users, group=cohort))
p + geom_area(aes(fill = cohort)) +
scale_fill_manual(values = blues(12)) +
ggtitle('Users by cohort') +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
As you can see in the legend, the months are not in chronological order, and that is reflected in the plot. I have tried changing both the cohort and the month to a yearmon object with as.yearmon() but this produces the error that continuous variables cannot be used. How does one solve this issue in ggplot() ?
Here is the data
> dput(cohort.chart)
structure(list(cohort = structure(c(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), .Label = c("Apr 2017", "Aug 2017", "Dec 2016",
"Feb 2017", "Jan 2017", "Jul 2017", "Jun 2017", "Mar 2017", "May 2017",
"Nov 2016", "Oct 2016", "Sep 2017"), class = "factor"), month = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L), .Label = c("Oct 2016", "Nov 2016",
"Dec 2016", "Jan 2017", "Feb 2017", "Mar 2017", "Apr 2017", "May 2017",
"Jun 2017", "Jul 2017", "Aug 2017", "Sep 2017"), class = "factor"),
users = c(795, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 672, 92,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 526, 48, 26, 0, 0, 0, 0, 0,
0, 0, 0, 0, 506, 37, 14, 26, 0, 0, 0, 0, 0, 0, 0, 0, 514,
46, 18, 19, 37, 0, 0, 0, 0, 0, 0, 0, 532, 47, 16, 18, 22,
57, 0, 0, 0, 0, 0, 0, 589, 55, 15, 20, 24, 39, 88, 0, 0,
0, 0, 0, 548, 53, 18, 21, 25, 39, 62, 172, 0, 0, 0, 0, 559,
53, 20, 20, 23, 36, 61, 139, 133, 0, 0, 0, 538, 58, 17, 18,
22, 39, 54, 130, 99, 109, 0, 0, 519, 45, 16, 19, 26, 39,
50, 125, 96, 86, 115, 0, 530, 54, 18, 20, 23, 37, 51, 129,
88, 75, 85, 126)), row.names = c(NA, -144L), .Names = c("cohort",
"month", "users"), class = "data.frame")
The plot should resemble the following:
Based on the data frame your provided, your month column is in the right factor level, but your cohort column is not, so change the factor level of your cohort column to be the same as your month column.
library(ggplot2)
blues <- colorRampPalette(c('lightblue', 'darkblue'))
# Change the factor level
cohort.chart$cohort <- factor(cohort.chart$cohort, levels = levels(cohort.chart$month))
p <- ggplot(cohort.chart, aes(x=month, y=users, group=cohort))
p + geom_area(aes(fill = cohort)) +
scale_fill_manual(values = blues(12)) +
ggtitle('Users by cohort') +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
When you created the dataframe, R (automatically) converted the strings into factors, creating a new factor for each new cohort date in alphabetical order.
cohort.chart$cohort
[1]... 12 Levels: Apr 2017 Aug 2017 Dec 2016 Feb 2017 Jan 2017 Jul 2017 Jun 2017 Mar 2017 May 2017 ... Sep 2017
You can use as.yearmon to fix this, you just need to convert back to a factor afterwards.
cohort.chart$cohort <- as.yearmon(cohort.chart$cohort)
cohort.chart$cohort <- as.factor(cohort.chart$cohort)

How can I order the months chronologically in ggplot2 short of writing the months out?

I am trying to plot count v/s month
ggplot(dat, aes(x=month, y=count,group=region)) +
geom_line(data=mcount[mcount$region == "West coast", ],colour="black",stat="identity", position="dodge")+
geom_point(data=mcount[mcount$region == "West coast", ],colour="black", size=2, shape=21, fill="white")+
theme_bw()+
theme(legend.key = element_rect(colour = "black")) +
guides(fill = guide_legend(override.aes = list(colour = NULL)))+
ggsave("test.png",width=6, height=4,dpi=300)
But I want to order the months chronologically from Jan to Dec. How can I do this short of writing all the months out?
dput
structure(list(region = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L), .Label = c("West coast", "Arizona", "Front range", "Flash flood alley",
"Mississippi valley", "Appalachians"), class = "factor"), month = structure(c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 4L, 12L, 11L,
5L, 2L, 9L, 8L, 6L, 10L, 3L, 7L, 8L, 10L, 5L, 1L, 6L, 7L, 4L,
6L, 8L, 2L, 1L, 7L, 5L, 3L, 11L, 12L, 9L, 10L, 2L, 7L, 3L, 6L,
12L, 11L, 10L, 9L, 4L, 1L, 11L, 4L, 2L, 1L, 12L, 9L, 3L, 8L,
5L, 6L, 10L, 7L, 5L, 8L, 11L, 12L, 4L, 3L, 9L, 2L), .Label = c("Apr",
"Dec", "Oct", "Mar", "May", "Jul", "Sep", "Jun", "Nov", "Aug",
"Jan", "Feb"), class = "factor"), count = c(566, 545, 427, 751,
357, 399, 568, 433, 454, 347, 511, 251, 267, 207, 167, 142, 417,
109, 117, 373, 207, 130, 125, 145, 7, 14, 2, 2, 7, 3, 107, 74,
135, 48, 80, 53, 117, 125, 59, 53, 103, 30, 21, 18, 8, 22, 26,
37, 20, 5, 11, 1, 96, 29, 109, 8, 33, 53, 6, 1, 5, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0)), .Names = c("region", "month", "count"), row.names = c(NA,
-72L), class = c("data.table", "data.frame"))
Use the built-in month.name or month.abb variable to specify the levels of your factor in the correct order. In this case, you have abbreviations so month.abb is appropriate.
your_data$month = factor(your_data$month, levels = month.abb)
I think creating the factor in the correct order is the best way, but you can also just order the axis using the limits argument of the discrete scale (see ?discrete_scale for more info).
+ scale_x_discrete(limits = month.abb)
Locales
If you are in a non-English locale, you can construct your own month name constants with a little date formatting (basically stolen from Brian Ripley in this R-Help thread):
month.name.loc = format(ISOdate(2004, 1:12, 1), "%B")
month.abb.loc = format(ISOdate(2004, 1:12, 1), "%b")
If you want to use month names/abbreviations from a different locale than you're in, the withr package is useful.

Plot per species using a for-loop?

I have 9 plant species for which I want to plot a relationship. I have been plotting these the long way because I need to add text to the graphs individually. I have the 9 plots formatted to fit on one page in a 3x3 grid.
But is there a way to do this with a loop instead? And get the 9 plots formatted into 3x3? If this is possible, can you still add text to each plot?
I have code like this for all 9 but here are the first 2 species.
First I subset by species:
Acru<-carbon2[Species=="Acru",]
Arte<-carbon2[Species=="Arte",]
...
par(mfrow=c(3,3), cex=.3)
plot(LogRecBio~LogPreBiomass,data=Acru,font.lab=2,font.main=2, font.sub=3, mgp=c(2.5,1,0), cex.lab=2, cex.main=1.5, axes=F,lwd=1.5, cex=2.5, ann=F)
box()
axis(1,at=c(-2,-1,0,1,2,3,4),font=2,cex.axis=3)
axis(2,at=c(-5,-4,-3,-2,-1,0,1,2),font=2,cex.axis=3)
text(2,-0.27, sprintf("a) Acer rubrum"),font=3,cex=2.5)
text(2.35,-0.6,sprintf("R²= 0.18"),cex=2.5)
fit.bio.acru1 <- lm(LogRecBio ~LogPreBiomass, data=Acru, subset=c(Site=="7"))
abline(fit.bio.acru1, lwd=2, col='red')
fit.bio.acru2 <- lm(LogRecBio ~LogPreBiomass, data=Acru,subset=c(Site=="8"))
abline(fit.bio.acru2, lwd=2, col='blue')
fit.bio.acru
plot(LogRecBio~LogPreBiomass,data=Arte,font.lab=2,font.main=2, font.sub=3, mgp=c(2.5,1,0), cex.lab=2, cex.main=1.5, cex.axis=1.2,axes=F,lwd=1.5, cex=2.2, ann=F)
box()
axis(1,at=c(-3,-2,-1,0,1.0,2,3),font=2,cex.axis=3)
axis(2,at=c(-4,-3,-2,-1,0,1,2),font=2,cex.axis=3)
text(2.05,0.97, sprintf("b) Arundinaria tecta"),font=3,cex= 2.5)
text(2.56,0.59,sprintf("R² = 0.04"),cex= 2.5)
fit.bio.arte <- lm(LogRecBio ~LogPreBiomass, data=Arte)
abline(fit.bio.arte, lwd=2, col='red')
fit.bio.arte
Part of data frame carbon2:
structure(list(Treatment = c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), Species = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L), .Label = c("Acru", "Arte", "Clal", "Euro", "Gafr", "Ilgl",
"Lylu", "Oxar", "Pepu"), class = "factor"), Tag = c(64L, 248L,
249L, 250L, 251L, 252L, 253L, 315L, 316L, 318L, 931L, 932L, 933L,
934L, 935L, 936L, 3L, 4L, 5L, 6L, 917L, 918L, 919L, 920L, 921L,
923L, 924L, 995L, 996L, 997L, 208L, 209L, 210L, 211L, 212L, 213L,
214L, 215L, 323L, 324L, 925L, 926L, 927L, 928L, 929L, 930L, 987L,
988L, 989L, 990L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 51L,
52L, 912L, 913L, 914L, 915L, 916L, 59L, 60L, 62L, 63L, 240L,
901L, 902L, 903L, 904L, 905L, 907L, 908L, 909L, 22L, 23L, 937L,
938L, 939L, 976L, 977L, 979L, 980L, 981L, 985L, 986L), site = c(12L,
7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 23L, 23L, 23L, 23L, 23L,
23L, 16L, 16L, 16L, 16L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 16L,
16L, 16L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 23L, 23L, 23L,
23L, 23L, 23L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 12L, 12L, 23L, 23L, 23L, 23L, 23L, 12L, 12L, 12L, 12L,
7L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 12L, 12L, 23L, 23L,
23L, 16L, 16L, 16L, 16L, 16L, 16L, 16L), stem = 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), heightPre = c(58, 50, 38, 33, 28,
18, 20, 28, 47, 118, 109, 103, 99, 79, 69, 73, 54, 33, 32.5,
74, 67, 57, 77, 49, 45, 58, 89, 49, 33, 37, 69, 53, 64, 49, 70,
36, 59, 66.5, 21, 36.5, 59, 37, 59, 55, 33, 27, 35, 41, 65, 56,
37, 53, 73, 59, 16, 51, 37, 77, 79, 45, 68, 75, 110, 56, 115,
42, 90, 107, 86.5, 37, 113, 211, 130, 175, 135, 175, 122, 66,
115, 167, 84, 63.5, 144, 16, 51, 45, 54, 32, 70, 53), height1mo = c(0,
0, 0, 0, 0, 0, 0, 6, 10, 15, 25.5, 25, 26.5, 20, 19.5, 18, 19,
8.5, 20, 12, 7.5, 4, 7, 0, 4, 11, 3, 9, 3.5, 12.5, 10, 12.5,
19, 17.5, 11.5, 7, 12, 14, 18, 18, 3, 3.5, 6, 1, 9, 0.5, 7.5,
9, 17, 15, 1.5, 7.5, 14, 18, 5, 4.5, 6, 15.5, 15, 2, 0.5, 0,
12, 0, 5, 0, 0, 0, 0, 0, 37, 31, 14, 0, 20, 35, 0, 39, 0, 0,
21, 7, 12, 11, 23.5, 15, 26, 7, 5, 17), height2mo = c(2.5, 7.5,
9, 10, 12, 0, 8, 23, 34, 26.5, 57, 49, 57.5, 41, 43, 28, 29,
11, 29.5, 20, 21, 20, 10, 7, 11, 22.5, 13, 21, 17.5, 29.5, 24.5,
38, 53, 49, 44, 19, 38, 49, 37, 40, 10, 0, 18, 8.5, 19.5, 0.5,
9, 23, 29, 22, 6.5, 26, 36.5, 39, 15, 12.5, 13.5, 30, 45, 16.5,
2, 0, 14, 20, 43, 0.5, 1, 1, 0, 0, 86, 106, 59, 0, 67, 83, 32,
83, 3, 4.5, 49, 9.5, 43, 20, 35, 16, 27, 7, 7, 26.5), height4mo = c(7,
11.5, 10, 8.5, 17, 6.5, 8.5, 26, 34, 33, 62.5, 51.5, 61, 42.5,
46.5, 29, 30, 11.5, 26.5, 20, 23, 29, 11.5, 21, 12.5, 25, 15.5,
24, 28.5, 37, 51, 46, 60.5, 55, 53, 25, 49, 56, 40, 43, 23, 8,
16, 8.5, 19.5, 2.5, 9.5, 22.5, 21, 20, 6.5, 31, 42, 45, 15, 17.5,
13.5, 36.5, 47, 20, 13, 18, 20, 27.5, 70, 31, 21, 25, 42, 12,
89, 128, 64, 45, 69, 84.5, 32, 86, 31, 31, 50, 10, 49.5, 20,
42.5, 15, 38, 7.5, 13, 33), PreBiomass = c(3.895575649, 2.834165975,
1.573928396, 1.163246736, 0.817978445, 0.317316605, 0.39770427,
0.817978445, 2.48217785, 17.85049586, 12.66281683, 11.31037967,
10.45107234, 6.662611474, 5.086139353, 5.691293433, 3.119036919,
1.167754804, 1.13272457, 5.847881474, 5.365833116, 3.999508626,
6.910020728, 3.038057965, 2.602293243, 4.127993434, 8.991590789,
3.038057965, 1.480678454, 1.823048057, 2.062949335, 1.18423519,
1.760986422, 1.004008739, 2.126354774, 0.524854338, 1.483984987,
1.908835132, 0.168869468, 0.540308647, 5.375747945, 2.053683314,
5.375747945, 4.65118718, 1.622061536, 1.072373896, 1.831323212,
2.537878044, 6.564139518, 4.827265975, 2.156129781, 5.153936858,
11.20247906, 6.684694751, 0.282366443, 4.694933633, 2.156129781,
12.74952183, 13.56745754, 3.465912243, 7.799750512, 9.849322745,
24.51741906, 4.912423703, 27.25487445, 2.476234093, 15.20382114,
22.9550974, 13.83353724, 1.831101565, 33.25213131, 139.5296871,
45.87772104, 90.79892694, 50.03156249, 90.79892694, 39.65094302,
9.671283982, 34.61929294, 81.54741169, 26.00946401, 14.01849263,
85.55945731, 0.667045516, 8.637292691, 6.550744926, 9.799815468,
3.084543759, 17.38618089, 9.403377144), Biomass1mo = c(0, 0,
0, 0, 0, 0, 0, 0.030124963, 0.090031144, 0.214679983, 0.698192549,
0.67114866, 0.75387857, 0.430024245, 0.408844585, 0.348506855,
0.388198419, 0.078012828, 0.430024245, 0.155212563, 0.296240906,
0.112200441, 0.26629734, 0, 0.112200441, 0.535234264, 0.071949318,
0.39259111, 0.091290587, 0.652064296, 0.094858153, 0.142288516,
0.304507388, 0.262240101, 0.122283739, 0.04961378, 0.132115845,
0.174825154, 0.276013506, 0.276013506, 0.022723782, 0.030242664,
0.082163804, 0.002963157, 0.174263551, 0.000819511, 0.124274142,
0.174263551, 0.566729877, 0.449345809, 0.01811283, 0.21782346,
0.57145776, 0.842645845, 0.116410145, 0.09891947, 0.154294203,
0.668793517, 0.635749905, 0.028252321, 0.005400266, 0, 0.308521938,
0, 0.101231381, 0, 0, 0, 0, 0, 3.291544691, 2.308284799, 0.468693323,
0, 0.958429423, 2.944402977, 0, 3.658082992, 0, 0, 1.524728852,
0.19037959, 0.528356631, 0.448087868, 1.886696465, 0.806223299,
2.284810808, 0.19037959, 0.10066607, 1.021875261), Biomass2mo = c(0.00461379,
0.048598634, 0.071833216, 0.090031144, 0.133074246, 0, 0.055807842,
0.536596536, 1.24010478, 0.726931224, 3.474265098, 2.569451142,
3.535326978, 1.800572943, 1.980042107, 0.841402428, 0.902414303,
0.130479556, 0.933718887, 0.430024245, 1.453050159, 1.347577447,
0.461969077, 0.26629734, 0.535234264, 1.616437082, 0.6927849,
1.453050159, 1.09644076, 2.456139386, 0.483312867, 1.0729993,
1.964067221, 1.703060052, 1.400530346, 0.304507388, 1.0729993,
1.703060052, 1.0222427, 1.177817154, 0.211862783, 0, 0.630095648,
0.156738681, 0.730913281, 0.000819511, 0.174263551, 0.992673892,
1.525735605, 0.914132292, 0.174609348, 1.487410339, 2.512370265,
2.7832013, 0.635749905, 0.479653397, 0.540228204, 1.855529897,
3.472016487, 0.736631727, 0.031533874, 0, 0.375407219, 0.591122274,
1.566167617, 0.005400266, 0.013049571, 0.013049571, 0, 0, 17.86670522,
27.17489168, 8.39141448, 0, 10.82904112, 16.63862372, 2.460045699,
16.63862372, 0.021336774, 0.048116873, 7.586947075, 0.339458495,
5.924289588, 1.390158514, 4.011712305, 0.911037264, 2.454089599,
0.19037959, 0.19037959, 2.368736511), Biomass4mo = c(0.041918611,
0.121473174, 0.090031144, 0.063551149, 0.280731338, 0.035762566,
0.063551149, 0.697852512, 1.24010478, 1.163246736, 4.175122771,
2.837608296, 3.977614516, 1.934377716, 2.314568827, 0.902414303,
0.96555584, 0.142578593, 0.75387857, 0.430024245, 1.672251324,
2.392140111, 0.573271986, 1.453050159, 0.652064296, 1.902090818,
0.909034181, 1.785867231, 2.328738868, 3.484981432, 1.831472627,
1.518350095, 2.498057072, 2.100814509, 1.964067221, 0.501385136,
1.703060052, 2.170736601, 1.177817154, 1.343229421, 0.992673892,
0.140073002, 0.506470737, 0.156738681, 0.730913281, 0.016205216,
0.192640481, 0.953030318, 0.838582725, 0.766045551, 0.174609348,
1.951972553, 3.12089825, 3.472016487, 0.635749905, 0.806750379,
0.540228204, 2.512370265, 3.713345005, 0.991640221, 0.341613273,
0.516931045, 0.591122274, 0.886590001, 2.912202284, 1.032643803,
0.628997882, 0.785297256, 1.519953371, 0.308521938, 19.13863569,
39.66748749, 9.878452915, 4.874133229, 11.48709241, 17.24718389,
2.460045699, 17.86670522, 2.308284799, 2.308284799, 7.882847273,
0.374087949, 7.734229305, 1.390158514, 5.794509429, 0.806223299,
4.687787249, 0.21695254, 0.614836496, 3.588685645), PreCarbon = c(0.156967627,
0.122843082, 0.075813652, 0.059169491, 0.044340523, 0.020426006,
0.024570672, 0.043002708, 0.106999734, 0.545053086, 0.320308244,
0.290501628, 0.271291986, 0.183819859, 0.145631674, 0.160452129,
0.095815131, 0.041269875, 0.0402085, 0.164611233, 0.206240523,
0.161958253, 0.253981948, 0.129206528, 0.113781088, 0.166223479,
0.31553078, 0.129335339, 0.071711875, 0.085049069, 0.09317797,
0.056638382, 0.080836829, 0.04885422, 0.09574618, 0.027353778,
0.069332271, 0.086903123, 0.009475442, 0.026773874, 0.160707434,
0.070160311, 0.160707434, 0.141844012, 0.057277905, 0.04013973,
0.063665515, 0.084295725, 0.191213272, 0.146673529, 0.04757003,
0.100344991, 0.195222186, 0.125396417, 0.008354372, 0.092637319,
0.04757003, 0.218128582, 0.230401206, 0.07162901, 0.131269005,
0.157389096, 0.320282156, 0.091653711, 0.347792395, 0.054313153,
0.221509506, 0.305003004, 0.205854943, 0.043576888, 1.197871953,
3.518128126, 1.526275089, 2.548979546, 1.629080226, 2.548979546,
1.367623475, 0.472267765, 1.236694558, 2.352499024, 0.638415034,
0.362069714, 1.904909524, 0.022514492, 0.232778297, 0.18076034,
0.261297274, 0.090855905, 0.441867498, 0.25160457), X1moCarbon = c(0,
0, 0, 0, 0, 0, 0, 0.002996291, 0.00733127, 0.014924131, 0.027262412,
0.026357968, 0.02910929, 0.018027068, 0.017267076, 0.015069346,
0.016555346, 0.004223662, 0.018064917, 0.007582482, 0.025240181,
0.011434171, 0.023138557, 0, 0.011434171, 0.040903752, 0.007959618,
0.031793903, 0.009674612, 0.048110681, 0.007167162, 0.009865614,
0.017984257, 0.015981995, 0.00875496, 0.004302588, 0.009305238,
0.011605185, 0.015676514, 0.015676514, 0.001435067, 0.001828411,
0.004266066, 0.000255474, 0.008072101, 8.6e-05, 0.006067683,
0.008083206, 0.022002661, 0.018064652, 0.000900408, 0.009931103,
0.025212867, 0.036698014, 0.005423393, 0.004634604, 0.007118678,
0.029351971, 0.028106107, 0.001391831, 0.000170474, 0, 0.00861976,
0, 0.002921775, 0, 0, 0, 0, 0, 0.145554027, 0.112095861, 0.034715829,
0, 0.058726823, 0.134088229, 0, 0.157321152, 0, 0, 0.046764496,
0.007082034, 0.017870499, 0.015406694, 0.056813876, 0.026251236,
0.067611463, 0.007089935, 0.003979541, 0.032552937), X2moCarbon = c(0.000649991,
0.004515538, 0.006213783, 0.007472947, 0.010284713, 0, 0.005055737,
0.031163252, 0.061950899, 0.03996902, 0.104905954, 0.080952912,
0.106489463, 0.0596727, 0.064736059, 0.031121663, 0.033108443,
0.006368256, 0.034087686, 0.017584511, 0.090428862, 0.085023956,
0.035457668, 0.022619457, 0.039985244, 0.098665234, 0.04936287,
0.090524095, 0.071905993, 0.139112044, 0.01893176, 0.035575351,
0.057471358, 0.051317148, 0.043939693, 0.013147169, 0.035575351,
0.051317148, 0.032779136, 0.036681952, 0.010956757, 0, 0.027658897,
0.008484002, 0.031380578, 9.89e-05, 0.009296871, 0.040783331,
0.058816936, 0.038019773, 0.008653254, 0.068600624, 0.113960148,
0.125843837, 0.030152569, 0.022965412, 0.025762347, 0.084974223,
0.156538314, 0.034969776, 0.000617961, 0, 0.006829998, 0.010617342,
0.027416033, 0.000112755, 0.000265127, 0.000265127, 0, 0, 0.893643875,
1.221422984, 0.50961417, 0, 0.615827082, 0.847505481, 0.205500657,
0.847505481, 0.006343264, 0.011521572, 0.304122678, 0.017974685,
0.242561906, 0.064815574, 0.170213577, 0.04413614, 0.108738758,
0.010654473, 0.010654473, 0.105287901), X4moCarbon = c(0.003939148,
0.009545784, 0.007472947, 0.005621946, 0.018936017, 0.003514856,
0.005621946, 0.038653227, 0.061950899, 0.058780944, 0.122873324,
0.088156055, 0.117853953, 0.063453978, 0.074010607, 0.03304018,
0.035078824, 0.006867286, 0.028392571, 0.017584511, 0.101443992,
0.135990762, 0.042290614, 0.090428862, 0.046980199, 0.112718863,
0.061628519, 0.10716323, 0.133173424, 0.185313121, 0.054366886,
0.046847602, 0.069583598, 0.060629528, 0.057471358, 0.019488753,
0.051317148, 0.062228442, 0.036681952, 0.040720029, 0.040718675,
0.007712115, 0.022971487, 0.008484002, 0.031380578, 0.001238836,
0.010122608, 0.039392597, 0.035328505, 0.032710879, 0.008653254,
0.089246941, 0.140615722, 0.155927128, 0.030152569, 0.037960536,
0.025762347, 0.113960148, 0.167057162, 0.046603193, 0.006232079,
0.009319662, 0.010617342, 0.01574909, 0.050249507, 0.018386847,
0.011359619, 0.014091865, 0.026775581, 0.005739173, 0.940599218,
1.618014419, 0.57521217, 0.340713733, 0.643414594, 0.870471728,
0.205500657, 0.893643875, 0.198091533, 0.198091533, 0.314956518,
0.01963135, 0.309519324, 0.064815574, 0.23812921, 0.039496022,
0.196215504, 0.011995076, 0.030876347, 0.153759364), TotalCarbon = c(2.501938424,
2.332218951, 1.641013423, 1.392483377, 1.696000406, 0.411835774,
0.764726634, 3.251928154, 6.361282467, 11.96169249, 13.62052751,
11.04045665, 12.83366357, 7.61659474, 7.576922118, 5.024502369,
4.227801803, 1.174993138, 3.259824259, 3.901744082, 10.5848291,
10.50668723, 7.021121066, 5.668839356, 5.08697083, 10.92840985,
8.922540693, 9.705419805, 8.451769698, 13.81683185, 3.988112769,
4.003878786, 6.156035357, 5.100700715, 5.268944019, 1.651130691,
4.319967871, 5.653749527, 2.952799027, 3.509044554, 4.146751849,
1.311194265, 4.40839747, 2.767792413, 3.333793425, 0.645000544,
1.768035403, 4.402711785, 6.904856277, 5.163288885, 1.376050648,
7.418577716, 12.65320411, 12.77220297, 2.468109142, 3.631338446,
2.75250667, 10.95485279, 15.93354872, 4.067048349, 2.1863628,
2.640426289, 5.559398908, 2.325058743, 8.001919262, 1.371376664,
3.675361853, 5.009731732, 3.891091568, 0.825828495, 88.5833406,
157.9578266, 63.60386651, 48.45610517, 72.33176226, 104.4979151,
35.92690142, 74.39089664, 24.77861122, 41.74870207, 33.41190899,
6.935077567, 49.04256584, 5.429985864, 19.14736986, 6.276180599,
15.71334028, 2.308491154, 8.093447265, 13.61309907), OLDestAssim = c(0.16273292,
0.531283865, 0.546784515, 0.548005831, 1.117252104, 0.114703735,
0.430112005, 2.958994249, 5.413171027, 4.46316534, 10.53485645,
8.109549494, 10.50791092, 5.898234837, 6.492409411, 3.283621134,
3.499740655, 0.718250337, 3.370987696, 1.789036071, 8.664887392,
9.086176264, 3.919440166, 4.123497491, 3.919792348, 9.950535196,
4.756772076, 9.060767572, 8.278498547, 14.54720467, 4.724107286,
5.766516948, 9.027313298, 7.989111013, 6.951312225, 2.28863022,
5.981065361, 7.901084227, 5.4728181, 6.020818486, 1.853280059,
0.311044291, 2.18794981, 0.68034361, 2.765418329, 0.047128668,
0.980573348, 3.453418537, 4.673671847, 3.476060731, 0.646316335,
5.778390351, 9.633196576, 10.92370219, 2.319186273, 2.209960947,
2.058639203, 7.773645484, 12.22756086, 2.852689634, 0.386565881,
0.496860166, 1.407968874, 1.680611096, 4.916640727, 0.977215919,
0.620253347, 0.763829673, 1.409081851, 0.302135427, 44.7470637,
63.92221417, 24.86052444, 6.076027823, 29.59811425, 41.96954834,
9.196665562, 43.0963223, 3.680815508, 3.819136463, 19.30141334,
1.332232454, 16.07431445, 4.286639367, 13.30975772, 3.365326795,
10.62150582, 0.898527189, 1.227305975, 8.317751517), CARBONmulti = c(2.621415393,
2.398836005, 1.641013423, 1.392483377, 1.696000406, 0.411835774,
0.809806973, 3.251928154, 6.361282467, 11.96169249, 13.62052751,
11.04045665, 12.83366357, 7.61659474, 7.576922118, 5.024502369,
4.227801803, 1.174993138, 4.510076237, 3.901744082, 18.5717312,
10.50668723, 7.021121066, 8.059558949, 5.08697083, 10.92840985,
11.70126006, 16.07362835, 9.527447818, 21.36601039, 4.377265297,
4.003878786, 6.156035357, 6.514095673, 5.268944019, 2.169858768,
4.319967871, 5.653749527, 2.952799027, 4.588995837, 4.705122057,
1.542557708, 6.195249968, 2.767792413, 3.480946773, 0.645000544,
1.768035403, 4.961955587, 6.904856277, 5.163288885, 1.765904086,
7.418577716, 12.65320411, 12.77220297, 2.468109142, 3.631338446,
3.441469031, 15.95980398, 15.93354872, 4.067048349, 2.1863628,
2.640426289, 6.723791976, 2.325058743, 8.001919262, 1.857730159,
3.675361853, 5.330578088, 3.891091568, 0.825828495, 88.5833406,
157.9578266, 63.60386651, 48.45610517, 82.73192555, 106.3419007,
35.92690142, 74.66251592, 24.77861122, 41.74870207, 35.0178608,
6.935077567, 49.04256584, 5.429985864, 19.14736986, 6.276180599,
23.40428597, 2.308491154, 8.093447265, 13.61309907), RecBiomass = c(0.010760569,
0.042860289, 0.05720155, 0.054632562, 0.343201388, 0.112703103,
0.159794989, 0.853142911, 0.499603516, 0.065166074, 0.329715167,
0.250885327, 0.380593913, 0.290333261, 0.455073813, 0.158560495,
0.309568583, 0.122096345, 0.665544467, 0.073535048, 0.311648031,
0.598108502, 0.082962412, 0.478282566, 0.25057295, 0.460778547,
0.10109826, 0.587831849, 1.572751236, 1.911623458, 0.887793314,
1.282135599, 1.418555556, 2.092426518, 0.923678046, 0.955284351,
1.1476262, 1.137204866, 6.974719413, 2.48604095, 0.184657819,
0.068205746, 0.094214004, 0.03369864, 0.450607616, 0.015111536,
0.105191962, 0.375522504, 0.127752118, 0.158691391, 0.080982764,
0.378734278, 0.278589965, 0.519397911, 2.25150658, 0.171834246,
0.250554586, 0.197056039, 0.273694979, 0.286112328, 0.043797974,
0.052483918, 0.024110298, 0.180479139, 0.106850695, 0.417021883,
0.041371039, 0.034210147, 0.109874528, 0.168489801, 0.575561173,
0.284294248, 0.215321352, 0.053680516, 0.229596915, 0.189949204,
0.062042552, 1.847397435, 0.066676255, 0.028306046, 0.303076114,
0.026685319, 0.09039596, 2.084053457, 0.670871028, 0.123073529,
0.478354645, 0.070335374, 0.035363517, 0.381637957), RecovCarbonMULTI = c(0.672921188,
0.846399267, 1.042622667, 1.197066224, 2.073404766, 1.297870225,
2.03620387, 3.975567052, 2.562782706, 0.670104214, 1.075631725,
0.976134929, 1.227975767, 1.143184586, 1.489719725, 0.882840154,
1.355483091, 1.006198505, 3.981617736, 0.667206423, 3.461108611,
2.626994517, 1.016078148, 2.65286543, 1.954803074, 2.647390319,
1.301355937, 5.290757627, 6.434515065, 11.7199381, 2.121848182,
3.380982781, 3.495788088, 6.488086629, 2.477923291, 4.134211359,
2.911059012, 2.961884676, 17.4856892, 8.493285951, 0.875249752,
0.751117613, 1.15244428, 0.595072248, 2.146001675, 0.601469829,
0.965441486, 1.955159193, 1.051905777, 1.069609363, 0.819015674,
1.439400194, 1.129500358, 1.910663604, 8.740801894, 0.773458952,
1.596132599, 1.251796278, 1.174394589, 1.173442391, 0.280311889,
0.268082015, 0.274245505, 0.473301752, 0.293595895, 0.750223965,
0.241739351, 0.23221762, 0.281279582, 0.451000923, 2.663989859,
1.132073252, 1.386378073, 0.533663853, 1.65359468, 1.171180148,
0.906079369, 7.72002105, 0.715745734, 0.511956188, 1.346350728,
0.494709221, 0.573198655, 8.14035284, 2.216825404, 0.958086549,
2.388237416, 0.748406032, 0.465510357, 1.447681919), LogRespBiomass = c(-3.172025374,
-2.108061831, -2.407599624, -2.755910201, -1.27035716, -3.330853575,
-2.755910201, -0.359747499, 0.215195876, 0.151215006, 1.429143764,
1.042961548, 1.380682272, 0.659785681, 0.839223418, -0.102681548,
-0.035051343, -1.947861901, -0.282523972, -0.843913688, 0.514170817,
0.872188409, -0.556395005, 0.373664905, -0.427612108, 0.642953712,
-0.095372583, 0.579904141, 0.845326863, 1.248462716, 0.605120357,
0.417624281, 0.915513259, 0.742325131, 0.675017436, -0.690380739,
0.532426664, 0.775066557, 0.163662856, 0.29507673, -0.007353076,
-1.965591549, -0.680288732, -1.853175313, -0.313460457, -4.122422113,
-1.64692962, -0.048108563, -0.176042044, -0.266513645, -1.745204097,
0.668840427, 1.138120861, 1.244735545, -0.452950024, -0.214740978,
-0.615763629, 0.921226636, 1.311933089, -0.008394918, -1.074075962,
-0.659845789, -0.52573239, -0.120372635, 1.068909594, 0.032122313,
-0.46362739, -0.241692963, 0.418679657, -1.175962326, 2.951709103,
3.680531897, 2.290355912, 1.583942289, 2.441224006, 2.847648877,
0.900179927, 2.882938937, 0.836504737, 0.836504737, 2.064689168,
-0.983264351, 2.045655842, 0.329417779, 1.756910819, -0.215394529,
1.544960669, -1.528076659, -0.486398907, 1.27778602), LogPreBiomass = c(1.35984146,
1.041747705, 0.453574657, 0.151215006, -0.200919294, -1.14785525,
-0.92204659, -0.200919294, 0.90913634, 2.882031287, 2.53866989,
2.425720859, 2.346704589, 1.896511521, 1.626519066, 1.738937539,
1.137524274, 0.155082934, 0.124625855, 1.766079454, 1.680051651,
1.38617151, 1.932972637, 1.111218484, 0.956393073, 1.417791438,
2.196289784, 1.111218484, 0.392500398, 0.600509857, 0.724136675,
0.169097157, 0.565874119, 0.004000725, 0.75440914, -0.644634506,
0.394731028, 0.646493177, -1.778629241, -0.615614734, 1.681897717,
0.719634919, 1.681897717, 1.537122495, 0.483697893, 0.069874785,
0.605038772, 0.931328316, 1.881621428, 1.574280257, 0.768314847,
1.639760861, 2.416135098, 1.899820548, -1.264549608, 1.546483977,
0.768314847, 2.545493767, 2.607674097, 1.242975872, 2.054091748,
2.287402696, 3.199383847, 1.591767446, 3.305232384, 0.906738895,
2.721546787, 3.133540021, 2.627095879, 0.604917734, 3.504118865,
4.938277389, 3.825979619, 4.508647468, 3.912654056, 4.508647468,
3.680114731, 2.269161081, 3.544411126, 4.40118459, 3.258460472,
2.64037736, 4.449211541, -0.404896996, 2.156089188, 1.879578772,
2.282363556, 1.126403756, 2.855675689, 2.241068895), LogRecBio = c(-4.531866845,
-3.149809546, -2.861174283, -2.9071252, -1.069437867, -2.182998325,
-1.833863604, -0.158828206, -0.693940463, -2.730816283, -1.109526128,
-1.382759309, -0.966022317, -1.23672584, -0.787295647, -1.841619087,
-1.172575619, -2.102944833, -0.407149826, -2.609993143, -1.165880834,
-0.5139831, -2.489367641, -0.737553579, -1.384005183, -0.774837727,
-2.291662364, -0.531314343, 0.452826465, 0.647952859, -0.119016318,
0.248527124, 0.34963914, 0.738324406, -0.079391703, -0.045746233,
0.137695635, 0.12857338, 1.942292099, 0.910691465, -1.689250794,
-2.685226465, -2.362186446, -3.390297799, -0.797158349, -4.192296853,
-2.251968388, -0.979436879, -2.057663471, -1.8407939, -2.513518937,
-0.970920433, -1.278014238, -0.655085002, 0.811599583, -1.761224953,
-1.384078474, -1.624267129, -1.295741008, -1.25137079, -3.128167718,
-2.94724848, -3.725116227, -1.712140081, -2.236322793, -0.874616581,
-3.185174184, -3.375232983, -2.208416219, -1.780880059, -0.552409761,
-1.257745493, -1.535623706, -2.924705174, -1.471430051, -1.66099859,
-2.779934807, 0.613777857, -2.707906386, -3.564679858, -1.193771304,
-3.623641715, -2.403555703, 0.734314775, -0.399178369, -2.094973305,
-0.737402886, -2.65448042, -3.342074583, -0.963282876), LogCarbAss = c(0.963714398,
0.874983622, 0.495313992, 0.331088755, 0.528272777, -0.887130616,
-0.210959365, 1.179248099, 1.850230003, 2.481709251, 2.611578031,
2.401566403, 2.552071685, 2.030329385, 2.025107064, 1.614326418,
1.44168219, 0.161262308, 1.506314057, 1.361423654, 2.921640597,
2.352011933, 1.948922901, 2.086858834, 1.626682532, 2.391365807,
2.459696533, 2.777179938, 2.254176877, 3.06180136, 1.476424168,
1.387263588, 1.817432959, 1.873968394, 1.661829967, 0.774662082,
1.463247965, 1.732318958, 1.082753543, 1.523661228, 1.548651715,
0.433441888, 1.823782864, 1.01805004, 1.247304318, -0.438504119,
0.569868988, 1.601799935, 1.932224972, 1.641573757, 0.568662789,
2.003987356, 2.537910472, 2.547271167, 0.903452328, 1.289601298,
1.235898424, 2.77007331, 2.768426869, 1.402917515, 0.782239341,
0.970940377, 1.905652278, 0.843745305, 2.079681421, 0.619355398,
1.301651591, 1.673459692, 1.358689727, -0.19136816, 4.483943811,
5.062328077, 4.152674263, 3.88065834, 4.415605568, 4.666659382,
3.581486358, 4.312978171, 3.20998083, 3.731668363, 3.55585824,
1.936592239, 3.892688612, 1.691936531, 2.952165362, 1.836761611,
3.152919167, 0.836594131, 2.091054755, 2.611032496), LogRecCarb = c(-0.396127062,
-0.166764084, 0.041739334, 0.17987375, 0.729192071, 0.260724633,
0.711087226, 1.380167393, 0.941093663, -0.400322035, 0.07290814,
-0.024154455, 0.205367096, 0.133817864, 0.398587998, -0.124611121,
0.304157916, 0.006179373, 1.381688203, -0.404655801, 1.241588946,
0.965840424, 0.015950264, 0.97564035, 0.670289459, 0.973574369,
0.263406749, 1.665961454, 1.861676479, 2.461291503, 0.752287493,
1.218166431, 1.25155884, 1.869967669, 0.907420827, 1.419296587,
1.068516937, 1.085825781, 2.861382786, 2.139275963, -0.133246002,
-0.286193031, 0.141885148, -0.519072456, 0.763606425, -0.508378904,
-0.035169784, 0.670471619, 0.050603545, 0.067293501, -0.199652057,
0.364226495, 0.121775374, 0.647450618, 2.168001935, -0.256882678,
0.467583578, 0.224579542, 0.160752771, 0.159941643, -1.271852406,
-1.316462319, -1.29373157, -0.74802214, -1.225550964, -0.287383497,
-1.419895195, -1.46008033, -1.268406151, -0.796285893, 0.979824946,
0.124050688, 0.326694644, -0.627989127, 0.502951512, 0.158011914,
-0.098628373, 2.043817091, -0.334430295, -0.669516228, 0.297397768,
-0.703785121, -0.55652293, 2.096833526, 0.796076174, -0.042817162,
0.870555611, -0.289809625, -0.764620933, 0.369963601)), .Names = c("Treatment",
"Species", "Tag", "site", "stem", "heightPre", "height1mo", "height2mo",
"height4mo", "PreBiomass", "Biomass1mo", "Biomass2mo", "Biomass4mo",
"PreCarbon", "X1moCarbon", "X2moCarbon", "X4moCarbon", "TotalCarbon",
"OLDestAssim", "CARBONmulti", "RecBiomass", "RecovCarbonMULTI",
"LogRespBiomass", "LogPreBiomass", "LogRecBio", "LogCarbAss",
"LogRecCarb"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 46L,
47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 105L, 106L, 107L,
108L, 109L, 110L, 111L, 112L, 113L, 114L, 123L, 124L, 125L, 126L,
127L, 128L, 129L, 130L, 131L, 132L, 172L, 173L, 174L, 175L, 176L,
177L, 178L, 179L, 180L, 181L, 227L, 228L, 229L, 230L, 231L, 232L,
233L, 234L, 235L, 236L, 255L, 256L, 257L, 258L, 259L, 260L, 261L,
262L, 263L, 264L, 290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L,
298L, 299L), class = "data.frame")
This should work with the simple graphics package. You need to add the real scientific names, in the same order as they are listed in species. If you want a different order, you can manually set the species too.
species = unique(carbon2$Species) # c("Acru", "Arte", ...)
bin.name = c("Acer rubrum", "Arundinaria tecta", "Clethra alnifolia",
"Eupatorium rotundifolium", "Gaylussacia frondosa",
"Ilex glabra", "Lyonia lucida", "Oxydendrum arboreum",
"Persea palustris")
sites = c(7,8) # sites to use
color = c("blue", "green") # colors for each site
fit.bio = list() # list to save all models
par(mfrow=c(3,3), cex=.3)
for(isp in seq_along(species)) {
sp.data = carbon2[carbon2$Species==species[isp], ]
fit.bio[[isp]] <- lm(LogRecBio ~ LogPreBiomass, data=sp.data)
sp.label = paste(letters[isp], ") ", bin.name[isp], sep="")
sp.r.squared = paste("R²", "=", round(summary(fit.bio[[isp]])$adj.r.squared, 3))
plot(LogRecBio ~ LogPreBiomass, data=sp.data,
font.lab=2,font.main=2, font.sub=3, ylim=c(-5,4),
mgp=c(2.5,1,0), cex.lab=2, cex.main=1.5,
axes=FALSE, lwd=1.5, cex=2.5, ann=FALSE)
for(isite in seq_along(sites)) {
# loop for each selected site
site.data = sp.data[sp.data$site==sites[isite], ]
site.model = lm(LogRecBio ~ LogPreBiomass, data=site.data)
abline(site.model, lwd=2, col=color[isite])
}
abline(fit.bio[[isp]], lwd=2, col='red') # all data
axis(1,at=c(-2,-1,0,1,2,3,4),font=2,cex.axis=3)
axis(2,at=c(-5,-4,-3,-2,-1,0,1,2),font=2,cex.axis=3)
box()
mtext(sp.label, 3, line=-2.5, adj=0.05, font=3, cex=0.75)
mtext(sp.r.squared, 3, line=-2.5, adj=0.95, cex=0.75)
}
In reply to the comments, this is how your code would look like if you used ggplot2.
DF<-read.csv("test.csv")
require(ggplot2)
qplot(data=DF,x=LogPreBiomass,y=LogRecBio,facets=~Species)
I saved your sample as a .csv file named test.csv.Does this work for you?

Resources