3

我正在做一个 ggpairs 绘图,但回归线太粗而且“Corr:”文本字体太大。

data(mtcars)
head(mtcars)

mtcars$am <- as.factor(mtcars$am)

g <- ggpairs( 
  data = mtcars,
  lower = list(
    continuous = wrap("smooth", alpha = 0.3, color = "blue") 
    )
  )
g <- g + theme(
    axis.text = element_text(size = 6),
    axis.title = element_text(size = 6),
    legend.background = element_rect(fill = "white"),
    panel.grid.major = element_line(colour = NA),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "grey95")
  )
print(g, bottomHeightProportion = 0.5, leftWidthProportion = .5)

这是输出:

在此处输入图像描述

我在GGally文档中找不到可以设置它的位置。

任何指针?

4

3 回答 3

3

这个怎么样?

lowerFn <- function(data, mapping, ...) {
  p <- ggplot(data = data, mapping = mapping) +
    geom_point(color = 'blue', alpha=0.3, size=4) +
    geom_smooth(color = 'black', method='lm', size=1,...)
  p
}

g <- ggpairs( 
  data = mtcars,
  lower = list(
    continuous =  wrap(lowerFn)
  )
)
g <- g + theme(
  axis.text = element_text(size = 6),
  axis.title = element_text(size = 6),
  legend.background = element_rect(fill = "white"),
  panel.grid.major = element_line(colour = NA),
  panel.grid.minor = element_blank(),
  panel.background = element_rect(fill = "grey95")
)
print(g, bottomHeightProportion = 0.5, leftWidthProportion = .5)

在此处输入图像描述

于 2016-09-26T18:40:41.940 回答
3

试试这个来增加字体大小:

data(mtcars)
head(mtcars)

mtcars$am <- as.factor(mtcars$am)

library(ggplot2)
library(GGally)

lowerFn <- function(data, mapping, ...) {
  p <- ggplot(data = data, mapping = mapping) +
    geom_point(color = 'blue', alpha=0.3, size=4) +
    geom_smooth(color = 'black', method='lm', size=1,...)
  p
}

g <- ggpairs( 
  data = mtcars,
  lower = list(
    continuous =  wrap(lowerFn) #wrap("smooth", alpha = 0.3, color = "blue", lwd=1) 
  ),
  upper = list(continuous = wrap("cor", size = 5))
)
g <- g + theme(
  axis.text = element_text(size = 6),
  axis.title = element_text(size = 6),
  legend.background = element_rect(fill = "white"),
  panel.grid.major = element_line(colour = NA),
  panel.grid.minor = element_blank(),
  panel.background = element_rect(fill = "grey95")
)
print(g, bottomHeightProportion = 0.5, leftWidthProportion = .5)

在此处输入图像描述

于 2016-09-27T05:10:37.823 回答
1

@Chris Snow:对函数使用函数的upper参数。将解决您的问题,但是我还添加了以防您也想更改颜色。礼貌:更改 ggpairs 中的颜色,因为参数已被弃用ggpairswrapggally_corsize = 2color = "black"

修改后的 MWE 为:

data(mtcars)
head(mtcars)

mtcars$am <- as.factor(mtcars$am)

g <- ggpairs( 
  data = mtcars,
  lower = list(
    continuous = wrap("smooth", alpha = 0.3, color = "blue") 
    ),
  upper = list(continuous = wrap(ggally_cor, size = 2, color = "black")))
g <- g + theme(
    axis.text = element_text(size = 6),
    axis.title = element_text(size = 6),
    legend.background = element_rect(fill = "white"),
    panel.grid.major = element_line(colour = NA),
    panel.grid.minor = element_blank(),
    panel.background = element_rect(fill = "grey95")
  )
print(g, bottomHeightProportion = 0.5, leftWidthProportion = .5)
于 2017-10-09T07:03:06.490 回答