使用facet_grid和删除未使用的水平coord_flip在GGPLOT2
问题描述:
我有一个数据帧df
使用facet_grid和删除未使用的水平coord_flip在GGPLOT2
df<- structure(list(Categorie = structure(c(1L, 1L, 2L, 2L, 4L, 4L,
3L, 1L, 1L, 2L, 2L, 4L, 4L, 3L, 1L, 1L, 2L, 2L, 4L, 4L, 3L, 1L,
1L, 2L, 2L, 4L, 4L, 3L, 1L, 1L, 2L, 2L, 4L, 4L, 3L, 1L, 1L, 2L,
2L, 4L, 4L, 3L, 1L, 1L, 2L, 2L, 4L, 4L, 3L, 1L, 1L, 2L, 2L, 4L,
4L, 3L), .Label = c("Age classes", "Climate", "Nutrient availability",
"PFT"), class = "factor"), Sub_categories = structure(c(7L, 4L,
6L, 1L, 3L, 2L, 5L, 7L, 4L, 6L, 1L, 3L, 2L, 5L, 7L, 4L, 6L, 1L,
3L, 2L, 5L, 7L, 4L, 6L, 1L, 3L, 2L, 5L, 7L, 4L, 6L, 1L, 3L, 2L,
5L, 7L, 4L, 6L, 1L, 3L, 2L, 5L, 7L, 4L, 6L, 1L, 3L, 2L, 5L, 7L,
4L, 6L, 1L, 3L, 2L, 5L), .Label = c("Continental", "DBF", "ENF",
"Intermediate-Old", "Low-High", "Temperate", "Young"), class = "factor"),
Variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L), .Label = c("Age", "Clay content", "GPP", "GPP*Age",
"GPP*P", "GPP*P trend", "N availability", "SPI"), class = "factor"),
Importance = c(19.2, 23.7, 45.2, 45.7, 39, 66.8, 34.8, 10.9,
16.2, 9.2, 6.3, 15.7, 2.1, 10, 13.2, 7.1, 6.1, 3.8, 2.4,
7.3, 5.2, 6.4, 10, 6.6, 3.7, 2.9, 5.8, 8.4, 17.7, 0, 6.1,
5.2, 8.4, 2.8, 6.7, 11.8, 21.1, 9.8, 21.9, 20, 6.3, 13.5,
2.3, 7.6, 3.9, 1.3, 3.9, 0.4, 3.8, 10.9, 7.5, 4.5, 5.8, 0.3,
2.5, 9.4)), .Names = c("Categorie", "Sub_categories", "Variable",
"Importance"), class = "data.frame", row.names = c(NA, -56L))
我希望做一个facet_grid绘制我的数据。与Categorie
和Sub_categories
变量。我运行以下命令行:
library(ggplot2)
ggplot(data = var_Imp) +
geom_bar(mapping = aes(x = Variable, y = Importance, fill=Variable), width = 1, stat= "identity", position = "stack") +
coord_flip() +
facet_grid(Categorie~Sub_categories, scales="free", space="free", shrink=TRUE, drop=TRUE)+
theme_bw(base_size = 14, base_family = "Helvetica")+
theme(axis.ticks.length=unit(-0.25, "cm"),
legend.position="none",
legend.box="horizontal",
legend.key = element_blank(),
legend.text=element_text(size=14),
axis.text.x = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")),
axis.text.y = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")),
axis.ticks.y=element_blank())+
xlab("") +
ylab("Relative contribution [%]")+
scale_fill_brewer(type = "div")
然而,在某些方面的任何信息的水平仍然绘制虽然他们不应该因为它没有任何信息。我认为scales="free"
和space="free"
参数可以完成这项工作,但显然不是。任何人都知道我可以如何绘制我的数据而不使用未使用的级别?谢谢
答
我不知道在ggplot2中的特定命令。两种可能的选择:
1)任何在这个岗位ggplot2: How to force the number of facets with too few plots?
2)提出的选项导出的.SVG(或其它矢量格式的情节),使用Inkscape或任何其他程序打开它,并删除空方面
请参阅['?droplevels'](http://stackoverflow.com/questions/1195826/drop-factor-levels-in-a-subsetted-data-frame)。 –
这个问题更多来自'categorie'和'Sub_categories'变量之间的交互。从理论上讲,所有的级别都只是用于一些交互。 –