我有一个使用ggplot2和以下数据创建的组合分组/堆叠条形图:
Type Category Count Blank
CAT1 Total 36 Not Blank
CAT1 Researched 36 Not Blank
CAT1 Structured 36 Not Blank
CAT1 Ingested 36 Not Blank
CAT1 Researched 0 Blank
CAT1 Structured 0 Blank
CAT1 Ingested 0 Blank
CAT2 Total 36 Not Blank
CAT2 Researched 36 Not Blank
CAT2 Structured 36 Not Blank
CAT2 Ingested 36 Not Blank
CAT2 Researched 0 Blank
CAT2 Structured 0 Blank
CAT2 Ingested 0 Blank
CAT3 Total 72 Not Blank
CAT3 Researched 72 Not Blank
CAT3 Structured 72 Not Blank
CAT3 Ingested 72 Not Blank
CAT3 Researched 0 Blank
CAT3 Structured 0 Blank
CAT3 Ingested 0 Blank
CAT4 Total 36 Not Blank
CAT4 Researched 36 Not Blank
CAT4 Structured 36 Not Blank
CAT4 Ingested 36 Not Blank
CAT4 Researched 0 Blank
CAT4 Structured 0 Blank
CAT4 Ingested 0 Blank
CAT5 Total 108 Not Blank
CAT5 Researched 108 Not Blank
CAT5 Structured 108 Not Blank
CAT5 Ingested 108 Not Blank
CAT5 Researched 0 Blank
CAT5 Structured 0 Blank
CAT5 Ingested 0 Blank
CAT6 Total 18 Not Blank
CAT6 Researched 18 Not Blank
CAT6 Structured 18 Not Blank
CAT6 Ingested 18 Not Blank
CAT6 Researched 0 Blank
CAT6 Structured 0 Blank
CAT6 Ingested 0 Blank
CAT7 Total 36 Not Blank
CAT7 Researched 36 Not Blank
CAT7 Structured 36 Not Blank
CAT7 Ingested 36 Not Blank
CAT7 Researched 0 Blank
CAT7 Structured 0 Blank
CAT7 Ingested 0 Blank
CAT8 Total 210 Not Blank
CAT8 Researched 150 Not Blank
CAT8 Structured 150 Not Blank
CAT8 Ingested 150 Not Blank
CAT8 Researched 60 Blank
CAT8 Structured 60 Blank
CAT8 Ingested 60 Blank
CAT9 Total 60 Not Blank
CAT9 Researched 36 Not Blank
CAT9 Structured 36 Not Blank
CAT9 Ingested 36 Not Blank
CAT9 Researched 24 Blank
CAT9 Structured 24 Blank
CAT9 Ingested 24 Blank
CAT10 Total 120 Not Blank
CAT10 Researched 90 Not Blank
CAT10 Structured 90 Not Blank
CAT10 Ingested 90 Not Blank
CAT10 Researched 30 Blank
CAT10 Structured 30 Blank
CAT10 Ingested 30 Blank
然后使用此代码,我得到这张图像,该图像准确地显示了分组图表和堆叠图表,如何显示它们。
library(ggplot2)
example <- read.delim("example.txt")
example$Category <- factor(example$Category, levels = c("Total", "Researched", "Structured", "Ingested"))
ggplot(data = example, aes(x=Category, y = Count, fill = Blank)) + geom_bar(stat = "identity") + facet_grid(~Type)
上面图像的问题是每个构面中的4个单独的条具有相同的颜色,唯一的区别在于堆叠。如何更改ggplot2行,以使构面中的4个条形图各具有不同的颜色,同时仍显示堆叠的空白和非空白之间的分隔?
我尝试将填充选项更改为“类别”,这将提供所需的配色方案,但不会以堆叠图表的形式显示空白与非空白。一如既往地感谢您的帮助。