Как построить диаграмму рассеяния переменной в кадре данных со всеми другими переменными на одном графике с помощью R?

Я попытался использовать ggpairs в GGally для построения диаграмм рассеяния. Но он отображает каждую переменную со всеми другими переменными. Я хочу построить диаграмму рассеяния конкретной переменной по сравнению со всеми другими переменными в кадре данных со значением корреляции каждой отображаемой диаграммы рассеяния. Помогите мне, пожалуйста. Заранее спасибо.


person Community    schedule 24.07.2017    source источник


Ответы (1)


Ответ на самом деле находится в одном поиске Google, но вот.

library(ggplot2)
library(grid)

# google search: "multiplot ggplot2"
# http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {


    # Make a list from the ... arguments and plotlist
    plots <- c(list(...), plotlist)

    numPlots = length(plots)

    # If layout is NULL, then use 'cols' to determine layout
    if (is.null(layout)) {
        # Make the panel
        # ncol: Number of columns of plots
        # nrow: Number of rows needed, calculated from # of cols
        layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                         ncol = cols, nrow = ceiling(numPlots/cols))
    }

    if (numPlots==1) {
        print(plots[[1]])

    } else {
        # Set up the page
        grid.newpage()
        pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

        # Make each plot, in the correct location
        for (i in 1:numPlots) {
            # Get the i,j matrix positions of the regions that contain this subplot
            matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

            print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                            layout.pos.col = matchidx$col))
        }
    }
}

# some dummy data
x <- as.data.frame(matrix(rnorm(100), 10, 10))

# plot the first variable against all others
plotList <- list()
for (i in 1:9) {
    plotList[[i]] <- ggplot(data = x, aes_(x = x[, 1], y = x[, i+1])) + geom_point() + xlab("x") + ylab("y")
}

# actually draw the multiplot
multiplot(plotlist = plotList, cols = 3)
person Vandenman    schedule 24.07.2017
comment
Спасибо. Это было действительно полезно. - person ; 24.07.2017