Изменить метки оси измененного графика ggpairs (тепловая карта корреляции) - PullRequest
1 голос
/ 30 марта 2020

Я хотел объединить график ggpairs с тепловой картой и нашел замечательное решение: график ggpairs с тепловой картой значений корреляции

#library
library(GGally)
library(ggplot2)

#data
sample_df <- data.frame(replicate(7,sample(0:5000,100)))
colnames(sample_df) <- c("KUM", "MHP", "WEB", "OSH", "JAC", "WSW", "gaugings")

#function for heatmap
my_fn <- function(data, mapping, method="p", use="pairwise", ...){

          # grab data
          x <- eval_data_col(data, mapping$x)
          y <- eval_data_col(data, mapping$y)

          # calculate correlation
          corr <- cor(x, y, method=method, use=use)

          # calculate colour based on correlation value
          # Here I have set a correlation of minus one to blue, 
          # zero to white, and one to red 
          # Change this to suit: possibly extend to add as an argument of `my_fn`
          colFn <- colorRampPalette(c("blue", "white", "red"), interpolate ='spline')
          fill <- colFn(100)[findInterval(corr, seq(-1, 1, length=100))]

          ggally_cor(data = data, mapping = mapping, ...) + 
            theme_void() +
            theme(panel.background = element_rect(fill=fill))
        }

#combine
ggpairs(sample_df, 
                   upper = list(continuous = my_fn),
                   lower = list(continuous = "smooth"))

Поскольку метки оси не подходят в моих реальных данных я хотел бы изменить их (угол), и я нашел это решение:

ggpairs повернуть метку оси

Однако, если я добавлю это, я потерять карту тепла

ggpairs(sample_df,
        upper = list(continuous = my_fn),
        lower = list(continuous = "smooth")) +
        theme(axis.text.x = element_text(angle = 90, hjust = 1, size=8))

Я также пытался добавить тему к my_fn, но безуспешно.

1 Ответ

2 голосов
/ 30 марта 2020

Как насчет:

  ggpairs(sample_df, 
        upper = list(continuous = my_fn),
        lower = list(continuous = "smooth"))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1, size=8))

enter image description here

Версии пакетов: ggplot2 3.3.0, GGally 1.4.0

...