r - How to plot multiple graphs on one plot using a different columns in a dataframe? -
i have data frame containing 4 columns, want plot column 1 against column 2 , column 3 against column 4 using qplot , have plots on same graph, need extend dataframe 20 columns. can give
edit below example of data frame i'm working with:
1 2 3 4 1 0.01795918 0.9755562 0.02040816 0.05259072 2 0.04244898 0.9455753 0.03591837 0.03864464 3 0.05224490 0.9816900 0.06122449 0.03280435 4 0.07183673 0.9635419 0.08000000 0.03453257 5 0.09551020 0.9821122 0.10040816 0.03134642 6 0.12000000 0.9354895 0.11510204 0.03920271 7 0.13877551 0.9703654 0.13877551 0.03588973 8 0.16244898 0.9506424 0.15836735 0.03402917 9 0.17224490 0.9610043 0.18530612 0.03621932 10 0.20000000 0.9863483 0.19591837 0.03021983 11 0.22122449 0.9845782 0.22530612 0.03268187 12 0.22938776 0.9835922 0.22530612 0.03513692 .... i can plot 1 against other, need way plot them onto 1 graph
reproducible example
df <- data.frame(a=runif(20), b=runif(20), c=runif(20), d=runif(20)) some data prep
reorganize data into
library(dplyr) library(tidyr) df1 <- df %>% gather(key, value) %>% # convert long format mutate(grp = rep(1:(ncol(df)/2), each=(nrow(df)*2))) %>% # each pairs of columns gets unique grouping value mutate(index = rep(1:nrow(df), ncol(df))) %>% # each observation in each group gets unique value mutate(key = rep(rep(c("x","y"), each=nrow(df)), ncol(df)/2)) %>% # label x , y spread(key, value) # convert wide format again grp index x y 1 1 1 0.4820801 0.47761962 2 1 2 0.5995658 0.86120948 3 1 3 0.4935413 0.43809711 4 1 4 0.1862176 0.24479728 5 1 5 0.8273733 0.07067905 # etc basic ggplot solution
uses facet_wrap make n plots n grp
library(ggplot2) ggplot(data=df1, aes(x=x, y=y)) + geom_point() + facet_wrap(~grp) plotting data in 1 plot using geom_smooth
ggplot(data=df1, aes(x=x, y=y, colour=factor(grp))) + geom_smooth()
Comments
Post a Comment