Related
My database contains 2 measurement periods on the same monitoring plots. On each monitoring plot we measure the volume and area of deadwood and number of tree species regeneration rejuvenated on deadwood. The deadwood is divided into 3 degrees of decomposition. I need to convert wider table to a longer one.
After reading many posts here I am almost sure that the solution is in the tidyr package and pivot_longer function. I will be very grateful for your help.
picture of dw_wider table and dw_longer table
here is the code for wider table:
dw_wider <- structure(list(Period = c(
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L),
DwType = c(
"1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps"),
Plot = c(
101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L),
m3ha_dc1 = c(
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
m3ha_dc2 = c(
185.9886004,
36.14392074, 13.09592051, 61.99620013, 12.04797358, 4.365306836,
399.9427251, 187.306535, 59.38662, 133.3142417, 62.43551166,
19.79554),
m3ha_dc3 = c(
0.160849544, 16.04894292, 10.76337767,
0.053616515, 5.349647639, 3.587792556, 75.0880521, 27.88578286,
32.00025432, 25.0293507, 9.295260953, 10.66675144),
m2ha_dc1 = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), m2ha_dc2 = c(669.9940464,
255.4172853, 147.8409739, 167.4985116, 63.85432132, 36.96024346,
1697.643681, 734.7850821, 303.60205, 424.4109202, 183.6962705,
75.9005125),
m2ha_dc3 = c(
0.40212386, 74.99540121, 87.88647158,
0.100530965, 18.7488503, 21.97161789, 297.8308267, 148.4095964,
132.5277239, 74.45770669, 37.10239909, 33.13193096),
pcsm2_dc1 = c(
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
pcsm2_dc2 = c(
0.895530346, 0, 0, 0, 0.326626119, 0.065875708, 1.432848553, 0, 0, 0, 0.544376866, 0.263502832),
pcsm2_dc3 = c(
0, 0, 12.06101441, 0, 1.212859575, 0.30182364, 0, 0, 18.20530477, 0, 2.1561948, 0.60364728)),
class = "data.frame", row.names = c(NA,
-12L))
and here is the result I want to achieve:
dw_longer <- structure(list(Period = c(
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L),
DwType = c(
"1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps", "2_stumps", "2_stumps"),
Plot = c(
101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 03L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L, 101L, 102L, 103L),
dc = 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),
m3ha = c(
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 185.9886004, 36.14392074, 13.09592051, 61.99620013, 12.04797358, 4.365306836, 399.9427251, 187.306535, 59.38662, 133.3142417, 62.43551166, 19.79554, 0.160849544, 16.04894292, 10.76337767, 0.053616515, 5.349647639, 3.587792556, 75.0880521, 27.88578286, 32.00025432, 25.0293507, 9.295260953, 10.66675144),
m2ha = c(
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 669.9940464, 255.4172853, 147.8409739, 167.4985116, 63.85432132, 36.96024346, 1697.643681, 734.7850821, 303.60205, 424.4109202, 183.6962705, 75.9005125, 0.40212386, 74.99540121, 87.88647158, 0.100530965, 18.7488503, 21.97161789, 297.8308267, 148.4095964, 132.5277239,
74.45770669, 37.10239909, 33.13193096),
pcsha = c(
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.895530346, 0, 0, 0, 0.326626119,
0.065875708, 1.432848553, 0, 0, 0, 0.544376866, 0.263502832, 0, 0, 12.06101441, 0, 1.212859575, 0.30182364, 0, 0, 18.20530477, 0, 2.1561948, 0.60364728)),
class = "data.frame", row.names = c(NA, -36L))
Try this:
library(tidyverse)
dw_wider <- structure(list(Period = c(
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L
), DwType = c(
"1_trunks", "1_trunks", "1_trunks", "2_stumps",
"2_stumps", "2_stumps", "1_trunks", "1_trunks", "1_trunks", "2_stumps",
"2_stumps", "2_stumps"
), Plot = c(
101L, 102L, 103L, 101L, 102L,
103L, 101L, 102L, 103L, 101L, 102L, 103L
), m3ha_dc1 = c(
0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), m3ha_dc2 = c(
185.9886004,
36.14392074, 13.09592051, 61.99620013, 12.04797358, 4.365306836,
399.9427251, 187.306535, 59.38662, 133.3142417, 62.43551166,
19.79554
), m3ha_dc3 = c(
0.160849544, 16.04894292, 10.76337767,
0.053616515, 5.349647639, 3.587792556, 75.0880521, 27.88578286,
32.00025432, 25.0293507, 9.295260953, 10.66675144
), m2ha_dc1 = c(
0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), m2ha_dc2 = c(
669.9940464,
255.4172853, 147.8409739, 167.4985116, 63.85432132, 36.96024346,
1697.643681, 734.7850821, 303.60205, 424.4109202, 183.6962705,
75.9005125
), m2ha_dc3 = c(
0.40212386, 74.99540121, 87.88647158,
0.100530965, 18.7488503, 21.97161789, 297.8308267, 148.4095964,
132.5277239, 74.45770669, 37.10239909, 33.13193096
), pcsm2_dc1 = c(
0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), pcsm2_dc2 = c(
0.895530346,
0, 0, 0, 0.326626119, 0.065875708, 1.432848553, 0, 0, 0, 0.544376866,
0.263502832
), pcsm2_dc3 = c(
0, 0, 12.06101441, 0, 1.212859575,
0.30182364, 0, 0, 18.20530477, 0, 2.1561948, 0.60364728
)), class = "data.frame", row.names = c(
NA,
-12L
))
dw_wider |>
pivot_longer(-c(Period, Plot, DwType), names_pattern = "(.*)_dc(.+)", names_to = c("col", "dc")) |>
pivot_wider(names_from = col, values_from = value) |>
arrange(dc, Period, Plot, DwType)
#> # A tibble: 36 × 7
#> Period DwType Plot dc m3ha m2ha pcsm2
#> <int> <chr> <int> <chr> <dbl> <dbl> <dbl>
#> 1 1 1_trunks 101 1 0 0 0
#> 2 1 2_stumps 101 1 0 0 0
#> 3 1 1_trunks 102 1 0 0 0
#> 4 1 2_stumps 102 1 0 0 0
#> 5 1 1_trunks 103 1 0 0 0
#> 6 1 2_stumps 103 1 0 0 0
#> 7 2 1_trunks 101 1 0 0 0
#> 8 2 2_stumps 101 1 0 0 0
#> 9 2 1_trunks 102 1 0 0 0
#> 10 2 2_stumps 102 1 0 0 0
#> # … with 26 more rows
Created on 2022-05-07 by the reprex package (v2.0.1)
I have measured soil organic matter (SOM) content for different soil horizons (i.e. different soil layers). Now I would like to plot the SOM content for the different horizons in a horizontal way, for each soil profile (i.e. each spot where I dug a hole and took soil samples) individually. I'll just add a sketch to describe what kind of plot I'd like to have:
I'd like to have the bars representing the SOM content, and want their left and right border to represent the top and bottom end of each horizon, respectively - so the bar width would illustrate the thickness of each horizon (soil layer). Basically, this would give me a soil profile, just in a horizontal instead of a vertical order.
Now, as I have a total of 24 profiles with different soil types, I would probably use facet_grid or facet_wrap to plot each soil profile individually. However, I'm quite lost how to do the rest. Does anybody have suggestions? I got stuck after a first trial with a subset of my data:
barplot <- ggplot(TSM1, aes(x=top, y=SOM)) + geom_col()
I'll attach a data subset with data of 3 soil profiles here below:
structure(list(tmp_id = 1:19, Profil = structure(c(10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L,
12L, 12L, 12L, 12L), .Label = c("TNH1", "TNH2", "TNH3", "TNM1",
"TNM2", "TNM3", "TSH1", "TSH2", "TSH3", "TSM1", "TSM2", "TSM3",
"VNH1", "VNH2", "VNH3", "VNM1", "VNM2", "VNM3", "VSH1", "VSH2",
"VSH3", "VSM1", "VSM2", "VSM3"), class = "factor"), site = structure(c(4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L), .Label = c("TNH", "TNM", "TSH", "TSM", "VNH", "VNM",
"VSH", "VSM"), class = "factor"), elevation = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("high", "mid"), class = "factor"), exposition = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("north", "south"), class = "factor"), region = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("ticino", "valais"), class = "factor"), Horizon = structure(c(42L,
24L, 18L, 19L, 41L, 11L, 12L, 42L, 24L, 26L, 40L, 20L, 42L, 24L,
18L, 19L, 44L, 1L, 17L), .Label = c("[IICsi]", "[nOL-vOL]", "[nOL]-vOL-S*",
"[OF]", "[vOL-S*]", "A", "A-S1", "Acaho", "bryOF", "bryOH", "Csi",
"Dsi", "g", "IIA", "IICsi", "IICsi&Dsi", "IIDsi", "ligOF", "ligOH",
"Msi", "nOL-[vOL]-[S*]", "nOL-vOL", "nOL-vOL-[S*]", "OF", "OF-[OH]",
"OH", "rhiA", "rhiA-S1", "rhiA1", "rhiA2", "rhiAho", "rhiAho-Csi",
"rhiOF", "rhiOH", "S", "S1", "S2", "Sal", "Sal-Csi", "sgA", "sgAho",
"vOL", "Xp", "Xp&[Aho]", "Xpcaho", "Xpcaho "), class = "factor"),
Horizon_order = c(0L, 0L, 0L, 0L, 1L, 2L, 3L, 0L, 0L, 0L,
1L, 2L, 0L, 0L, 0L, 0L, 1L, 2L, 3L), Horizon_type = structure(c(1L,
2L, 4L, 8L, 10L, 15L, 16L, 1L, 2L, 6L, 10L, 16L, 1L, 2L,
4L, 8L, 12L, 15L, 16L), .Label = c("H1", "H2", "H2a", "H2b",
"H2c", "H3", "H3a", "H3b", "H3c", "H4", "H4a", "H4b", "H5",
"H5b", "H6", "H7", "H7b", "H8"), class = "factor"), Sol_ref = structure(c(7L,
7L, 7L, 7L, 7L, 7L, 7L, 13L, 13L, 13L, 13L, 13L, 8L, 8L,
8L, 8L, 8L, 8L, 8L), .Label = c("ALOCRISOL TYPIQUE", "BRUNISOL DYSTRIQUE",
"BRUNISOL DYSTRIQUE à horizon rédoxique de profondeur", "COLLUVIOSOL",
"COLLUVIOSOL brunifié", "COLLUVIOSOL leptique", "ORGANOSOL",
"ORGANOSOL (holorganique)", "ORGANOSOL holorganique", "ORGANOSOL INSATURE à COLLUVIOSOL",
"ORGANOSOL SATURE à COLLUVIOSOL", "PEYROSOL pierrique humifère calcaire",
"REGOSOL d'apport"), class = "factor"), Type_sol = structure(c(4L,
4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 6L, 6L, 6L, 4L, 4L, 4L, 4L,
4L, 4L, 4L), .Label = c("ALOCRISOL", "BRUNISOL", "COLLUVIOSOL",
"ORGANOSOL", "PEYROSOL", "REGOSOL"), class = "factor"), Type_humus = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L), .Label = c("Dysmull", "Humimor", "ligno", "ligno Humimor",
"ligno/bryo", "Mésomull", "Oligomull", "rhizo Dysmoder",
"rhizo Eumoder", "rhizo Hémimoder", "rhizo Mésomull", "rhizo Oligomull"
), class = "factor"), N_deca = c(NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_), C_deca = c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), N = c(NA, NA, NA, NA, 0.33, 0.16, NA, NA, NA, NA, 0.25,
0.09, NA, NA, NA, NA, 0.56, 0.21, NA), C = c(NA, NA, NA,
NA, 9.95, 3.16, NA, NA, NA, NA, 6.29, 2.23, NA, NA, NA, NA,
11.65, 4.93, NA), C.N = c(NA, NA, NA, NA, 30.56, 20.26, NA,
NA, NA, NA, 24.87, 23.97, NA, NA, NA, NA, 20.82, 23.43, NA
), HR = c(NA, NA, NA, NA, 2.6, 1.79, NA, NA, NA, NA, 2.58,
1.91, NA, NA, NA, NA, 4.28, 4.42, NA), PAF = c(NA, NA, NA,
NA, 16.97, 6.71, NA, NA, NA, NA, 13.34, 5.44, NA, NA, NA,
NA, 22.11, 10.06, NA), SOM = c(NA, NA, NA, NA, 8.49, 3.35,
NA, NA, NA, NA, 6.67, 2.72, NA, NA, NA, NA, 11.06, 5.03,
NA), Argile = c(NA, NA, NA, NA, 3.78, 3.75, NA, NA, NA, NA,
3.05, 5.33, NA, NA, NA, NA, 3.78, 7.08, NA), Limon = c(NA,
NA, NA, NA, 32.76, 27.26, NA, NA, NA, NA, 28.44, 40.9, NA,
NA, NA, NA, 33.2, 47.65, NA), Sable = c(NA, NA, NA, NA, 63.47,
68.99, NA, NA, NA, NA, 68.51, 53.77, NA, NA, NA, NA, 63.02,
45.26, NA), pH = c(NA, NA, NA, NA, 3.76, 4.11, NA, NA, NA,
NA, 4.29, 4.94, NA, NA, NA, NA, 3.94, 4.8, NA), CEC_tot = c(NA,
NA, NA, NA, 8.43, 4.21, NA, NA, NA, NA, 7.83, 1.24, NA, NA,
NA, NA, 11.87, 6.1, NA), CO3 = c(NA, NA, NA, NA, 0.8, 0.89,
NA, NA, NA, NA, 0.88, 0.96, NA, NA, NA, NA, 0.82, 0.91, NA
), K = c(NA, NA, NA, NA, 0.02, 0.02, NA, NA, NA, NA, 0.02,
0.02, NA, NA, NA, NA, 0.03, 0.02, NA), Mg = c(NA, NA, NA,
NA, 0, 0, NA, NA, NA, NA, 0.01, 0.01, NA, NA, NA, NA, 0.01,
0.01, NA), Ca = c(NA, NA, NA, NA, 0, 0, NA, NA, NA, NA, 0,
0, NA, NA, NA, NA, 0, 0, NA), Na = c(NA, NA, NA, NA, 0.01,
0.01, NA, NA, NA, NA, 0.02, 0.02, NA, NA, NA, NA, 0.02, 0.02,
NA), CEC.T = c(NA, NA, NA, NA, 4.84, 1.14, NA, NA, NA, NA,
21.51, 29.74, NA, NA, NA, NA, 19.09, 19.55, NA), Munsell = structure(c(NA,
NA, 24L, 22L, 8L, 11L, NA, NA, NA, 5L, 9L, 14L, NA, NA, NA,
22L, 9L, 11L, NA), .Label = c("10YR", "10YR2/1", "10YR2/2",
"10YR2/2 foncé", "10YR3/3", "10YR3/4", "10YR3/6", "10YR4/2",
"10YR4/3", "10YR4/4", "10YR4/6", "10YR5/4", "10YR5/6", "10YR5/8",
"2.5Y3/2", "2.5Y4/1", "2.5YR4/1", "5YR2.5/1", "5YR3/3", "7.5YR",
"7.5YR2.5/2", "7.5YR2.5/3", "7.5YR3/1", "7.5YR3/3", "7.5YR4/2",
"7.5YR5/6"), class = "factor"), hue = structure(c(NA, NA,
5L, 5L, 1L, 1L, NA, NA, NA, 1L, 1L, 1L, NA, NA, NA, 5L, 1L,
1L, NA), .Label = c("10YR", "2.5Y", "2.5YR", "5YR", "7.5YR"
), class = "factor"), value = c(NA, NA, 3L, 3L, 4L, 4L, NA,
NA, NA, 3L, 4L, 5L, NA, NA, NA, 3L, 4L, 4L, NA), chroma = c(NA,
NA, 3L, 3L, 2L, 6L, NA, NA, NA, 3L, 3L, 8L, NA, NA, NA, 3L,
3L, 6L, NA), top = c(1.25, 0, 1, 5, 19, 23, 33, 2.5, 0, 3.5,
6, 8.5, 2.5, 0, 2, 6, 13, 31, 38.5), bottom = c(0, 1, 5,
19, 23, 33, NA, 0, 3.5, 6, 8.5, NA, 0, 2, 6, 13, 31, 38.5,
NA), Classe = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Caricetea curvulae",
"Fraxino-Fagetea", "Vaccinio-Picetea"), class = "factor"),
Alliance = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Abieti-Piceion",
"Caricion curvulae", "Dicrano-Pinion", "Fagion", "Nardion strictae",
"Rhododendro-Vaccinion", "Vaccinio-Piceion"), class = "factor"),
Association = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Abieti-Fagetum",
"Adenostylo-Abietetum", "Calluno-Pinetum", "Carici curvulae-Nardetum",
"Geo montani-Nardetum", "Homogyno-Piceetum", "Hypochoerido-Nardetum",
"Larici-Piceetum", "Rhododendro ferruginei-Vaccinietum"), class = "factor"),
Localite = structure(c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("Campo_tencia",
"Dalpe", "La Forcle", "La Tourche", "Osco", "Predelp", "Valassier",
"Valerette"), class = "factor"), Canton = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = c("Tessin", "Valais-Vaud"), class = "factor"),
Exposition = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("North",
"South"), class = "factor"), Altitude = structure(c(2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("High", "Mid"), class = "factor"), Profil.1 = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L), Latitude = c(702521L, 702521L, 702521L, 702521L,
702521L, 702521L, 702521L, 702762L, 702762L, 702762L, 702762L,
702762L, 702929L, 702929L, 702929L, 702929L, 702929L, 702929L,
702929L), Longitude = c(150999L, 150999L, 150999L, 150999L,
150999L, 150999L, 150999L, 150835L, 150835L, 150835L, 150835L,
150835L, 150630L, 150630L, 150630L, 150630L, 150630L, 150630L,
150630L), Slope = c(20L, 20L, 20L, 20L, 20L, 20L, 20L, 35L,
35L, 35L, 35L, 35L, 20L, 20L, 20L, 20L, 20L, 20L, 20L)), row.names = c(NA,
19L), class = "data.frame")
Relevant variables/factors:
Profil = unique name/identifier for each profile
SOM = soil organic matter (in %)
top = top margin of each soil horizon (soil layer)
bottom = bottom margin of each soil horizon
It seems to me that what you're describing isn't a typical bar plot, but what you describe can be done with geom_rect().
library(ggplot2)
# df <- structure(...) # omitted for brevity
ggplot(df, aes(x = top, y = SOM)) +
geom_rect(
aes(xmin = bottom, xmax = top, ymin = 0, ymax = SOM)
) +
facet_wrap(~ Profil)
#> Warning: Removed 14 rows containing missing values (geom_rect).
I have an issue with aligning categorical x axis labels using facet_grid
Its out of alignment and I cannot figure out how to force it to align.
my code:
ggplot(data=dataset, aes(x=Question)) +
facet_grid(~Indicator,scales = "free_x")+
scale_x_discrete(labels = function(x) lapply(strwrap(x, width = 45, simplify = FALSE),paste, collapse="\n"))+
coord_cartesian(ylim = c(20, 90))+
geom_line(aes(y = Result))+ geom_point(aes(y = Result))+
geom_line(aes(y = Result_2))+ geom_point(aes(y = Result_2))+
theme_minimal()+
theme(
axis.title.x=element_blank(),
axis.text.x=element_text(angle = 90, hjust = 0, vjust = 0.5))
Data:
structure(list(Question = structure(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, 32L, 33L, 34L, 35L, 36L, 37L,
38L, 39L, 27L, 28L, 29L, 30L, 31L, 40L, 44L, 45L, 46L, 47L, 48L,
49L, 50L, 51L, 41L, 42L, 43L), .Label = c("A.1 long label", "A.2 long label",
"A.3 long label", "A.4 long label", "A.5 long label", "A.6 long label",
"A.7 long label", "A.8 long label", "B.1 very long label", "B.2 very long label",
"B.3 very long label", "B.4 very long label", "B.5 very long label",
"B.6 very long label", "B.7 very long label", "B.8 very long label",
"C.1 very long label", "C.2 very long label", "C.3 very long label",
"C.4 very long label", "C.5 very long label", "C.6 very long label",
"C.7 very long label", "C.8 very long label", "C.9 very long label",
"D.1 very very long label", "D.10 very very long label", "D.11 very very long label",
"D.12 very very long label", "D.13 very very long label", "D.14 very very long label",
"D.2 very very long label", "D.3 very very long label", "D.4 very very long label",
"D.5 very very long label", "D.6 very very long label", "D.7 very very long label",
"D.8 very very long label", "D.9 very very long label", "E.1 very very long label",
"E.10 very very long label", "E.11 very very long label", "E.12 very very long label",
"E.2 very very long label", "E.3 very very long label", "E.4 very very long label",
"E.5 very very long label", "E.6 very very long label", "E.7 very very long label",
"E.8 very very long label", "E.9 very very long label"), class = "factor"),
Indicator = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 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), .Label = c("A",
"B", "C", "D", "E"), class = "factor"), Index = c(47L, 44L,
48L, 46L, 50L, 45L, 49L, 51L, 37L, 36L, 38L, 39L, 40L, 41L,
43L, 42L, 30L, 28L, 32L, 27L, 29L, 31L, 33L, 34L, 35L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 23L, 13L, 22L, 25L, 26L,
24L, 4L, 5L, 2L, 3L, 1L, 7L, 9L, 8L, 10L, 12L, 6L, 11L),
Result = c(67.1, 62.9, 50.7, 59, 56.4, 50.4, 41.6, 65.9,
82.4, 84.8, 83.2, 87.1, 85.6, 79.3, 86.8, 78.9, 62, 73.7,
70.3, 81, 77, 55.1, 64.2, 62.2, 64.1, 62.7, 64.8, 62.3, 60.5,
49.4, 50.6, 72.6, 43.7, 49.3, 73.4, 56, 54.4, 56.1, 58.4,
72.1, 66.7, 78.2, 79.6, 80.7, 65.6, 59.5, 66.4, 53.5, 68,
76.3, 70.8), Result_2 = c(54.8, 61.4, 52.1, 58.5, 63.8, 47.4,
50.4, 69.8, 77.7, 79.3, 78.8, 82.1, 80.3, 72.6, 84.1, 74.2,
59.8, 68.6, 65.3, 75.8, 71.1, 49.1, 61.5, 53.3, 55.4, 54.4,
57.2, 57.5, 53.7, 41.2, 40.2, 65, 31.2, 37.3, 62.2, 47.3,
45.2, 47.2, 42.8, 65.5, 58.7, 70.6, 72.5, 73.9, 57.5, 52.6,
60.7, 44.3, 63.6, 69.7, 63.5)), class = "data.frame", row.names = c(NA,
-51L))
The labels are seem to be stepped no matter how I seem to format them
As per request - using the moderate hack from my other answer
I have also changed your plot code mildly. I'd suggest to have a look and understand what the advantages are - most importantly bringing the data into long format. Other convenience based changes was to define the aesthetics in the ggplot main call.
the data frame for the geom_text call needs the same values/ variabel for faceting !
May I also make a suggestion - if every x for each of your categories has the same long label - why don't you just bring this label into the facet strip, and then each x can have only the index as a label. long x labels don't really help visualisation. Long labels are generally much easier to read if you flip the graph, so they would be horizontal, but in most cases one really doesn't need long x labels.
library(tidyverse)
ann_x <- data.frame(Question = unique(dataset$Question), Indicator = str_sub(unique(dataset$Question), 1, 1))
dataset_l <- dataset %>% pivot_longer(names_to = "key", values_to = "value", cols = contains("result"))
ggplot(data = dataset_l, aes(x = Question, y = value)) +
geom_line(aes(group = key)) +
geom_point() +
geom_text(data = ann_x, aes(y = -35, label = Question), angle = 90, hjust = 0, size = 5 / 14 * 10) +
facet_grid(~Indicator, scales = "free_x") +
coord_cartesian(ylim = c(20, 90), clip = "off") +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
plot.margin = margin(0.1, 0.1, 2, 0.1, "in")
)
Created on 2020-03-15 by the reprex package (v0.3.0)
I am trying to plot these one graph to show change of length at three different sites over time as temperature increases.
I shortened the temperature data frame to match the length data to create this plot: Length/Temperature vs Time
with this set of data:
structure(list(month = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L), .Label = c("Jan",
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec"), class = c("ordered", "factor")), site = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L), .Label = c("port", "bluff", "palme"), class = "factor"),
mean = c(15.4066666666667, 14.7866666666667, 0, 38.3066666666667,
40.31, 52.06, 93.6266666666667, 84.6233333333333, 91.1083333333333,
51.6, 78.2766666666667, 96.9466666666667, 15.9633333333333,
59.1333333333333, 42.3233333333333, 10.3033333333333, 0.1,
0.1), sd = c(6.10053323465469, 5.62921770355553, 0, 14.1369211722314,
18.7262925984499, 18.0693224497518, 34.0724297822208, 43.6832697662534,
45.0430332232857, 26.4636380327995, 36.7493624934932, 44.660549074974,
6.0898719387098, 18.5524853495072, 16.3840478206244, 5.80234692905013,
0, 0), n = c(30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L), se = c(1.11379988847678,
1.02774983911493, 0, 2.58103687323451, 3.41893762487101,
3.29899183485452, 6.22074612691106, 7.9754374121868, 8.22369511828274,
4.83157716807198, 6.70948493720683, 8.15386338630991, 1.11185341104969,
3.38720490790306, 2.99130419153311, 1.05935876650386, 0,
0), tmp = c(18.893625, 18.893625, 18.893625, 19.7045148809524,
19.7045148809524, 19.7045148809524, 20.6929475806452, 20.6929475806452,
20.6929475806452, 23.8039069444444, 23.8039069444444, 23.8039069444444,
25.7307553763441, 25.7307553763441, 25.7307553763441, 28.3008958333333,
28.3008958333333, 28.3008958333333)), class = "data.frame", row.names = c(7L,
8L, 9L, 4L, 5L, 6L, 13L, 14L, 15L, 1L, 2L, 3L, 16L, 17L, 18L,
10L, 11L, 12L))
Plot:
library(ggplot2)
ggplot(df3, aes(x=month, y=mean)) +
geom_bar(aes(fill=site), stat="identity", color="black", position=position_dodge()) +
geom_line(aes(x=month, y=tmp*3, group=1), size=1) +
scale_y_continuous(sec.axis = sec_axis(~./3, name = "Temperature"))
But I would like to produce a plot with the complete temperature data set that is much longer than the length data set, hence to superimpose the two plots below:
Length vs Time
structure(list(month = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L,
3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L), .Label = c("Jan",
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
"Nov", "Dec"), class = c("ordered", "factor")), site = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L), .Label = c("port", "bluff", "palme"), class = "factor"),
mean = c(15.4066666666667, 14.7866666666667, 0, 38.3066666666667,
40.31, 52.06, 93.6266666666667, 84.6233333333333, 91.1083333333333,
51.6, 78.2766666666667, 96.9466666666667, 15.9633333333333,
59.1333333333333, 42.3233333333333, 10.3033333333333, 0.1,
0.1), sd = c(6.10053323465469, 5.62921770355553, 0, 14.1369211722314,
18.7262925984499, 18.0693224497518, 34.0724297822208, 43.6832697662534,
45.0430332232857, 26.4636380327995, 36.7493624934932, 44.660549074974,
6.0898719387098, 18.5524853495072, 16.3840478206244, 5.80234692905013,
0, 0), n = c(30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L), se = c(1.11379988847678,
1.02774983911493, 0, 2.58103687323451, 3.41893762487101,
3.29899183485452, 6.22074612691106, 7.9754374121868, 8.22369511828274,
4.83157716807198, 6.70948493720683, 8.15386338630991, 1.11185341104969,
3.38720490790306, 2.99130419153311, 1.05935876650386, 0,
0)), row.names = c(7L, 8L, 9L, 4L, 5L, 6L, 13L, 14L, 15L,
1L, 2L, 3L, 16L, 17L, 18L, 10L, 11L, 12L), class = "data.frame")
Temperature vs Time
structure(list(month = structure(c(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), class = "Date"), tmp = c(18.893625, 18.5962083333333,
18.6715416666667, 18.9369583333333, 19.3453333333333, 19.274,
19.5038333333333, 19.6745833333333, 19.6940833333333, 19.8091666666667,
19.706125, 19.40075, 19.46825, 19.6822083333333, 19.8845416666667,
19.8647916666667, 20.1585, 20.1541666666667, 19.6780416666667,
19.8689583333333, 20.44425, 20.551375, 20.29725, 19.876625, 19.6822083333333,
19.428375, 19.7496666666667, 19.9916666666667, 20.3329583333333,
20.6546666666667, 20.328875, 20.5115833333333, 20.2219583333333,
20.2930833333333, 20.4754583333333, 20.0629166666667, 19.8211666666667,
19.7100416666667, 19.4284583333333, 19.6745, 20.1306666666667,
20.301375, 20.01925, 19.7935416666667, 19.828875, 20.015375,
20.0948333333333, 20.7105416666667, 21.1350833333333, 21.613875,
22.1526666666667, 21.5527916666667, 20.9920833333333, 21.0596666666667,
21.3459166666667, 21.4136666666667, 21.7238333333333, 22.4411666666667,
22.0822916666667, 21.8911666666667, 21.5847916666667, 21.7676666666667,
22.0030833333333, 21.9430833333333, 22.4825416666667, 23.0897083333333,
23.5414583333333, 23.82, 24.0025833333333, 23.98625, 24.5325416666667,
23.6770416666667, 23.136, 23.3080416666667, 23.1600833333333,
22.7844583333333, 23.293375, 23.220125, 23.1320833333333, 23.0760833333333,
23.7297916666667, 24.1209166666667, 24.9144583333333, 25.6327083333333,
26.0205833333333, 26.126, 25.4773333333333, 25.1097916666667,
25.6192916666667, 25.8253333333333, 25.7808333333333, 25.137125,
25.0000416666667, 24.7380416666667, 24.4359166666667, 24.1663333333333,
24.110125, 23.913625, 24.0101666666667, 24.3034583333333, 24.6014583333333,
24.9077916666667, 25.0850833333333, 25.6234583333333, 25.9030416666667,
26.968125, 26.897875, 27.5782916666667, 27.9786666666667, 27.61375,
26.67575, 26.3882083333333, 26.0404166666667, 25.8984166666667,
26.2809583333333, 26.1988333333333, 26.3817916666667, 26.46325,
26.382, 26.0689166666667, 26.1216666666667, 26.6195, 26.859625,
27.0225833333333, 27.2107083333333, 27.810125, 28.0363333333333,
28.48275, 28.514375, 28.3447916666667, 28.0068333333333, 27.5096666666667,
27.5627083333333, 27.0342083333333, 27.6620416666667, 27.9710833333333,
27.953875, 27.936625, 28.21675, 28.439875, 29.0535833333333,
29.439625, 29.6357083333333, 29.4480833333333, 29.015625, 28.4232916666667,
28.386125, 28.8215416666667, 29.5276666666667, 29.602625, 30.4785416666667,
29.8260416666667, 29.632625, 29.2770416666667, 29.3150416666667,
29.6074583333333, 29.8493333333333, 29.778125), lgt = c(147.29012345679,
11.8685956790123, 13.272299382716, 39.9496913580247, 99.766049382716,
39.0691358024691, 25.5725308641975, 25.8137345679012, 8.34699074074074,
3.13125, 2.07638888888889, 2.64945987654321, 10.3154320987654,
15.0579475308642, 17.3253086419753, 5.01666666666667, 9.97484567901235,
4.50987654320988, 0.490046296296296, 10.1826388888889, 84.2596450617284,
24.7587191358025, 18.7372685185185, 0.431944444444444, 3.09814814814815,
2.27561728395062, 1.17939814814815, 2.3420524691358, 14.7756172839506,
5.03317901234568, 2.97345679012346, 10.9051697530864, 9.20262345679012,
5.7971450617284, 2.7158950617284, 1.27893518518519, 2.15941358024691,
1.35378086419753, 7.41674382716049, 15.3153549382716, 45.1071759259259,
8.91180555555556, 0.847145061728395, 2.97337962962963, 5.51496913580247,
6.80239197530864, 9.55956790123457, 19.1108796296296, 12.7988425925926,
100.853858024691, 103.503472222222, 1.48672839506173, 1.52824074074074,
7.30887345679012, 12.8653549382716, 8.03966049382716, 14.7921296296296,
9.89174382716049, 3.88703703703704, 3.20594135802469, 3.16442901234568,
6.14606481481481, 13.2888888888889, 10.797299382716, 93.5950617283951,
67.7730709876543, 140.919984567901, 58.2300925925926, 39.8996913580247,
34.9829475308642, 13.9283179012346, 5.21597222222222, 2.43371913580247,
4.75910493827161, 6.20424382716049, 5.37368827160494, 31.4861882716049,
8.70424382716049, 8.67098765432099, 5.43179012345679, 70.7297067901235,
58.6121141975309, 55.7964506172839, 86.9338734567901, 18.9948302469136,
19.418287037037, 9.50162037037037, 22.1922839506173, 25.4814043209877,
10.3818672839506, 54.5672067901235, 14.6177469135802, 38.5707561728395,
35.9960648148148, 6.78557098765432, 15.3651234567901, 11.5861111111111,
13.6625, 57.0010030864198, 73.3876543209877, 89.4920524691358,
124.690817901235, 29.2520833333333, 166.451080246914, 101.327237654321,
252.703935185185, 280.303240740741, 515.025694444444, 523.862731481481,
107.548225308642, 74.7662037037037, 264.074228395062, 16.6194444444444,
26.8019290123457, 116.119598765432, 38.2054012345679, 47.7068672839506,
53.346450617284, 17.9067901234568, 53.8114969135802, 60.1318672839506,
191.243055555556, 164.881172839506, 99.0101080246914, 78.977237654321,
163.701697530864, 139.366666666667, 107.556558641975, 4.65941358024691,
0, 0, 0, 0.00833333333333333, 0.00833333333333333, 0.0498456790123457,
0.0166666666666667, 0.00833333333333333, 0, 0, 0, 0, 0, 0.157793209876543,
62.9890432098765, 16.3951388888889, 33.5790895061728, 52.6984567901235,
50.3315586419753, 54.1604938271605, 66.2529320987654, 103.378703703704,
123.984799382716, 37.2088734567901, 6.13788580246914, 1.47013888888889,
28.2389660493827, 111.103240740741, 66.1949074074074)), row.names = c(NA,
-158L), class = c("tbl_df", "tbl", "data.frame"))
I wonder if there's any way I could do that?
Assuming your length versus time data.frame is named lvt and the temperature versus time is named tvt, we first convert every date to a date
# Assuming lvt$month is a factor beginning at 1 = january, 12 = december
# We'll pseudocenter the month around the 15th
lvt$month2 <- as.Date(ISOdate(2019, as.numeric(lvt$month), 15))
Then once we'll have both x-axes in date format, we can plot the data:
ggplot() +
geom_col(data = lvt, aes(month2, mean, fill = site),
position = "dodge",
colour = "black") +
geom_line(data = tvt, aes(month, tmp*3), size = 1) +
scale_y_continuous(sec.axis = sec_axis(~./3, name = "Temperature"))
Which looks like the following:
Note that it is a bit of a weird plot, because essentially you're mixing ordered categorical data with continuous data. For example tvt starts at January 31st, so it looks like it actually starts in Februari. Furthermore the distances between the groups of bars aren't constant because February for example is a shorter month.
Trying to create a heatmap using ggmosaic, I keep getting the error Error in is.finite(x) : default method not implemented for type 'list'
Searching for that error message, one answer was that "This error is because the is.infinite() and the is.finite() functions are not implemented with a method for data.frames." But not a useful solution in the question nor about ggmosaic
Even the example from the vignette fails for me.
ggplot(data = NHANES) +
geom_mosaic(aes(weight = Weight, x = product(SleepHrsNight), fill=factor(SleepHrsNight)), na.rm=TRUE) +
labs(x="Hours of sleep a night ", title='f(SleepHrsNight)') + guides(fill=guide_legend(title = "SleepHrsNight", reverse = TRUE))
My system is Windows, RStudio, R version one before the most current, and ggmosaic Ver 0.1.2
The made-up data frame I want to use is
structure(list(Diversity = structure(c(1L, 5L, 4L, 5L, 1L, 2L,
2L, 2L, 4L, 1L, 5L, 4L, 5L, 4L, 2L, 3L, 3L, 1L, 5L, 2L, 1L, 4L,
3L, 3L, 3L), .Label = c("AfricanAm", "Asian", "Cauc.", "Latino",
"Other"), class = "factor"), Office = structure(c(1L, 2L, 1L,
3L, 4L, 5L, 2L, 4L, 5L, 3L, 4L, 4L, 1L, 2L, 3L, 4L, 1L, 5L, 5L,
1L, 2L, 3L, 2L, 3L, 5L), .Label = c("Hamlet", "MainTown", "Metroprole",
"Smithville", "Urbanburg"), class = "factor"), JrAssoc = c(1,
1, 1, 1, 1, 1, 2, 2, 2, 4, 4, 1, 1, 1, 2, 2, 3, 3, 3, 5, 5, 2,
5, 9, 10), SrAssPtr = c(2, 2, 1, 1, 3, 2, 1, 4, 4, 5, 1, 1, 3,
5, 7, 3, 2, 1, 1, 1, 1, 2, 3, 4, 4)), row.names = c(NA, -25L), .Names = c("Diversity",
"Office", "JrAssoc", "SrAssPtr"), class = c("tbl_df", "tbl",
"data.frame"))
This code has not succeeded:
ggplot(diverse) +
geom_mosaic(aes(weight = 1, x = product(JrAssoc, SrAssPtr), fill = Diversity))
Thank you for any guidance.
If you just update your ggplot2 library in this way
devtools::install_github('cran/ggplot2')
your problem should be solved.
As mentioned in my comments, author are trying to fix the issue.