How to stack graphs on top of each other with two segments each in ggplot

139 views Asked by At

I am working on a graph showing three life stages of a species for two different localities.

I want to create a set of graphs showing the different life stages on top of each other for the different localities.

So far I can do it using patchwork, but my columns are barely readable due to the small size.

I have also tried doing it entirely in ggplot, but so far I can not have both separate life stages and localities at the same time.

My data comes from my own filed work.

Other packages used are: library(tidyverse) library(dplyr) library(tidyr) library(scales)

How I used my data:

lifestages <- read.csv("Lifestages_ny.csv", 
                       sep=";", header = T, 
                       stringsAsFactors=FALSE)

#dates to actual dates!
lifestages$Dato <- as.Date(lifestages$Dato)

#selecting a subset of your data:
spidersubset <- subset(lifestages, select = c(Dato, Place, total_tiny, total_medium, total_large))

data_long <- pivot_longer(data=spidersubset, cols=c("total_tiny", "total_medium", "total_large"), names_to = "Llifestages", values_to = "Count")

Here is a subset of my data:

 Nr       Dato        Place spider_count total_tiny total_medium total_large
1    1 2021-09-15 Nedre Leberg            1          0            1           0
2    2 2021-09-15 Nedre Leberg            4          0            0           4
3    3 2021-09-15 Nedre Leberg            1          0            0           1
4    4 2021-09-15 Nedre Leberg            2          0            0           2
5    5 2021-09-15 Nedre Leberg            2          0            0           2
6    6 2021-09-15 Nedre Leberg            7          0            0           7
7    7 2021-09-15 Nedre Leberg            2          0            0           2
8    8 2021-09-15 Nedre Leberg            3          0            0           3
10  10 2021-09-15 Nedre Leberg            1          0            1           0
11  11 2021-09-15 Nedre Leberg            1          0            0           1
12  12 2021-09-15 Nedre Leberg            1          0            0           1
13  13 2021-09-15 Nedre Leberg            1          0            0           1
14  14 2021-09-15 Nedre Leberg            5          0            0           5
15  15 2021-09-15 Nedre Leberg            1          0            0           1
16  16 2021-09-15 Nedre Leberg            1          0            0           2
18   1 2022-04-23 Nedre Leberg            1          0            0           1
19   2 2022-04-23 Nedre Leberg            1          0            0           1
20   3 2022-04-23 Nedre Leberg            1          0            1           0
21   4 2022-04-23 Nedre Leberg            2          0            0           2
22   5 2022-04-23 Nedre Leberg            5          0            4           1
23   6 2022-04-23 Nedre Leberg            2          0            2           0
24   7 2022-04-23 Nedre Leberg            1          0            0           1
25   8 2022-04-23 Nedre Leberg            1          0            0           1
26   9 2022-04-23 Nedre Leberg            1          0            0           1
27  10 2022-04-23 Nedre Leberg            1          0            0           1
28  11 2022-04-23 Nedre Leberg            1          0            1           0
29  12 2022-04-23 Nedre Leberg            3          0            2           0
30  13 2022-04-23 Nedre Leberg            8          0            2           6
31  14 2022-04-23 Nedre Leberg            4          0            2           2
32  15 2022-04-23 Nedre Leberg            2          0            0           2
33  16 2022-04-23 Nedre Leberg            6          0            2           4
34   1 2022-05-18 Nedre Leberg            1          0            0           1
35   2 2022-05-18 Nedre Leberg            1          0            0           1
36   3 2022-05-18 Nedre Leberg            1          0            0           1
37   4 2022-05-18 Nedre Leberg            4          0            1           3
38   5 2022-05-18 Nedre Leberg            1          0            0           1
39   6 2022-05-18 Nedre Leberg            1          0            0           1
40   7 2022-05-18 Nedre Leberg            1          0            0           1
41   8 2022-05-18 Nedre Leberg            1          0            0           1
42   9 2022-05-18 Nedre Leberg            1          0            1           0
43  10 2022-05-18 Nedre Leberg            1          0            0           1
44  11 2022-05-18 Nedre Leberg            4          0            1           3
45  12 2022-05-18 Nedre Leberg            3          0            1           2
46  13 2022-05-18 Nedre Leberg            4          0            0           4
47  14 2022-05-18 Nedre Leberg            1          0            0           1
48  15 2022-05-18 Nedre Leberg            2          0            0           2
49  16 2022-05-18 Nedre Leberg            2          0            1           1
50  17 2022-05-18 Nedre Leberg            1          0            0           1
51   1 2022-06-07   Rinnleiret            2          0            0           1
52   2 2022-06-07   Rinnleiret            4          0            0           4
53   1 2022-06-09 Nedre Leberg            0          0            0           0
54   1 2022-06-10   Rinnleiret            1          0            0           1
55   2 2022-06-10   Rinnleiret            1          0            1           0
56   3 2022-06-10   Rinnleiret            1          0            0           1
57   4 2022-06-10   Rinnleiret            1          0            1           0
58   5 2022-06-10   Rinnleiret            1          0            0           1
59   6 2022-06-10   Rinnleiret            1          0            0           1
60   1 2022-06-23 Nedre Leberg            1          0            1           0
61   2 2022-06-23 Nedre Leberg            2          0            0           1
62   1 2022-07-04 Nedre Leberg            1          1            0           0
63   2 2022-07-04 Nedre Leberg            1          1            0           0
64   3 2022-07-04 Nedre Leberg            1          1            0           0
65   4 2022-07-04 Nedre Leberg            1          0            1           0
66   5 2022-07-04 Nedre Leberg            1          0            1           0
67   6 2022-07-04 Nedre Leberg            1          1            0           0
68   1 2022-07-12   Rinnleiret            1          0            1           0
69   2 2022-07-12   Rinnleiret            1          0            1           0
70   3 2022-07-12   Rinnleiret            1          0            1           0
71   4 2022-07-12   Rinnleiret            1          0            0           1
72   5 2022-07-12   Rinnleiret            1          0            0           1
73   6 2022-07-12   Rinnleiret            1          0            0           1
74   7 2022-07-12   Rinnleiret            4          0            1           3
75   8 2022-07-12   Rinnleiret            6          1            5           0
76   9 2022-07-12   Rinnleiret            1          0            0           1
77  10 2022-07-12   Rinnleiret            1          0            0           1
78  11 2022-07-12   Rinnleiret            1          0            0           1
79  12 2022-07-12   Rinnleiret            1          0            0           1
80  13 2022-07-12   Rinnleiret            2          0            2           0
81  14 2022-07-12   Rinnleiret            3          1            2           0
82  15 2022-07-12   Rinnleiret            3          1            2           0
83   1 2022-07-29 Nedre Leberg            1          1            0           0
84   2 2022-07-29 Nedre Leberg            1          1            0           0
85   1 2022-08-02   Rinnleiret            1          0            1           0
86   2 2022-08-02   Rinnleiret            1          0            1           0
87   3 2022-08-02   Rinnleiret            1          0            0           1
89   5 2022-08-02   Rinnleiret            1          0            1           0
90   6 2022-08-02   Rinnleiret            1          1            0           0
91   7 2022-08-02   Rinnleiret            1          0            0           1
92   8 2022-08-02   Rinnleiret            1          0            1           0
93   9 2022-08-02   Rinnleiret            1          0            0           1
94  10 2022-08-02   Rinnleiret            1          0            0           1
95  11 2022-08-02   Rinnleiret            3          1            2           0
96  12 2022-08-02   Rinnleiret            2          0            2           0
97  13 2022-08-02   Rinnleiret            4          2            2           0
98  14 2022-08-02   Rinnleiret            3          0            3           0
99  15 2022-08-02   Rinnleiret            4          0            2           2
100 16 2022-08-02   Rinnleiret            3          0            3           0
101 17 2022-08-02   Rinnleiret            1          0            0           1
102 18 2022-08-02   Rinnleiret            4          0            2           2
103 19 2022-08-02   Rinnleiret            4          0            2           2
104 20 2022-08-02   Rinnleiret            1          0            0           1
105 21 2022-08-02   Rinnleiret            2          0            2           0
106 22 2022-08-02   Rinnleiret            2          0            2           0
107 23 2022-08-02   Rinnleiret            1          0            0           1
108 24 2022-08-02   Rinnleiret            1          0            0           1
109 25 2022-08-02   Rinnleiret            2          0            2           0
110  1 2022-08-15   Rinnleiret            1          1            0           0
111  2 2022-08-15   Rinnleiret            1          0            1           0
112  3 2022-08-15   Rinnleiret            1          1            0           0
113  4 2022-08-15   Rinnleiret            1          0            1           0
114  5 2022-08-15   Rinnleiret            3          2            1           0
115  6 2022-08-15   Rinnleiret            1          0            0           1
116  7 2022-08-15   Rinnleiret            2          0            1           1
117  8 2022-08-15   Rinnleiret            4          0            0           4
118  9 2022-08-15   Rinnleiret            1          0            0           1
119 10 2022-08-15   Rinnleiret            1          0            0           1
120 11 2022-08-15   Rinnleiret            1          0            0           1
121 12 2022-08-15   Rinnleiret            1          0            0           1
122 13 2022-08-15   Rinnleiret            3          0            0           1
123 14 2022-08-15   Rinnleiret            2          0            0           2
124 15 2022-08-15   Rinnleiret            4          1            3           0
125 16 2022-08-15   Rinnleiret            1          0            1           0
126 17 2022-08-15   Rinnleiret            3          0            3           0
127 18 2022-08-15   Rinnleiret            2          0            1           1
128 19 2022-08-15   Rinnleiret            3          1            1           1
129 20 2022-08-15   Rinnleiret            1          0            0           1
130 21 2022-08-15   Rinnleiret            3          1            2           0
131 22 2022-08-15   Rinnleiret            2          0            1           1
132 23 2022-08-15   Rinnleiret            4          1            3           0
133 24 2022-08-15   Rinnleiret            6          2            3           1
134 25 2022-08-15   Rinnleiret            2          0            0           2
135 26 2022-08-15   Rinnleiret           13         10            0           4
136 27 2022-08-15   Rinnleiret            2          0            0           2
137 28 2022-08-15   Rinnleiret            1          0            0           1
138 29 2022-08-15   Rinnleiret            1          0            1           0
139 30 2022-08-15   Rinnleiret            2          0            1           1
140 31 2022-08-15   Rinnleiret            5          0            2           3
141 32 2022-08-15   Rinnleiret            5          0            2           3
142 33 2022-08-15   Rinnleiret            2          0            0           2
143 34 2022-08-15   Rinnleiret            3          0            1           2
148  5 2022-08-18 Nedre Leberg            1          1            0           0
150  7 2022-08-18 Nedre Leberg            1          0            0           1

If I try using only ggplot I get either only separate lifestages or separate localities.

ggplot graph showing lifestages in separate graphs

This is the sort of look I want to achieve, but in addition I want to have the localities separate. When I try to do this using patchwork, the graph becomes too messy and hard to read.

What my ggplot code looks like:

#using patchwork in stead?
small <- ggplot(lifestages, aes(x = Dato, y = total_tiny))+
  labs(title = "Spider life stages",
       x = "Date", y = "Amount small spiders")+
  guides(x =  guide_axis(angle = 90)) +
  facet_wrap(~ Place, ncol = 2)+
  scale_x_date(breaks=date_breaks("1 month"), labels=date_format("%Y-%m-%d"))+
  theme_bw()+
  geom_bar(stat = "identity", position = "dodge", width = 15, fill="lightgray")
small

medium <- ggplot(lifestages, aes(x = Dato, y = total_medium))+
  labs(title = "Spider life stages",
       x = "Date", y = "Amount medium spiders")+
  guides(x =  guide_axis(angle = 90)) +
  facet_wrap(~ Place, ncol = 2)+
  scale_x_date(breaks=date_breaks("1 month"), labels=date_format("%Y-%m-%d"))+
  theme_bw()+
  geom_bar(stat = "identity", position = "dodge", width = 15, fill="darkgray")
medium

large <- ggplot(lifestages, aes(x = Dato, y = total_large))+
  labs(title = "Spider life stages",
       x = "Date", y = "Amount large spiders")+
  guides(x =  guide_axis(angle = 90)) +
  facet_wrap(~ Place, ncol = 2)+
  scale_x_date(breaks=date_breaks("1 month"), labels=date_format("%Y-%m-%d"))+
  theme_bw()+
  geom_bar(stat = "identity", position = "dodge", width = 15, fill="black")
large
large/medium/small

patchwork plot

Does anyone have any tips to improve my graph?

Any help is greatly appreciated!

1

There are 1 answers

0
George Savva On BEST ANSWER

You should use your data_long data frame that you already made, and use facet_grid to lay out the panels. That way your plots can share axes and save space. So:

combined <- ggplot(data_long, aes(x = Dato, y = Count))+
  labs(title = "Spider life stages",
       x = "Date", y = "Amount small spiders")+
  guides(x =  guide_axis(angle = 90)) +
  facet_grid(Llifestages ~ Place)+
  scale_x_date(breaks=date_breaks("1 month"), labels=date_format("%Y-%m-%d"))+
  theme_bw()+
  geom_bar(stat = "identity", position = "dodge", width = 15, fill="lightgray")
combined

enter image description here