Plot one data frame column against all other colum

2019-08-21 16:17发布

I have a data frame with 20 columns, and I want to plot one specific column (called BB) against each single column in the data frame. The plots I need are probability density plots, and I’m using the following code to generate one plot (plotting columns BB vs. AA as an example):

mydata = as.data.frame(fread("filename.txt")) #read my data as data frame

#function to calculate density
get_density <- function(x, y, n = 100) {
  dens <- MASS::kde2d(x = x, y = y, n = n)
  ix <- findInterval(x, dens$x)
  iy <- findInterval(y, dens$y)
  ii <- cbind(ix, iy)
  return(dens$z[ii])
}
set.seed(1)

#define the x and y of the plot; x = column called AA; y = column called BB
xy1 <- data.frame(
  x = mydata$AA,
  y = mydata$BB
)

#call function get_density to calculate density for the defined x an y
xy1$density <- get_density(xy1$x, xy1$y) 

#Plot
ggplot(xy1) + geom_point(aes(x, y, color = density), size = 3, pch = 20) + scale_color_viridis() +
  labs(title = "BB vs. AA") +
  scale_x_continuous(name="AA") +
  scale_y_continuous(name="BB")

Would appreciate it if someone can suggest a method to produce multiple plot of BB against every other column, using the above density function and ggplot command. I tried adding a loop, but found it too complicated especially when defining the x and y to be plotted or calling the density function.

1条回答
乱世女痞
2楼-- · 2019-08-21 16:23

Since you don't provide sample data, I'll demo on mtcars. We convert the data to long format, calculate the densities, and make a faceted plot. We plot the mpg column against all others.

library(dplyr)
library(tidyr)
mtlong = gather(mtcars, key = "var", value = "value", -mpg) %>%
    group_by(var) %>%
    mutate(density = get_density(value, mpg))

ggplot(mtlong, aes(x = value, y = mpg, color = density)) +
    geom_point(pch = 20, size = 3) +
    labs(x = "") +
    facet_wrap(~ var, scales = "free")

enter image description here

查看更多
登录 后发表回答