我正在尝试创建带有嵌套x轴的堆叠直方图
我已经尝试过ggplot,但到目前为止还没有结束
从上图可以看出,我正在尝试制作一个直方图 都显示每年每个垃圾箱的总数,以及组成该数字的单个数 条形图中显示的总计数
构成总数的个体计数是从幻想得出的 与FAMILY_DESC列相关的列
总而言之,我一直试图在直方图和 堆积的条形图
下一部分是一个嵌套的x轴,所以会有6个bin, 会一直重复
我希望这很有道理,并在此先非常感谢您
structure(list(Year = c("2009", "2009", "2009", "2009", "2009",
"2009", "2009", "2009", "2009", "2009", "2009", "2009", "2009",
"2009", "2009", "2009", "2009", "2009", "2009", "2009", "2009",
"2009", "2009", "2009", "2011", "2011", "2011", "2011", "2011",
"2011", "2011", "2011", "2011", "2011", "2011", "2011", "2011",
"2011", "2011", "2011", "2011", "2011", "2011", "2011", "2011",
"2011", "2011", "2011", "2013", "2013", "2013", "2013", "2013",
"2013", "2013", "2013", "2013", "2013", "2013", "2013", "2013",
"2013", "2013", "2013", "2013", "2013", "2013", "2013", "2013",
"2013", "2013", "2013", "2015", "2015", "2015", "2015", "2015",
"2015", "2015", "2015", "2015", "2015", "2015", "2015", "2015",
"2015", "2015", "2015", "2015", "2015", "2015", "2015", "2015",
"2015", "2015", "2015"), FAMILY_DESC = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 11L, 11L, 11L, 11L, 11L,
11L, 12L, 12L, 12L, 12L, 12L, 12L, 1L, 1L, 1L, 1L, 1L, 1L, 5L,
5L, 5L, 5L, 5L, 5L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L,
12L, 12L, 12L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L,
11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 1L,
1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 11L, 11L, 11L, 11L,
11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L), .Label = c("ACROPORIDAE",
"AGARICIIDAE", "DENDROPHYLLIIDAE", "EUPHYLLIDAE", "FAVIIDAE",
"FUNGIIDAE", "MERULINIDAE", "MUSSIDAE", "OCULINIDAE", "PECTINIIDAE",
"POCILLOPORIDAE", "PORITIDAE", "SIDERASTREIDAE", "UNKNOWN"), class = "factor"),
bin = c("five", "four", "one", "three", "two", "zero", "five",
"four", "one", "three", "two", "zero", "five", "four", "one",
"three", "two", "zero", "five", "four", "one", "three", "two",
"zero", "five", "four", "one", "three", "two", "zero", "five",
"four", "one", "three", "two", "zero", "five", "four", "one",
"three", "two", "zero", "five", "four", "one", "three", "two",
"zero", "five", "four", "one", "three", "two", "zero", "five",
"four", "one", "three", "two", "zero", "five", "four", "one",
"three", "two", "zero", "five", "four", "one", "three", "two",
"zero", "five", "four", "one", "three", "two", "zero", "five",
"four", "one", "three", "two", "zero", "five", "four", "one",
"three", "two", "zero", "five", "four", "one", "three", "two",
"zero"), famtotal = c(0L, 1L, 87L, 1L, 4L, 136L, 0L, 1L,
35L, 2L, 7L, 6L, 0L, 1L, 33L, 8L, 12L, 64L, 0L, 1L, 37L,
1L, 8L, 20L, 0L, 2L, 88L, 3L, 6L, 122L, 0L, 2L, 57L, 4L,
9L, 16L, 0L, 5L, 43L, 4L, 15L, 81L, 0L, 1L, 32L, 1L, 7L,
25L, 0L, 1L, 57L, 4L, 9L, 100L, 0L, 6L, 38L, 7L, 9L, 8L,
0L, 0L, 29L, 8L, 8L, 47L, 0L, 0L, 26L, 2L, 4L, 23L, 0L, 0L,
65L, 1L, 14L, 124L, 0L, 2L, 29L, 1L, 3L, 19L, 0L, 5L, 38L,
4L, 5L, 50L, 0L, 0L, 20L, 0L, 6L, 18L), yeartotal = c(0L,
4L, 192L, 12L, 31L, 226L, 0L, 4L, 192L, 12L, 31L, 226L, 0L,
4L, 192L, 12L, 31L, 226L, 0L, 4L, 192L, 12L, 31L, 226L, 0L,
10L, 220L, 12L, 37L, 244L, 0L, 10L, 220L, 12L, 37L, 244L,
0L, 10L, 220L, 12L, 37L, 244L, 0L, 10L, 220L, 12L, 37L, 244L,
0L, 7L, 150L, 21L, 30L, 178L, 0L, 7L, 150L, 21L, 30L, 178L,
0L, 7L, 150L, 21L, 30L, 178L, 0L, 7L, 150L, 21L, 30L, 178L,
0L, 7L, 152L, 6L, 28L, 211L, 0L, 7L, 152L, 6L, 28L, 211L,
0L, 7L, 152L, 6L, 28L, 211L, 0L, 7L, 152L, 6L, 28L, 211L)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -96L), .Names = c("Year",
"FAMILY_DESC", "bin", "famtotal", "yeartotal"), vars = c("Year",
"FAMILY_DESC"))
答案 0 :(得分:3)
您可以尝试一下。与其他好的答案类似,但有一些重大变化以模仿您的方案。
ggplot(d, aes(x=factor(bin, levels = c("zero","one", "two", "three", "four", "five")),
y=yeartotal, fill=FAMILY_DESC)) +
geom_col() +
xlab("") +
facet_grid(~Year, switch = "x") +
theme(panel.border = element_blank(),
panel.spacing.x = unit(0,"cm"),
strip.placement = "outside",
strip.background = element_blank())
答案 1 :(得分:2)
dat
是您的data.frame,bin
可能需要重新排序:
ggplot(dat, aes(x=bin, y=yeartotal, fill=FAMILY_DESC)) + geom_col() +
facet_wrap(~Year)
尽管高度并不完全符合您的图形。
由于您已经完成了分箱计数,因此您不想使用geom_bar
,因为它默认开始计数。相反,我们使用geom_col
。