How to plot expressions within facet_grid()?

Asked

Viewed 27 times

1

I’m having trouble plotting two distinct expressions:

  • expression 1: "RN (MJ m"^2~"d"^-1~")"
  • expression 2: "ET"[0]~~" (mm"^-1*" m"^2*" )"

inside:

ggplot()+facet_grid(labeller = label_bquote()

according to the script below, Are presented only the first expression (Figure 1). I would like you to present one with RN and another to ET0.

At the opportunity, should anyone know, as I could, plot these expressions within labs(), for each one to appear in their respective grid axes, it would also solve my problem (unused ggarrange(), plot_grid(), because I need that x axis, be fixed).

Figure 1 - Example

inserir a descrição da imagem aqui

Chart script:

ggplot(exe1,aes(x=Time,y=value))+
  facet_grid(variable~.,scales="free_y",labeller = label_bquote(RN=="RN (MJ m"^2~"d"^-1~")",ET0=="ET"[0]~~" (mm"^-1*" m"^2*" )"))+
  geom_line(aes(linetype=variable,color=variable))+labs(y=c(expression("RN (MJ m"^2~"d"^-1~")"),expression("ET"[0]~~" (mm"^-1*" m"^2*" )")))

sample data:

exe1<-structure(list(Time = structure(c(1607644800, 1607644800, 1607680800, 
1607680800, 1607682600, 1607682600, 1607684400, 1607684400, 1607686200, 
1607686200, 1607688000, 1607688000, 1607689800, 1607689800, 1607691600, 
1607691600, 1607693400, 1607693400, 1607695200, 1607695200, 1607697000, 
1607697000, 1607698800, 1607698800, 1607700600, 1607700600, 1607702400, 
1607702400, 1607704200, 1607704200, 1607706000, 1607706000, 1607707800, 
1607707800, 1607709600, 1607709600, 1607711400, 1607711400, 1607713200, 
1607713200, 1607715000, 1607715000, 1607648400, 1607648400, 1607650200, 
1607650200, 1607716800, 1607716800, 1607718600, 1607718600, 1607720400, 
1607720400, 1607722200, 1607722200, 1607724000, 1607724000, 1607725800, 
1607725800, 1607727600, 1607727600, 1607729400, 1607729400, 1607652000, 
1607652000, 1607653800, 1607653800, 1607646600, 1607646600, 1607655600, 
1607655600, 1607657400, 1607657400, 1607659200, 1607659200, 1607661000, 
1607661000, 1607662800, 1607662800, 1607664600, 1607664600, 1607666400, 
1607666400, 1607668200, 1607668200, 1607670000, 1607670000, 1607671800, 
1607671800, 1607673600, 1607673600, 1607675400, 1607675400, 1607677200, 
1607677200, 1607679000, 1607679000, 1607731200, 1607731200, 1607767200, 
1607767200, 1607769000, 1607769000, 1607770800, 1607770800, 1607772600, 
1607772600, 1607774400, 1607774400, 1607776200, 1607776200, 1607778000, 
1607778000, 1607779800, 1607779800, 1607781600, 1607781600, 1607783400, 
1607783400, 1607785200, 1607785200, 1607787000, 1607787000, 1607788800, 
1607788800, 1607790600, 1607790600, 1607792400, 1607792400, 1607794200, 
1607794200, 1607796000, 1607796000, 1607797800, 1607797800, 1607799600, 
1607799600, 1607801400, 1607801400, 1607734800, 1607734800, 1607736600, 
1607736600, 1607803200, 1607803200, 1607805000, 1607805000, 1607806800, 
1607806800, 1607808600, 1607808600, 1607810400, 1607810400, 1607812200, 
1607812200, 1607814000, 1607814000, 1607815800, 1607815800, 1607738400, 
1607738400, 1607740200, 1607740200, 1607733000, 1607733000, 1607742000, 
1607742000, 1607743800, 1607743800, 1607745600, 1607745600, 1607747400, 
1607747400, 1607749200, 1607749200, 1607751000, 1607751000, 1607752800, 
1607752800, 1607754600, 1607754600, 1607756400, 1607756400, 1607758200, 
1607758200, 1607760000, 1607760000, 1607761800, 1607761800, 1607763600, 
1607763600, 1607765400, 1607765400, 1607817600, 1607817600, 1607853600, 
1607853600, 1607855400, 1607855400, 1607857200, 1607857200, 1607859000, 
1607859000, 1607860800, 1607860800, 1607862600, 1607862600, 1607864400, 
1607864400, 1607866200, 1607866200, 1607868000, 1607868000, 1607869800, 
1607869800, 1607871600, 1607871600, 1607873400, 1607873400, 1607875200, 
1607875200, 1607877000, 1607877000, 1607878800, 1607878800, 1607880600, 
1607880600, 1607882400, 1607882400, 1607884200, 1607884200, 1607886000, 
1607886000, 1607887800, 1607887800, 1607821200, 1607821200, 1607823000, 
1607823000, 1607889600, 1607889600, 1607891400, 1607891400, 1607893200, 
1607893200, 1607895000, 1607895000, 1607896800, 1607896800, 1607898600, 
1607898600, 1607900400, 1607900400, 1607902200, 1607902200, 1607824800, 
1607824800, 1607826600, 1607826600, 1607819400, 1607819400, 1607828400, 
1607828400, 1607830200, 1607830200, 1607832000, 1607832000, 1607833800, 
1607833800, 1607835600, 1607835600, 1607837400, 1607837400, 1607839200, 
1607839200, 1607841000, 1607841000, 1607842800, 1607842800, 1607844600, 
1607844600, 1607846400, 1607846400, 1607848200, 1607848200, 1607850000, 
1607850000, 1607851800, 1607851800, 1607904000, 1607904000, 1607940000, 
1607940000, 1607941800, 1607941800, 1607943600, 1607943600, 1607945400, 
1607945400, 1607947200, 1607947200, 1607949000, 1607949000, 1607950800, 
1607950800, 1607952600, 1607952600, 1607954400, 1607954400, 1607956200, 
1607956200, 1607958000, 1607958000, 1607959800, 1607959800, 1607961600, 
1607961600, 1607963400, 1607963400, 1607965200, 1607965200, 1607967000, 
1607967000, 1607968800, 1607968800, 1607970600, 1607970600, 1607972400, 
1607972400, 1607974200, 1607974200, 1607907600, 1607907600, 1607909400, 
1607909400, 1607976000, 1607976000, 1607977800, 1607977800, 1607979600, 
1607979600, 1607981400, 1607981400, 1607983200, 1607983200, 1607985000, 
1607985000, 1607986800, 1607986800, 1607988600, 1607988600, 1607911200, 
1607911200, 1607913000, 1607913000, 1607905800, 1607905800, 1607914800, 
1607914800, 1607916600, 1607916600, 1607918400, 1607918400, 1607920200, 
1607920200, 1607922000, 1607922000, 1607923800, 1607923800, 1607925600, 
1607925600, 1607927400, 1607927400, 1607929200, 1607929200, 1607931000, 
1607931000, 1607932800, 1607932800, 1607934600, 1607934600, 1607936400, 
1607936400, 1607938200, 1607938200, 1607990400, 1607990400, 1608026400, 
1608026400, 1608028200, 1608028200, 1608030000, 1608030000, 1608031800, 
1608031800, 1608033600, 1608033600, 1608035400, 1608035400, 1608037200, 
1608037200, 1608039000, 1608039000, 1608040800, 1608040800, 1608042600, 
1608042600, 1608044400, 1608044400, 1608046200, 1608046200, 1608048000, 
1608048000, 1608049800, 1608049800, 1608051600, 1608051600, 1608053400, 
1608053400, 1608055200, 1608055200, 1608057000, 1608057000, 1608058800, 
1608058800, 1608060600, 1608060600, 1607994000, 1607994000, 1607995800, 
1607995800, 1608062400, 1608062400, 1608064200, 1608064200, 1608066000, 
1608066000, 1608067800, 1608067800, 1608069600, 1608069600, 1608071400, 
1608071400, 1608073200, 1608073200, 1608075000, 1608075000, 1607997600, 
1607997600, 1607999400, 1607999400, 1607992200, 1607992200, 1608001200, 
1608001200, 1608003000, 1608003000, 1608004800, 1608004800, 1608006600, 
1608006600, 1608008400, 1608008400, 1608010200, 1608010200, 1608012000, 
1608012000, 1608013800, 1608013800, 1608015600, 1608015600, 1608017400, 
1608017400, 1608019200, 1608019200, 1608021000, 1608021000, 1608022800, 
1608022800, 1608024600, 1608024600), tzone = "GMT", class = c("POSIXct", 
"POSIXt")), variable = c("ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", "ET0", "RN", 
"ET0", "RN", "ET0", "RN"), value = c(0.254, 0.604, 17.426, 44.76, 
14.844, 37.757, 9.145, 22.915, 12.865, 32.331, 12.826, 32.556, 
13.371, 34.922, 12.459, 32.121, 12.222, 31.06, 9.636, 25.364, 
6.111, 15.646, 5.745, 15.477, 4.858, 12.902, 2.54, 6.393, 3.099, 
7.652, 3.149, 8.271, 1.122, 2.494, 0.506, 0.777, 0.351, 0.471, 
0.292, 0.377, 0.268, 0.372, 0.304, 0.798, 0.219, 0.481, 0.158, 
0.003, 0.433, 1.116, 0.493, 1.327, 0.206, 0.244, 0.179, 0.084, 
0.291, 0.525, 0.247, 0.437, 0.323, 0.789, 0.237, 0.53, 0.306, 
0.802, 0.305, 0.794, 0.312, 0.869, 0.215, 0.476, 0.291, 0.775, 
0.361, 1.099, 0.482, 1.556, 0.864, 2.754, 1.656, 4.929, 5.356, 
16.1, 4.216, 10.584, 7.469, 18.89, 6.269, 17.191, 7.824, 20.66, 
10.684, 27.327, 9.616, 25.348, 0.344, 0.827, 12.969, 32.853, 
14.133, 35.666, 17.189, 44.22, 24.602, 64.747, 25.188, 66.111, 
25.298, 66.703, 19.859, 53.29, 10.327, 27.698, 5.311, 13.6, 4.516, 
11.725, 6.491, 17.216, 11.335, 31.197, 6.906, 19.458, 2.808, 
7.262, 1.913, 4.613, 0.939, 1.622, 0.728, 1.174, 0.354, 0, 0.272, 
-0.117, 0.447, 0.642, 0.323, 0.718, 0.181, 0.177, 0.447, 0.734, 
0.354, 0.497, 0.197, -0.05, 0.329, 0.534, 0.449, 1.051, 0.281, 
0.472, 0.424, 1.053, 0.313, 0.668, 0.165, 0.098, 0.149, 0.08, 
0.384, 0.974, 0.138, 0.085, 0.14, 0.125, 0.411, 1.202, 0.331, 
0.916, 0.461, 1.375, 0.857, 2.427, 1.882, 5.515, 2.603, 7.339, 
3.179, 9.166, 6.689, 17.094, 11.37, 28.085, 9.604, 22.629, 7.732, 
18.481, 11.651, 29.369, 0.355, 0.83, 8.559, 20.714, 12.456, 30.017, 
15.535, 38.772, 11.339, 28.859, 12.111, 31.176, 15.778, 41.89, 
12.975, 34.4, 13.863, 37.399, 12.668, 34.713, 8.911, 24.444, 
6.869, 18.299, 5.49, 14.89, 3.94, 10.258, 3.038, 7.828, 2.042, 
4.867, 1.22, 2.414, 0.557, 0.35, 0.418, 0.036, 0.282, -0.276, 
0.121, -0.694, 0.375, 0.899, 0.243, 0.38, 0.206, -0.291, 0.063, 
-0.849, 0.066, -0.853, 0.091, -0.736, -0.009, -1.012, 0.038, 
-0.719, -0.036, -0.997, 0.05, -0.59, 0.285, 0.577, 0.326, 0.722, 
0.302, 0.612, 0.363, 0.905, 0.663, 2.003, 0.369, 0.933, 0.301, 
0.685, 0.796, 2.413, 0.966, 2.591, 1.635, 4.407, 4.466, 12.541, 
5.632, 14.173, 6.872, 16.84, 8.596, 20.846, 7.917, 19.086, 8.004, 
18.427, 9.217, 21.973, 0.016, -0.754, 13.159, 31.995, 11.743, 
27.81, 13.443, 33.017, 22.591, 60.104, 22.622, 60.934, 25.42, 
68.375, 24.238, 66.833, 17.473, 49.202, 11.954, 33.622, 9.176, 
25.413, 8.483, 23.186, 7.172, 19.58, 5.972, 16.061, 4.659, 11.99, 
3.329, 8.866, 1.716, 4.086, 0.496, 0.329, 0.068, -0.847, 0.294, 
0.187, 0.188, -0.183, 0.262, 0.257, 0.286, 0.359, 0.334, 0.46, 
0.343, 0.512, 0.185, -0.076, 0.414, 0.834, 0.309, 0.47, 0.182, 
0.023, 0.298, 0.465, 0.435, 0.989, 0.282, 0.406, 0.379, 0.769, 
-0.051, -0.964, 0.403, 0.919, 0.219, 0.304, 0.345, 0.813, 0.454, 
1.283, 0.429, 1.109, 0.614, 1.534, 1.08, 2.876, 4.076, 11.937, 
5.146, 13.145, 7.213, 17.548, 9.198, 21.347, 7.134, 14.881, 5.17, 
9.05, 6.278, 11.994, 0.616, 1.652, 9.144, 19.461, 10.841, 24.35, 
10.873, 24.582, 21.237, 55.214, 23.441, 62.242, 24.678, 66.54, 
24.555, 67.536, 23.108, 64.221, 20.406, 56.831, 16.27, 45.235, 
12.421, 34.655, 8.365, 23.526, 6.184, 16.819, 4.682, 12.486, 
3.225, 8.137, 1.427, 2.804, 0.578, 0.42, 0.558, 0.696, 0.425, 
0.452, 0.449, 0.596, 0.481, 1.259, 0.398, 0.984, 0.401, 0.51, 
0.312, 0.313, 0.425, 0.882, 0.348, 0.587, 0.396, 0.808, 0.337, 
0.622, 0.418, 0.977, 0.32, 0.643, 0.364, 0.898, 0.483, 1.421, 
0.413, 0.964, 0.384, 1.089, 0.462, 1.399, 0.357, 1.003, 0.478, 
1.503, 0.527, 1.56, 1.022, 3, 1.442, 4.317, 3.571, 10.457, 4.691, 
12.179, 6.775, 16.047, 8.763, 20.5, 7.361, 14.889, 3.531, 3.792, 
3.142, 2.564)), class = "data.frame", row.names = c(NA, -480L
))

1 answer

3


For each facet to have different Labels, one can create a variable var class "factor" with the desired honeys.
To make the code of ggplot more readable, this is done before with a pipe dplyr.

library(ggplot2)
library(dplyr)

lbls <- c(
  expression("RN (MJ m"^2~"d"^-1~")"), 
  expression("ET"[0]~~" (mm"^-1*" m"^2*" )")
)
lvls <- c(
  expression(paste(RN==RN~(MJ~m^2~~d^-1))),
  expression(paste(ET==ET[0]~(mm^-1*m^2)))
)

exe1 %>%
  mutate(var = factor(variable, labels = lvls)) %>% 
  ggplot(aes(x = Time, y = value)) +
  geom_line(aes(linetype = variable, color = variable)) +
  labs(y = lbls) +
  facet_grid(
    var ~ .,
    scales = "free_y",
    labeller = label_parsed
  )

inserir a descrição da imagem aqui

  • Thank you very much! , i an earlier example for this problem, I had even tried to put the formulas within the date.frame, but when I used Parsed, it gave nothing kkkk.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.