Reputation: 23
I want to create a ggpairs plot colored by a factor, in order to do that I have to keep the factor column in the data frame that goes into ggpairs().
The problem with that is that it adds plots that are done with the factor (the last column and last line in the plot), which I do not want to have in the ggpairs plot (they only add a limited amount of information and make the plot messy).
Is there a way to not show them in the plot, or alternatively color by a factor which is in a separate dataframe? I was able to remove the whole top part of the plot by using: upper = 'blank' but it doesn't really help as I cannot remove by columns or rows of the ggmatrix. Is there a way to do this?
I searched for solutions but I didn't find anything relevant
here is an example using the gapminder dataset:
library(dplyr)
library(ggplot2)
library(GGally)
library(gapminder)
gapminder %>%
filter(year == 2002 & continent != 'Oceania') %>%
transmute(lifeExp = lifeExp, log_pop = log(pop), log_gdpPercap = log(gdpPercap), continent = continent) %>%
ggpairs(aes(color = continent, alpha = 0.5))
I get this: ggpairs with the factor
and I would like to get something like this: ggpairs colored by factor but without its related plots
Upvotes: 2
Views: 2277
Reputation: 36076
You can use the columns
argument for this.
From the documentation:
which columns are used to make plots. Defaults to all columns.
In your example output you want only columns 1:3.
... %>%
ggpairs(aes(color = continent, alpha = 0.5), columns = 1:3)
Upvotes: 2