2017-06-14 25 views
0

我有一堆ggplot() s,有两层:geom_boxplotgeom_pointsR-gridExtra - ggplot()的某一层不缩放

当我使用gridExtra在网格中插入这些字段时,geom_boxplot将缩放,但geom_point没有,这导致了一些不好看的情况(请参阅下图)。

请问我该如何解决这个问题?

重复性代码:

library(ggplot2) 
library(gridExtra) 
datablock <- data.frame(date = rep(1:10, 3) 
       , value = rnorm(30, 3,2) 
       , name = c(rep("one",10), rep("two",10), rep("three",10))) 
currentValues <- data.frame(date = rep(1,3) 
       , value = c(3, 2.3, 3.5) 
       , name = c("one","two", "three")) 
boxplotFg <- 
    ggplot(datablock, aes(x = name, y = value)) + geom_boxplot(outlier.shape=NA) + 
    geom_point(data=currentValues, aes(x=name, y=value, color = value), size = 8) 


grid.arrange(boxplotFg,boxplotFg, boxplotFg, boxplotFg,boxplotFg, boxplotFg, ncol = 3) 

输出:

enter image description here

我当然可以降低geom_point S的,比方说,4或5的大小...但我觉得改变绝对尺寸并不是正确的方式,因为它只能解决问题。

+1

控制在GGPLOT2绘制点的相对大小:https://stackoverflow.com/questions/3870303/controlling- ggplot2中的相对点大小 –

+1

@MarcoSandri使用该方法控制点的纵横比将是非常具有挑战性的。特别是因为x和y方向上的相对缩放比例需要根据图形输出的大小而改变,所以不容易通过算法确定。 – dww

+0

网格视口旨在提供一些继承属性来缩放与父上下文相关的grob大小,但AFAIK从未真正以系统的方式使用/实现过。例如,考虑'print(qplot(1,1)+ theme_bw(2),vp = viewport(gp = gpar(cex = 10)))' – baptiste

回答

0

也许最简单的解决方案是根据网格列的数量缩放点大小。

nc = 3 
boxplotFg <- 
    ggplot(datablock, aes(x = name, y = value)) 
    geom_boxplot(outlier.shape=NA) + 
    geom_point(data=currentValues, aes(x=name, y=value, color = value), 
     size = 8/nc) 
grid.arrange(boxplotFg, boxplotFg, boxplotFg, boxplotFg,boxplotFg, boxplotFg, 
    ncol = nc) 

enter image description here

相同的代码但nc=2产生

enter image description here