2017-06-14 26 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