获取代码以对这些数据集进行条形图或柱形图时遇到麻烦
trouble getting the code to barplot or column plot these dataset
data.frame(BADAGRY_CREEK_PUBLICATION_2)
month station COPEPODA CYCLOPOIDA
1 MAY. 1 25 0
2 JUNE 1 10 5
3 JULY 1 4 0
4 AUGUST 1 5 5
5 SEPTEMBER 1 20 5
6 MAY. 2 20 0
7 JUNE 2 10 10
8 JULY 2 3 5
9 AUGUST 2 20 0
10 SEPTEMBER 2 40 0
11 MAY. 3 35 5
12 JULY 3 5 0
13 JUNE 3 25 5
14 AUGUST 3 35 5
15 SEPTEMBER 3 10 5
JUVENILE.STAGES Number.of.Species..S.
1 15 5
2 35 6
3 0 1
4 10 4
5 15 5
6 15 5
7 5 4
8 0 2
9 10 4
10 25 6
11 20 6
12 5 2
13 30 6
14 0 3
15 30 6
Number.of.Individuals..N.
1 40
2 50
3 4
4 20
5 40
6 35
7 25
8 8
9 30
10 75
11 60
12 10
13 60
14 40
15 50
希望 x 轴上的 tp 绘图站,每个变量的计数反映在沿 y 轴的条上。我试过这段代码
ggplot(BADAGRY_CREEK_PUBLICATION_2, aes(x=station)) + geom_bar()
[这是绘制的条形图,并未反映收集到的数据]
结果绘制得不好
很难说出您在此处寻找什么,因为您的数据中也有 month
。如果你想在你的情节中忽略 month
,你可以这样做:
library(tidyverse)
BADAGRY_CREEK_PUBLICATION_2 %>%
pivot_longer(-c(1:2)) %>%
group_by(name) %>%
summarise(value = sum(value), station = station) %>%
ggplot(aes(x = factor(station), y = value, fill = name)) +
geom_col(position = "dodge") +
scale_fill_brewer(palette = "Set1") +
theme_light() +
labs(x = "Station", y = "Count")
要包含月份,您可能需要分面:
BADAGRY_CREEK_PUBLICATION_2 %>%
pivot_longer(-c(1:2)) %>%
mutate(month = factor(sub("\.", "", month), levels = c("MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER"))) %>%
ggplot(aes(x = factor(station), y = value, fill = name)) +
geom_col(position = "dodge") +
facet_grid(month~.) +
scale_fill_brewer(palette = "Set1") +
theme_light() +
labs(x = "Station", y = "Count")
以可重现的格式提问数据
BADAGRY_CREEK_PUBLICATION_2 <- structure(list(month = c("MAY.", "JUNE",
"JULY", "AUGUST", "SEPTEMBER",
"MAY.", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "MAY.", "JULY",
"JUNE", "AUGUST", "SEPTEMBER"), station = c(1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), COPEPODA = c(25L, 10L,
4L, 5L, 20L, 20L, 10L, 3L, 20L, 40L, 35L, 5L, 25L, 35L, 10L),
CYCLOPOIDA = c(0L, 5L, 0L, 5L, 5L, 0L, 10L, 5L, 0L, 0L, 5L,
0L, 5L, 5L, 5L), JUVENILE.STAGES = c(15L, 35L, 0L, 10L, 15L,
15L, 5L, 0L, 10L, 25L, 20L, 5L, 30L, 0L, 30L), Number.of.Species..S. = c(5L,
6L, 1L, 4L, 5L, 5L, 4L, 2L, 4L, 6L, 6L, 2L, 6L, 3L, 6L),
Number.of.Individuals..N. = c(40L, 50L, 4L, 20L, 40L, 35L,
25L, 8L, 30L, 75L, 60L, 10L, 60L, 40L, 50L)), class = "data.frame",
row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15"))
data.frame(BADAGRY_CREEK_PUBLICATION_2)
month station COPEPODA CYCLOPOIDA
1 MAY. 1 25 0
2 JUNE 1 10 5
3 JULY 1 4 0
4 AUGUST 1 5 5
5 SEPTEMBER 1 20 5
6 MAY. 2 20 0
7 JUNE 2 10 10
8 JULY 2 3 5
9 AUGUST 2 20 0
10 SEPTEMBER 2 40 0
11 MAY. 3 35 5
12 JULY 3 5 0
13 JUNE 3 25 5
14 AUGUST 3 35 5
15 SEPTEMBER 3 10 5
JUVENILE.STAGES Number.of.Species..S.
1 15 5
2 35 6
3 0 1
4 10 4
5 15 5
6 15 5
7 5 4
8 0 2
9 10 4
10 25 6
11 20 6
12 5 2
13 30 6
14 0 3
15 30 6
Number.of.Individuals..N.
1 40
2 50
3 4
4 20
5 40
6 35
7 25
8 8
9 30
10 75
11 60
12 10
13 60
14 40
15 50
希望 x 轴上的 tp 绘图站,每个变量的计数反映在沿 y 轴的条上。我试过这段代码
ggplot(BADAGRY_CREEK_PUBLICATION_2, aes(x=station)) + geom_bar()
[这是绘制的条形图,并未反映收集到的数据]
结果绘制得不好
很难说出您在此处寻找什么,因为您的数据中也有 month
。如果你想在你的情节中忽略 month
,你可以这样做:
library(tidyverse)
BADAGRY_CREEK_PUBLICATION_2 %>%
pivot_longer(-c(1:2)) %>%
group_by(name) %>%
summarise(value = sum(value), station = station) %>%
ggplot(aes(x = factor(station), y = value, fill = name)) +
geom_col(position = "dodge") +
scale_fill_brewer(palette = "Set1") +
theme_light() +
labs(x = "Station", y = "Count")
要包含月份,您可能需要分面:
BADAGRY_CREEK_PUBLICATION_2 %>%
pivot_longer(-c(1:2)) %>%
mutate(month = factor(sub("\.", "", month), levels = c("MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER"))) %>%
ggplot(aes(x = factor(station), y = value, fill = name)) +
geom_col(position = "dodge") +
facet_grid(month~.) +
scale_fill_brewer(palette = "Set1") +
theme_light() +
labs(x = "Station", y = "Count")
以可重现的格式提问数据
BADAGRY_CREEK_PUBLICATION_2 <- structure(list(month = c("MAY.", "JUNE",
"JULY", "AUGUST", "SEPTEMBER",
"MAY.", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "MAY.", "JULY",
"JUNE", "AUGUST", "SEPTEMBER"), station = c(1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), COPEPODA = c(25L, 10L,
4L, 5L, 20L, 20L, 10L, 3L, 20L, 40L, 35L, 5L, 25L, 35L, 10L),
CYCLOPOIDA = c(0L, 5L, 0L, 5L, 5L, 0L, 10L, 5L, 0L, 0L, 5L,
0L, 5L, 5L, 5L), JUVENILE.STAGES = c(15L, 35L, 0L, 10L, 15L,
15L, 5L, 0L, 10L, 25L, 20L, 5L, 30L, 0L, 30L), Number.of.Species..S. = c(5L,
6L, 1L, 4L, 5L, 5L, 4L, 2L, 4L, 6L, 6L, 2L, 6L, 3L, 6L),
Number.of.Individuals..N. = c(40L, 50L, 4L, 20L, 40L, 35L,
25L, 8L, 30L, 75L, 60L, 10L, 60L, 40L, 50L)), class = "data.frame",
row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15"))