我正在尝试在R中创建一个图形,类似于在Excel中生成的图形:
。
我试图按年份和指数对条形图进行分组,并按治疗方法分开。我试图将我的数据以“长格式”输入到R中,并想到了:
Year Indices Means Treatment
1 2015 Shannon 3.062402 Drought
2 2015 Shannon 3.329283 Control
3 2015 Simpson 3.784725 Drought
4 2015 Simpson 4.346054 Control
5 2015 Species Richness 7.833333 Drought
6 2015 Species Richness 8.000000 Control
7 2016 Shannon 3.320377 Drought
8 2016 Shannon 3.496330 Control
9 2016 Simpson 3.877924 Drought
10 2016 Simpson 4.443414 Control
11 2016 Species Richness 9.666667 Drought
12 2016 Species Richness 10.333333 Control
13 2017 Shannon 4.421234 Drought
14 2017 Shannon 4.347877 Control
15 2017 Simpson 7.070072 Drought
16 2017 Simpson 6.340170 Control
17 2017 Species Richness 14.833333 Drought
18 2017 Species Richness 14.833333 Control
19 2018 Shannon 4.055784 Drought
20 2018 Shannon 3.939885 Control
21 2018 Simpson 5.796247 Drought
22 2018 Simpson 5.018261 Control
23 2018 Species Richness 13.500000 Drought
24 2018 Species Richness 14.000000 Control
使用GGplot,我可以创建一个条形图,其中Year或Indices的分组时间不同。以下按年份分组。图表如下所示:
ggplot(data,aes(x=Year,y=Means, fill=Treatment),stat="identity",fill=factor(Treatment))+
geom_bar(stat="identity",position="dodge")
以下是按索引分组的图形
。
ggplot(data,aes(x=Indices,y=Means, fill=Treatment),stat="identity",fill=factor(Treatment))+
geom_bar(stat="identity",position="dodge")
我的问题是,如何创建按年份和索引分组的图表,并用不同的条形表示类似于Excel图形的处理方式?是否应该以其他方式重新输入数据?我尝试使用group_by,但是没有运气。
谢谢!
答案 0 :(得分:0)
我倾向于使用facet_wrap()
或facet_grid()
将多个组放入条形图中。
这应该可以创建与Excel中类似的输出。网格的好处是,它在每个网格中均等地缩放x和y轴。可以在facet_grid
的选项中进行调整。
ggplot(x,aes(x=Indices,y=Means, fill=Treatment),
stat="identity",
fill=factor(Treatment)) +
geom_bar(stat="identity", position="dodge") +
facet_grid(cols = vars(Year))
facet_grid()
在其中创建网格的地方,您可以在其中指定将网格分组到的列。
我还发现了this solution by Tom Martens,它看起来很冗长,但可能正是您需要的。
答案 1 :(得分:0)
在您提出要求后,我意识到这很好,但是我一直在研究一个软件包,以帮助您准确地创建这些类型的分组条形图(Action Recognition From Depth Maps Using Deep Convolutional Neural Networks)。以您的示例为例:
plotDat <- read.table(
header = TRUE,
sep = ",",
quote = "",
strip.white = TRUE,
text =
"Year, Indices, Means, Treatment
2015, Shannon, 3.062402, Drought
2015, Shannon, 3.329283, Control
2015, Simpson, 3.784725, Drought
2015, Simpson, 4.346054, Control
2015, Species Richness, 7.833333, Drought
2015, Species Richness, 8.000000, Control
2016, Shannon, 3.320377, Drought
2016, Shannon, 3.496330, Control
2016, Simpson, 3.877924, Drought
2016, Simpson, 4.443414, Control
2016, Species Richness, 9.666667, Drought
2016, Species Richness, 10.333333, Control
2017, Shannon, 4.421234, Drought
2017, Shannon, 4.347877, Control
2017, Simpson, 7.070072, Drought
2017, Simpson, 6.340170, Control
2017, Species Richness, 14.833333, Drought
2017, Species Richness, 14.833333, Control
2018, Shannon, 4.055784, Drought
2018, Shannon, 3.939885, Control
2018, Simpson, 5.796247, Drought
2018, Simpson, 5.018261, Control
2018, Species Richness, 13.500000, Drought
2018, Species Richness, 14.000000, Control")
devtools::install_github("davedgd/ggNestedBarChart")
library(ggNestedBarChart)
p1 <- ggplot(plotDat, aes(x = Treatment, y = Means, fill = Treatment), stat = "identity") +
geom_bar(stat = "identity") +
facet_wrap(vars(Year, Indices), strip.position = "top", scales = "free_x", nrow = 1) +
theme_bw(base_size = 13) +
theme(panel.spacing = unit(0, "lines"),
strip.background = element_rect(color = "black", size = 0, fill = "grey92"),
strip.placement = "outside",
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_line(colour = "grey"),
panel.border = element_rect(color = "black", fill = NA, size = 0),
panel.background = element_rect(fill = "white")) +
scale_y_continuous(expand = expand_scale(mult = c(0, .1))) +
labs(x = "")
ggNestedBarChart(p1)
ggsave("p1.png", width = 18, height = 5)
请注意,相对于仅调用p1,ggNestedBarChart可以很好地样式化绘图方面的条带(例如,顶部条带中的分组年份)。