[R] Scatterplot of many variables against a single variable

Ismail SEZEN sezenismail at gmail.com
Mon Nov 27 21:18:03 CET 2017


> On 27 Nov 2017, at 11:56, Engin YILMAZ <ispanyolcom at gmail.com> wrote:
> 
> Dear
> 
> I try to realize one scatter matrix which draws *one single variable to all
> variables* with *regression line* . You can see my eviews version  in the
> annex .
> 
> How can I draw this graph with R studio?

A tiny note; You do calculations in R not RSudio. RStudio is a tool (IDE) to use R in an easy way.

The code below shows how to accomplish this task easily by ggplot. It’s adapted from [1].

library(ggplot2)
library(reshape2)

# This is your initial data.frame and you want scatterplots of all variables against x1.
foo <- data.frame(x1 = runif(50, 0, 1), 
                  x2 = runif(50, 0, 1), 
                  x3 = runif(50, 0, 1), 
                  x4 = runif(50, 0, 1))

# melt data. This is very handy function from reshape2 library.
foo2 <- melt(foo, "x1”)

# plot points and add lm lines.
ggplot(foo2, aes(value, x1)) +  
  geom_point() + 
  geom_smooth(method=lm) +
  facet_grid(.~variable)


1- https://stackoverflow.com/questions/24648729/plot-one-numeric-variable-against-n-numeric-variables-in-n-plots

isezen


More information about the R-help mailing list