我正在更新ggplot2(2.2.0)。根据this页面,一个很棒的新功能是可以沿着每个x轴绘制不均匀数量的图。如何使用facet_wrap在ggplot2中包含所有x轴标签,包括缺失数据?
我想绘制在每个面的相同的x轴。但是,当缺少数据时,这不起作用。作为一个例子,我将借鉴this问题中的示例数据。
# Example dataset
dataf <- data.frame(x=c(1:30), A=rnorm(30,20,5), B=rnorm(30,15,0.5))
datam <- melt(dataf, id="x")
# Plot in ggplot2
ggplot(datam, aes(factor(x), value)) +
geom_bar(stat="identity") +
facet_wrap(~variable,nrow = 2,scales = "free")
当我删除特定行并引入缺失数据(如每低于我代码),我不再具有相同的x轴标签两者“A”和“B”。
# Remove certain rows to introduce missing data
datam <- datam[-c(2, 4, 58), ]
我可以绘制缺失值和对齐用scales = fixed
底部x轴虽然我不再有X轴的刻度标记标签在所述小基团“A”。
ggplot(datam, aes(factor(x), value)) +
geom_bar(stat="identity") +
facet_wrap(~variable,nrow = 2,scales = "fixed")
即使在缺少数据的情况下,每个方面如何显示完全相同的x轴标签?
谢谢。
@hadley - 是否有ggplot2中的功能,请不要在下面的答案中使用解决方法以上做到上述做? – user2716568