我正在制作一张图表,显示两个不同地点的一个物种的三个生命阶段。
我想创建一组图表,显示不同地区的不同生命阶段。
到目前为止,我可以使用
patchwork
来完成此操作,但由于尺寸小,我的专栏几乎无法阅读。
我也尝试过完全在
ggplot
中做到这一点,但到目前为止我无法同时拥有单独的生命阶段和地点。
我的数据来自我自己的归档工作。
其他使用的包有:
library(tidyverse)
library(dplyr)
library(tidyr)
library(scales)
我如何使用我的数据:
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")
这是我的数据的子集:
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
如果我尝试仅使用 ggplot,我只能得到单独的生命阶段或单独的位置。
这就是我想要实现的外观,但除此之外我还希望将地点分开。当我尝试使用
patchwork
执行此操作时,图表变得太混乱且难以阅读。
我的 ggplot 代码是什么样的:
#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
有人有任何改进我的图表的建议吗?
非常感谢任何帮助!
您应该使用已经制作的
data_long
数据框,并使用 facet_grid
来布置面板。这样您的绘图就可以共享轴并节省空间。所以:
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