Related
I need to represent an important number of graphs. To do that I used a the facet_wrap_paginate function, but I only have the second page printed/plotted.
Here is a sample of my data:
df <- structure(list(oxygen = c(60.86414, 62.50372, 89.76055, 61.15914,
61.49936, 61.90402, 61.60323, 61.2873, 75.12329, 60.83503, 63.03796,
61.78298, 70.16533, 62.60887, 61.49043, 64.42095, 61.78358, 62.29302,
62.88122, 60.47264, 65.92805, 65.0235, 64.32414, 65.56869, 64.65729,
66.73502, 67.01285, 67.47757, 69.62232, 66.09624, 66.18106, 64.28226,
64.85658, 67.46348, 68.93226, 65.23919, 65.67461, 64.64727, 64.90544,
68.10878, 70.00292, 59.54631, 61.43627, 78.75032, 61.94263, 82.08963,
62.75281, 61.93749, 72.86382, 62.06785, 61.04865, 95.49206, 69.79339,
66.82354, 61.53702, 85.51962, 101.53748, 61.64096, 92.14781,
60.41892, 77.55303, 75.39007, 62.15703, 82.38292, 87.27823, 69.28644,
73.72369, 62.02667, 61.34973, 61.61716, 70.61777, 61.59655, 60.31008,
61.65777, 71.82138, 61.9823, 79.38791, 60.31816, 108.2866, 61.54543,
69.87043, 64.30304, 65.05726, 68.69304, 84.0604, 121.05592, 68.67872,
69.53102, 68.31013, 64.76675, 122.22451, 91.29598, 69.43838,
68.73515, 77.99483, 68.56142, 64.14964, 73.2695, 69.67608, 69.01853,
68.5685, 63.9001, 68.93557, 71.84125, 72.73052, 69.17045, 76.28085,
68.41978, 69.53005, 76.15901, 86.43705, 72.95997, 64.0582, 70.29805,
64.10386, 88.09503, 68.07687, 69.62147, 67.77802, 125.09757,
91.96934, 97.26108, 89.16293, 80.245, 124.97964, 97.28237, 95.95074,
99.24807, 82.59685, 89.2217, 77.50349, 88.54909, 91.61343, 77.38885,
94.06567, 84.77561, 80.46941, 92.28428, 74.25416, 97.07623),
depth = c(707.5, 195, 47.5, 750, 315.5, 506.5, 660.5, 678,
146, 821.5, 197, 365, 141.5, 448, 574.5, 325.5, 645.5, 236,
446, 872.5, 315, 729.5, 786.5, 573.5, 752, 202, 455, 412,
147, 552.5, 517, 904, 339.5, 184, 308.5, 307.5, 610, 705.5,
700, 375, 228.5, 261.5, 398.5, 168.5, 360, 136.5, 504, 299.5,
187, 473.5, 249.5, 64.5, 178.5, 251.5, 369.5, 95, 66.5, 599.5,
72, 474, 178, 33.5, 109, 86.5, 26.5, 240, 51.5, 367, 295.5,
408.5, 244.5, 106, 314, 304.5, 69.5, 361.5, 91.5, 125.5,
11, 281.5, 101.5, 472.5, 433, 257, 24, 6, 192.5, 265, 226,
449, 1.5, 21.5, 362.5, 238.5, 33, 246, 477.5, 46, 109, 315,
275.5, 636, 284.5, 267, 387, 336.5, 225.5, 291, 205, 136,
60.5, 171, 515.5, 260.5, 563, 50, 436, 123, 227, 7.5, 12,
87, 32.5, 239, 14.5, 103.5, 90.5, 120.5, 38.5, 36.5, 406,
38.5, 143.5, 371, 87, 54.5, 234, 53.5, 260.5, 123), ctd_file = c("BA16007",
"BA16002", "BA16011", "BA16003", "BA16004", "BA16006", "BA16001",
"BA16002", "BA16012", "BA16003", "BA16004", "BA16004", "BA16005",
"BA16004", "BA16009", "BA16005", "BA16012", "BA16004", "BA16006",
"BA16003", "BA16013", "BA16014", "BA16017", "BA16016", "BA16018",
"BA16016", "BA16014", "BA16020", "BA16020", "BA16020", "BA16016",
"BA16018", "BA16016", "BA16018", "BA16021", "BA16015", "BA16014",
"BA16013", "BA16015", "BA16021", "BA16025", "BA16023", "BA16024",
"BA16025", "BA16024", "BA16025", "BA16025", "BA16025", "BA16024",
"BA16025", "BA16023", "BA16025", "BA16023", "BA16025", "BA16024",
"BA16022", "BA16022", "BA16025", "BA16024", "BA16024", "BA16026",
"BA16035", "BA16030", "BA16035", "BA16031", "BA16026", "BA16032",
"BA16031", "BA16027", "BA16032", "BA16035", "BA16028", "BA16031",
"BA16027", "BA16033", "BA16032", "BA16035", "BA16028", "BA16027",
"BA16032", "BA16034", "BA16034", "BA16034", "BA16034", "BA16034",
"BA16034", "BA16034", "BA16034", "BA16034", "BA16034", "BA16034",
"BA16034", "BA16034", "BA16034", "BA16034", "BA16034", "BA16034",
"BA16034", "BA16034", "BA16034", "BA16040", "BA16036", "BA16043",
"BA16047", "BA16048", "BA16041", "BA16049", "BA16036", "BA16042",
"BA16044", "BA16048", "BA16045", "BA16039", "BA16046", "BA16036",
"BA16048", "BA16044", "BA16038", "BA16041", "BA16045", "BA16052",
"BA16055", "BA16054", "BA16054", "BA16053", "BA16053", "BA16053",
"BA16055", "BA16055", "BA16053", "BA16055", "BA16054", "BA16052",
"BA16055", "BA16054", "BA16053", "BA16054", "BA16055", "BA16052",
"BA16053"), Transect = structure(c(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, 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, 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, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L), .Label = c("1",
"2", "3", "4", "5", "6", "7"), class = "factor"), Station = structure(c(7L,
2L, 11L, 3L, 4L, 6L, 1L, 2L, 12L, 3L, 4L, 4L, 5L, 4L, 9L,
5L, 12L, 4L, 6L, 3L, 1L, 2L, 5L, 4L, 6L, 4L, 2L, 8L, 8L,
8L, 4L, 6L, 4L, 6L, 9L, 3L, 2L, 1L, 3L, 9L, 4L, 2L, 3L, 4L,
3L, 4L, 4L, 4L, 3L, 4L, 2L, 4L, 2L, 4L, 3L, 1L, 1L, 4L, 3L,
3L, 1L, 9L, 5L, 9L, 6L, 1L, 7L, 6L, 2L, 7L, 9L, 3L, 6L, 2L,
8L, 7L, 9L, 3L, 2L, 7L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 4L, 8L,
9L, 3L, 10L, 1L, 4L, 5L, 9L, 6L, 1L, 7L, 1L, 9L, 5L, 1L,
3L, 6L, 3L, 6L, 5L, 5L, 4L, 4L, 4L, 6L, 6L, 4L, 6L, 5L, 3L,
6L, 5L, 4L, 5L, 6L, 3L, 4L), .Label = c("1", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "11", "12"), class = "factor")), row.names = c(NA,
-140L), groups = structure(list(Transect = structure(1:7, .Label = c("1",
"2", "3", "4", "5", "6", "7"), class = "factor"), .rows = structure(list(
1:20, 21:40, 41:60, 61:80, 81:100, 101:120, 121:140), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -7L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
And there is the code I used:
library(dplyr)
library(tidyverse)
library(ggforce)
i <- ceiling(
length(levels(df$Transect)) / 4) # set the number of pages
pdf("multi_page.pdf", width = 16 / 2.54, height = 12 / 2.54)
SdesGG <- df %>% #launch each time or does not work
group_by(Transect) %>% #mandatory or need to fortify
ggplot(aes(x = oxygen, y = depth, color = Station)) +
geom_line() +
scale_color_brewer(palette = "Paired") +
scale_y_reverse() +
facet_wrap_paginate(~Transect, ncol = 2, nrow = 2, page = i) #ggforce
dev.off()
I have 180 000 observations, therefore it might be interesting to duplicate the number of row
lapply(df, rep, 1000)
With the page argument you only specify
The page to draw (see ?facet_wrap_paginate)
That's why you get only the last or second page with page = i.
If you want all pages you have to loop over the pages:
library(ggplot2
library(ggforce)
i <- ceiling(
length(levels(df$Transect)) / 4) # set the number of pages
pdf("multi_page.pdf", width = 16 / 2.54, height = 12 / 2.54)
lapply(seq(i), function(page) {
SdesGG <- df %>% #launch each time or does not work
group_by(Transect) %>% #mandatory or need to fortify
ggplot(aes(x = oxygen, y = depth, color = Station)) +
geom_line() +
scale_color_brewer(palette = "Paired") +
scale_y_reverse() +
facet_wrap_paginate(~Transect, ncol = 2, nrow = 2, page = page) #ggforce
})
dev.off()
#> [[1]]
#>
#> [[2]]
I didn´t found a sufficient answer in this forum yet, so I decided to raise my own question.
I want to get the linear regression equation of a linear fit from a boxplot. I have this data:
library(ggplot2)
data <- structure(list(x = 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, 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, 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, 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
), .Label = c("1", "2", "3", "4", "5", "6"), class = "factor"),
y = c(169, 79.5, 78.5, 75, 99.5, 68, 14, 30.5, 107.5, 51,
43, 33, 21.5, 35, 11, 1, 38, 54.5, 26.5, 143, 158, 171, 31.5,
67.5, 1, 57.5, 12, 36.5, 1, 23.5, 22.5, 71, 141, 218, 7.5,
1, 129, 144.5, 76, 46.5, 75.5, 45, 12, 24, 67, 65.5, 44.5,
37.5, 25.5, 19, 15, 1, 17.5, 50, 22.5, 90, 226, 220, 32,
69.5, 1, 79.5, 7, 44, 1, 15.5, 22, 75.5, 178, 153, 4.5, 1,
159, 89, 57, 71, 98.5, 47.5, 18.5, 30, 119, 57.5, 41, 33.5,
30, 31, 10, 1, 12, 43.5, 20.5, 98, 146.5, 145, 34, 64.5,
1, 40.5, 17, 41, 1, 14.5, 16.5, 71, 181, 168, 2, 1, 159,
103, 69, 65.5, 97.5, 37.5, 21, 15.5, 120.5, 46, 27, 29.5,
16.5, 20, 7.5, 1, 15.5, 42.5, 21.5, 111, 102.5, 124, 20.5,
51.5, 1, 22.5, 15, 42, 1, 13, 13.5, 64.5, 138, 155, 4.9,
1, 190, 89.5, 74.5, 79, 78, 59.5, 19.5, 21, 88.5, 44, 18,
19, 10, 13, 4, 1, 9.5, 44, 17, 140.5, 98, 112.5, 29.5, 62.56,
1, 31, 11.5, 49.5, 1, 10, 8.5, 40.5, 121, 141, 2.5, 1, 170,
87.5, 92, 77, 65, 34, 8, 26, 98, 51.5, 26, 19, 9, 8.5, 7.5,
1, 4.5, 0, 15.5, 80, 69, 59, 28, 44.5, 1, 38.5, 10, 51.5,
1, 3, 5, 65, 107, 152, 5, 1)), row.names = c(NA, -216L), class = "data.frame")
p <- ggplot(data = data) +
aes(x = x,
y = y) +
geom_boxplot(outlier.shape = NA) + geom_jitter(shape = 1, position = position_jitter(0.1)) +
ylim(0, NA) +
theme_light() +
geom_smooth(method = "lm",se = TRUE, formula = y ~ x, aes(group = 1))
print(p)
fit <- lm(y ~ x, data = data)
fit
which results in this output:
How can I extract the regression equation for this dataset? The function fit <- lm(y ~ x, data = data) just gives me one intercept and 5 coefficients, which is not my desired output. I want a simple regression equation in the form of y = a + bx.
How can I put this equation into the diagramm? I´ve already looked into ggpmisc::stat_poly_eq(), but this doesn´t seem to work with boxplot linear regression.
Can you guys help me out?
I recently updated R, and now ggpredict gives me error when calculating the predictions for my mixed effects model (using lme() function).
I want to analyse how anitoxidant activity varies between (plants at) different sites. In order to do that, I have 5 sites with 3 plots each, measured during 6 years.
I have established a linear mixed effects model (function lme()) where "anitox" is modeled against the fixed factor site, and the random factor plot.
Then I use ggpredict() (from package ggeffects) to obtain the model predictions and plot them.
It used to work fine, but since I updated R (version 3.6.0) I get an error message, which i do not know how to solve:
Error: Must use a vector in `[`, not an object of class matrix.
Do you have any idea about what the error means and how to solve the problem? I imagine it must be something with how the data are written/organized, but I do not know what.
Here is the data:
structure(list(year = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 1L, 1L, 1L, 2L, 2L,
2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 1L, 1L, 2L,
2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 1L, 1L,
1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L,
1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L,
6L, 6L), .Label = c("2011", "2012", "2013", "2014", "2015", "2016"
), class = "factor"), site = structure(c(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, 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, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L), .Label = c("Rebbenes", "Skogsfjord", "Snarby",
"Skibotn", "Gukhesjavri"), class = "factor"), plot = c(11, 12,
13, 11, 12, 13, 11, 12, 13, 11, 12, 13, 11, 12, 13, 11, 12, 13,
21, 22, 23, 21, 22, 23, 21, 22, 23, 21, 22, 23, 21, 22, 23, 21,
22, 23, 31, 32, 31, 32, 33, 31, 32, 33, 31, 32, 33, 31, 32, 33,
31, 32, 33, 41, 42, 43, 41, 42, 43, 41, 42, 43, 41, 42, 43, 41,
42, 43, 41, 42, 43, 51, 52, 53, 51, 52, 53, 51, 52, 53, 51, 52,
53, 51, 52, 53, 51, 52, 53), antiox = c(2.46653362547122, 2.47281284862989,
3.08407675482263, 2.87831523045473, 3.034372394251, 3.21454300619676,
2.74011075995272, 2.33528554925283, 3.03876785046206, 2.44794278726812,
2.31854154139051, 2.53086546956933, 2.80912124091414, 2.44954337356346,
3.28600361541029, 3.44682099175027, 1.20643110534499, 3.35829625741535,
3.05272373173726, 2.99366209743898, 3.16879701865554, 3.22166561521105,
3.17619665222095, 3.27004641403846, 3.00891075995272, 2.69110819491058,
2.56533904870318, 2.72225865388593, 2.50975004267303, 2.71080830577306,
3.21333084150044, 3.42928071324833, 3.23869397818054, 3.50487302546225,
3.45334645895437, 3.32049779644063, 2.93581321506448, 3.09215971561413,
3.02336528541992, 3.05388107875081, 3.13256220736936, 2.4930048603558,
2.43519907068925, 2.50833355218431, 2.38878206812421, 2.12290006006265,
2.21622655328361, 3.04982479532968, 3.11766502984782, 3.1409350921417,
3.70615906430432, 3.35201703425668, 3.07677775246848, 3.09648082230486,
3.10361574352142, 3.16501054015022, 2.68618331400182, 3.15280346790435,
3.17590731546756, 2.85941599996738, 2.76602794573506, 2.8732056665119,
2.9199920351451, 2.79834747669878, 2.58608510953132, 2.83503842669648,
2.73056939041945, 2.74792959562282, 3.45827133986313, 3.50868980816654,
3.71756016360809, 3.1311150472957, 2.990842126812, 3.04599463688895,
2.62511479073322, 2.66155890945803, 2.76085682078086, 2.69787990616012,
3.01615649098973, 3.04644450857859, 2.49460544665114, 2.41654608424733,
2.6073852194617, 1.64795744465177, 2.71924216432931, 2.72552138748798,
3.09217877114499, 3.14044260405082, 2.87856206172762)), row.names = c(NA,
-89L), class = c("tbl_df", "tbl", "data.frame"))
And this is the model:
m.antiox <- lme(antiox~site, random=~1|plot, data=antiox)
ggpredict(m.antiox)
Can't reproduce your error, for me, everything works fine:
library(nlme)
library(ggeffects)
snowmelt <- c(rep("early", 20), rep("mid",20), rep("late",20))
transect <- c(rep("T1",10), rep("T2",10), rep("T3",10), rep("T4",10), rep("T5",10), rep("T6",10))
plot <- c("T1_1","T1_1","T1_2","T1_2","T1_3","T1_3","T1_4","T1_4","T1_5","T1_5","T2_1","T2_1","T2_2","T2_2","T2_3","T2_3","T2_4","T2_4","T2_5","T2_5","T3_1","T3_1","T3_2","T3_2","T3_3","T3_3","T3_4","T3_4","T3_5","T3_5","T4_1","T4_1","T4_2","T4_2","T4_3","T4_3","T4_4","T4_4","T4_5","T4_5","T5_1","T5_1","T5_2","T5_2","T5_3","T5_3","T5_4","T5_4","T5_5","T5_5","T6_1","T6_1","T6_2","T6_2","T6_3","T6_3","T6_4","T6_4","T6_5","T6_5")
soillayer <- c(rep(c("shallow","deep"),30))
success <- runif(60)
plantsuccess <- data.frame(snowmelt, plot, soillayer, success)
m.success <- lme(success ~ snowmelt+soillayer, random=~1|transect/plot, data=plantsuccess)
ggpredict(m.success, c("snowmelt","soillayer"))
#>
#> # Predicted values of success
#> # x = snowmelt
#>
#> # soillayer = deep
#> x predicted std.error conf.low conf.high
#> 1 0.479 0.088 0.307 0.651
#> 2 0.599 0.088 0.427 0.771
#> 3 0.465 0.088 0.293 0.637
#>
#> # soillayer = shallow
#> x predicted std.error conf.low conf.high
#> 1 0.521 0.088 0.349 0.693
#> 2 0.642 0.088 0.470 0.814
#> 3 0.508 0.088 0.336 0.680
Created on 2019-06-10 by the reprex package (v0.3.0)
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.
This should be very simple. If I produce facet plots and I want to add the annotation_logticks, ticks appear in all the plots.
What if I want to have the ticks on only the first "columns" of the faceting? Is it possible?
For example, using the CO2 dataset:
structure(list(Plant = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 11L), .Label = c("Qn1", "Qn2", "Qn3", "Qc1", "Qc3", "Qc2",
"Mn3", "Mn2", "Mn1", "Mc2", "Mc3", "Mc1"), class = c("ordered",
"factor")), Type = 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, 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), .Label = c("Quebec",
"Mississippi"), class = "factor"), Treatment = structure(c(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, 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), .Label = c("nonchilled", "chilled"), class = "factor"),
conc = c(95, 175, 250, 350, 500, 675, 1000, 95, 175, 250,
350, 500, 675, 1000, 95, 175, 250, 350, 500, 675, 1000, 95,
175, 250, 350, 500, 675, 1000, 95, 175, 250, 350, 500, 675,
1000, 95, 175, 250, 350, 500, 675, 1000, 95, 175, 250, 350,
500, 675, 1000, 95, 175, 250, 350, 500, 675, 1000, 95, 175,
250, 350, 500, 675, 1000, 95, 175, 250, 350, 500, 675, 1000,
95, 175, 250, 350, 500, 675, 1000, 95, 175, 250, 350, 500,
675, 1000), uptake = c(16, 30.4, 34.8, 37.2, 35.3, 39.2,
39.7, 13.6, 27.3, 37.1, 41.8, 40.6, 41.4, 44.3, 16.2, 32.4,
40.3, 42.1, 42.9, 43.9, 45.5, 14.2, 24.1, 30.3, 34.6, 32.5,
35.4, 38.7, 9.3, 27.3, 35, 38.8, 38.6, 37.5, 42.4, 15.1,
21, 38.1, 34, 38.9, 39.6, 41.4, 10.6, 19.2, 26.2, 30, 30.9,
32.4, 35.5, 12, 22, 30.6, 31.8, 32.4, 31.1, 31.5, 11.3, 19.4,
25.8, 27.9, 28.5, 28.1, 27.8, 10.5, 14.9, 18.1, 18.9, 19.5,
22.2, 21.9, 7.7, 11.4, 12.3, 13, 12.5, 13.7, 14.4, 10.6,
18, 17.9, 17.9, 17.9, 18.9, 19.9)), .Names = c("Plant", "Type",
"Treatment", "conc", "uptake"), row.names = 1:84, class = c("nfnGroupedData",
"nfGroupedData", "groupedData", "data.frame"), formula = uptake ~
conc | Plant, outer = ~Treatment * Type, labels = structure(list(
x = "Ambient carbon dioxide concentration", y = "CO2 uptake rate"), .Names = c("x",
"y")), units = structure(list(x = "(uL/L)", y = "(umol/m^2 s)"), .Names = c("x",
"y")))
With this small piece of code:
ggplot(data, aes(conc, uptake))+
geom_point()+
scale_y_log10(breaks=c(1,10,100))+
facet_grid(~Type)+
annotation_logticks(sides="l")
Given the example plot
p <- ggplot(mtcars, aes(disp, hp)) +
facet_wrap(~vs) +
geom_point() +
scale_y_log10()
you can use the following "hack" to have the ticks only for facets identified by vs=c(0)
a <- annotation_logticks(sides='l')
a$data <- data.frame(x=NA, vs=c(0))
p + a
Unfortunately, it provokes a warning.