我试图产生两个并排的geom_violin图并缩小左边的 和右边界以及中间的分隔符这两个地块的x轴的 的主要类别。下面是一个小例子来说明我想要实现:减少两个并排geom_violins的宽度,同时保持单个小提琴的尺寸
require(dplyr)
require(ggplot2)
require(grid)
require(gridExtra)
require(cowplot)
# two major categories (cut) and five
# subcategories (color) are enough for the purpose of this question
pi <- filter(diamonds, (cut=="Premium" | cut=="Ideal") & color<"I")
pi$cut <- factor(pi$cut)
pi$color <- factor(pi$color)
th1 <- theme_bw() + theme(legend.position = "none")
g1 <- ggplot(data=pi, aes(x=cut, fill=color, y=price)) +
geom_violin(width=0.5) + th1
g2 <- ggplot(data=pi, aes(x=cut, fill=color, y=depth)) +
geom_violin(width=0.5) + th1
grid.newpage()
grid.arrange(plot_grid(g1,g2, ncol = 2, rel_widths = c(0.3,0.3)))
我也标志着我想修剪或减少的空间。 请注意,我知道增加geom_violin()中的宽度参数确实会减少这个间距;然而,我想保持小提琴的宽度为0.5,只减小剧情的总宽度(不影响小提琴的大小)。另请注意,plot_grid
函数的rel_widths
没有帮助,因为我需要两个图都是相同的宽度。
我搜索了一个类似的问题,发现this one;但是,答案使用“打印”语句,仅适用于一个绘图。任何人都可以请为我的情况概括一下这个答案吗?
在我的例子中重现,我也列举我sessionInfo()
:
R version 3.3.2 (2016-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X Yosemite 10.10.5
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] cowplot_0.7.0 gridExtra_2.2.1 ggplot2_2.2.1 tidyr_0.6.1 dplyr_0.5.0
loaded via a namespace (and not attached):
[1] Rcpp_0.12.10 assertthat_0.1 R6_2.2.0 plyr_1.8.4 gtable_0.2.0
[6] DBI_0.6-1 magrittr_1.5 scales_0.4.1 lazyeval_0.2.0 labeling_0.3
[11] tools_3.3.2 munsell_0.4.3 colorspace_1.3-2 tibble_1.3.0
编辑 我刚刚找到自己的答案。可以通过将+ scale_x_discrete(expand = c(0,0))
添加到ggplots中来删除边界间距。内部空间由宽度参数控制。
非常好;然而,你的答案有两个问题(1)不同切割类别之间的中间分隔符几乎被删除(我希望它减少,而不是删除)和(2)如果我需要左对角线尺度和右线性规模(我确实需要这个),我怎样才能用你的答案来做到这一点?这就是我使用grid.arrange的原因。无论如何,我提高了你的答案,因为我从你的答案中学到了很多东西。谢谢。 – user8420488483439
如果您在答案中使用'geom_violin(width = 0.5)'而不是'geom_violin()',那么您的情节的形状将诉诸于我的问题中的情节。 – user8420488483439
1)你应该能够在这里弹出'geom_violin(width = 0.7)+'等。玩耍,找到你最喜欢的那个。 2)我会更新日志传输的答案 – Zafar