Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinct color palettes for two different groups in ggplot2

I am trying to create a plot showing monthly vertical profiles of soil moisture, for both observed and modeled data across multiple sites.

So far, I am able to plot only one group of values, either the observed or modeled, as in this example:

library(ggplot2)
library(RColorBrewer)

# Create customized color palette
mypal <- colorRampPalette(brewer.pal(6,"PuBu"))

ggplot(df1, aes(x=value, y=depth, colour=as.factor(month))) +
  geom_path() +
  facet_wrap(~ site) +
  scale_y_reverse() +
  scale_colour_manual(values=mypal(12)) +
  theme_bw(base_size=18) +
  ylab("Depth") + xlab(bquote('Soil moisture (' ~m^3~m^-3*')')) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 

And here is the data to reproduce it:

df1 <- structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 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, 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7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L), .Label = c("IL_Shabbona_5_NNE", "ME_Limestone_4_NNW", 
"ME_Old_Town_2_W", "MI_Chatham_1_SE", "MI_Gaylord_9_SSW", "MN_Goodridge_12_NNW", 
"MN_Sandstone_6_W", "NH_Durham_2_N", "NH_Durham_2_SSW", "NY_Ithaca_13_E", 
"NY_Millbrook_3_W", "WI_Necedah_5_WNW"), class = "factor"), month = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 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, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L), depth = c(5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 
10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 
50, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 100, 100, 
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 50, 50, 
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 100, 100, 100, 100, 100, 
100, 100, 100, 100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 50, 50, 50, 50, 50, 
50, 50, 50, 50, 50, 50, 50, 100, 100, 100, 100, 100, 100, 100, 
100, 100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 50, 50, 50, 50, 50, 50, 50, 50, 
50, 50, 50, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 
10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 
50, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 100, 100, 
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 50, 50, 
50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 100, 100, 100, 100, 100, 
100, 100, 100, 100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 50, 50, 50, 50, 50, 
50, 50, 50, 50, 50, 50, 50, 100, 100, 100, 100, 100, 100, 100, 
100, 100, 100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 50, 50, 50, 50, 50, 50, 50, 50, 
50, 50, 50, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
100, 100, 100, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 
10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 
50, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 100, 100, 
100, 100, 100, 100, 100, 100, 100, 100, 100, 100), clm = c(0.324, 
0.321, 0.334, 0.33, 0.285, 0.277, 0.264, 0.261, 0.261, 0.273, 
0.303, 0.339, 0.348, 0.349, 0.356, 0.333, 0.289, 0.281, 0.269, 
0.267, 0.266, 0.277, 0.303, 0.352, 0.345, 0.349, 0.355, 0.337, 
0.295, 0.288, 0.277, 0.275, 0.274, 0.284, 0.299, 0.344, 0.299, 
0.306, 0.314, 0.331, 0.312, 0.305, 0.295, 0.293, 0.292, 0.3, 
0.299, 0.295, 0.274, 0.278, 0.284, 0.308, 0.302, 0.298, 0.289, 
0.287, 0.286, 0.292, 0.291, 0.281, 0.477, 0.475, 0.475, 0.518, 
0.379, 0.353, 0.352, 0.341, 0.347, 0.373, 0.46, 0.499, 0.399, 
0.401, 0.399, 0.435, 0.346, 0.326, 0.325, 0.316, 0.32, 0.339, 
0.37, 0.409, 0.339, 0.34, 0.337, 0.362, 0.313, 0.294, 0.293, 
0.286, 0.289, 0.306, 0.317, 0.345, 0.256, 0.259, 0.26, 0.274, 
0.281, 0.262, 0.26, 0.254, 0.255, 0.271, 0.263, 0.256, 0.243, 
0.242, 0.242, 0.252, 0.281, 0.267, 0.266, 0.261, 0.26, 0.274, 
0.27, 0.25, 0.497, 0.467, 0.461, 0.434, 0.377, 0.361, 0.341, 
0.314, 0.334, 0.372, 0.394, 0.514, 0.363, 0.35, 0.344, 0.366, 
0.34, 0.327, 0.311, 0.289, 0.304, 0.336, 0.344, 0.367, 0.308, 
0.294, 0.286, 0.318, 0.304, 0.293, 0.279, 0.26, 0.272, 0.3, 0.309, 
0.308, 0.232, 0.228, 0.237, 0.281, 0.271, 0.263, 0.25, 0.235, 
0.239, 0.265, 0.277, 0.25, 0.235, 0.233, 0.24, 0.28, 0.273, 0.265, 
0.254, 0.242, 0.241, 0.264, 0.277, 0.258, 0.448, 0.446, 0.454, 
0.49, 0.341, 0.31, 0.297, 0.298, 0.325, 0.359, 0.427, 0.478, 
0.351, 0.352, 0.359, 0.403, 0.312, 0.287, 0.276, 0.276, 0.298, 
0.325, 0.333, 0.355, 0.305, 0.309, 0.312, 0.338, 0.281, 0.26, 
0.25, 0.249, 0.266, 0.29, 0.288, 0.3, 0.233, 0.233, 0.237, 0.265, 
0.256, 0.238, 0.229, 0.227, 0.236, 0.256, 0.252, 0.239, 0.231, 
0.229, 0.228, 0.253, 0.262, 0.247, 0.237, 0.235, 0.24, 0.255, 
0.257, 0.24, 0.326, 0.323, 0.341, 0.343, 0.268, 0.236, 0.209, 
0.213, 0.228, 0.27, 0.301, 0.34, 0.311, 0.31, 0.321, 0.322, 0.254, 
0.228, 0.206, 0.208, 0.22, 0.254, 0.278, 0.317, 0.285, 0.285, 
0.295, 0.302, 0.243, 0.22, 0.2, 0.2, 0.211, 0.241, 0.253, 0.283, 
0.218, 0.22, 0.222, 0.259, 0.236, 0.215, 0.197, 0.193, 0.198, 
0.225, 0.232, 0.22, 0.19, 0.188, 0.187, 0.214, 0.225, 0.205, 
0.189, 0.182, 0.183, 0.201, 0.213, 0.2, 0.437, 0.435, 0.437, 
0.456, 0.391, 0.367, 0.358, 0.339, 0.347, 0.374, 0.468, 0.468, 
0.402, 0.401, 0.406, 0.442, 0.39, 0.369, 0.362, 0.345, 0.351, 
0.364, 0.41, 0.425, 0.386, 0.386, 0.389, 0.415, 0.375, 0.355, 
0.349, 0.336, 0.34, 0.349, 0.377, 0.401, 0.318, 0.319, 0.32, 
0.343, 0.341, 0.32, 0.316, 0.307, 0.307, 0.313, 0.31, 0.321, 
0.302, 0.308, 0.312, 0.323, 0.335, 0.313, 0.309, 0.304, 0.303, 
0.307, 0.303, 0.299, 0.39, 0.387, 0.391, 0.405, 0.347, 0.335, 
0.328, 0.317, 0.327, 0.339, 0.387, 0.411, 0.387, 0.386, 0.39, 
0.392, 0.336, 0.325, 0.319, 0.31, 0.318, 0.326, 0.367, 0.399, 
0.367, 0.368, 0.373, 0.38, 0.332, 0.322, 0.317, 0.308, 0.315, 
0.321, 0.342, 0.374, 0.299, 0.302, 0.305, 0.337, 0.318, 0.307, 
0.304, 0.297, 0.3, 0.306, 0.298, 0.3, 0.265, 0.27, 0.274, 0.293, 
0.293, 0.281, 0.28, 0.275, 0.276, 0.281, 0.275, 0.265, 0.588, 
0.591, 0.555, 0.362, 0.367, 0.355, 0.317, 0.228, 0.161, 0.28, 
0.381, 0.412, 0.38, 0.389, 0.399, 0.301, 0.304, 0.294, 0.265, 
0.199, 0.149, 0.234, 0.315, 0.303, 0.302, 0.296, 0.323, 0.277, 
0.279, 0.27, 0.245, 0.19, 0.148, 0.211, 0.288, 0.262, 0.173, 
0.168, 0.188, 0.202, 0.196, 0.184, 0.171, 0.134, 0.113, 0.116, 
0.192, 0.184, 0.151, 0.136, 0.142, 0.18, 0.176, 0.164, 0.155, 
0.131, 0.119, 0.115, 0.165, 0.166, 0.588, 0.591, 0.555, 0.362, 
0.367, 0.355, 0.317, 0.228, 0.161, 0.28, 0.381, 0.412, 0.38, 
0.389, 0.399, 0.301, 0.304, 0.294, 0.265, 0.199, 0.149, 0.234, 
0.315, 0.303, 0.302, 0.296, 0.323, 0.277, 0.279, 0.27, 0.245, 
0.19, 0.148, 0.211, 0.288, 0.262, 0.173, 0.168, 0.188, 0.202, 
0.196, 0.184, 0.171, 0.134, 0.113, 0.116, 0.192, 0.184, 0.151, 
0.136, 0.142, 0.18, 0.176, 0.164, 0.155, 0.131, 0.119, 0.115, 
0.165, 0.166, 0.472, 0.46, 0.457, 0.442, 0.387, 0.382, 0.371, 
0.37, 0.377, 0.394, 0.432, 0.497, 0.452, 0.45, 0.45, 0.416, 0.367, 
0.362, 0.353, 0.352, 0.358, 0.372, 0.39, 0.453, 0.414, 0.418, 
0.42, 0.4, 0.354, 0.35, 0.342, 0.34, 0.346, 0.359, 0.363, 0.408, 
0.331, 0.341, 0.345, 0.362, 0.336, 0.332, 0.325, 0.322, 0.327, 
0.341, 0.337, 0.327, 0.296, 0.301, 0.306, 0.329, 0.326, 0.321, 
0.314, 0.311, 0.314, 0.328, 0.327, 0.309, 0.451, 0.44, 0.458, 
0.441, 0.344, 0.315, 0.303, 0.314, 0.324, 0.349, 0.396, 0.47, 
0.334, 0.327, 0.349, 0.372, 0.302, 0.279, 0.269, 0.278, 0.285, 
0.305, 0.31, 0.34, 0.287, 0.284, 0.293, 0.33, 0.279, 0.259, 0.248, 
0.256, 0.263, 0.281, 0.281, 0.288, 0.181, 0.182, 0.181, 0.226, 
0.212, 0.194, 0.184, 0.185, 0.192, 0.208, 0.209, 0.188, 0.174, 
0.175, 0.177, 0.208, 0.209, 0.193, 0.185, 0.184, 0.189, 0.201, 
0.203, 0.186, 0.365, 0.364, 0.367, 0.372, 0.309, 0.303, 0.288, 
0.292, 0.299, 0.308, 0.351, 0.382, 0.371, 0.372, 0.379, 0.376, 
0.311, 0.305, 0.291, 0.295, 0.301, 0.309, 0.343, 0.383, 0.365, 
0.366, 0.372, 0.376, 0.318, 0.313, 0.3, 0.303, 0.309, 0.316, 
0.33, 0.371, 0.33, 0.338, 0.342, 0.359, 0.33, 0.325, 0.313, 0.315, 
0.32, 0.327, 0.317, 0.324, 0.291, 0.296, 0.3, 0.319, 0.317, 0.312, 
0.303, 0.304, 0.307, 0.314, 0.304, 0.293)), row.names = c(NA, 
-720L), class = "data.frame", .Names = c("site", "month", "depth", 
"value"))

However, I want to add one more "group" to this same plot. The final plot would have an additional "set" of vertical lines, but using a different color palette (using for example red instead of blue).

My unsuccessful attempt to create this plot uses this code:

# Create some random data based on the first dataset
df1 <- cbind(df1, type='observed')
df2 <- df1[1:4]
df2$value <- df2$value * 1.5
df2 <- cbind(df2, type='modeled')
df3 <- rbind(df1,df2)

# Try to plot it
ggplot(df3, aes(x=value, y=depth, group=type, colour=as.factor(month))) +
  geom_path() +
  facet_wrap(~ site) +
  scale_y_reverse() +
  theme_bw(base_size=18) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

which is clearly not showing what I want. Why is it showing only two lines per facet? Where are the other vertical lines? How can I configure different color palettes for the two groups?

How can this be done in ggplot2?

like image 321
thiagoveloso Avatar asked Dec 20 '17 08:12

thiagoveloso


1 Answers

You can use interaction to combine type and month: color = interaction(as.factor(month), type)). Instead of: group=type, colour=as.factor(month).

And to create red and blue pallets use two mypal functions:

mypal <- colorRampPalette(brewer.pal(6, "PuBu"))
mypal2 <- colorRampPalette(brewer.pal(6, "YlOrRd"))

Code:

library(ggplot2)
library(RColorBrewer)

mypal <- colorRampPalette(brewer.pal(6, "PuBu"))
mypal2 <- colorRampPalette(brewer.pal(6, "YlOrRd"))

ggplot(df3, 
       aes(value, depth, color = interaction(as.factor(month), type))) +
    geom_path() +
    facet_wrap(~ site) +
    labs(title = "Soil moisture by depth and site",
         subtitle = "Observed and expected data",
         x = bquote('Soil moisture (' ~m^3~m^-3*')'),
         y = "Depth") +
    scale_y_reverse() +
    scale_colour_manual(values = c(mypal(12), mypal2(12))) +
    theme_classic() + 
    theme(legend.position = "none")

Plot:

enter image description here

like image 198
pogibas Avatar answered Sep 27 '22 22:09

pogibas