0

I have two dataframes one for x and other for y values. I could use facet_grid but I need to fix x and y limits for individual plots independently which I think is not possible in ggpubr. Hence I tried to use ggplot2 in for loop. Heres my code

create list of names in data to loop over 
grid.names <- names(X1)
p <- vector("list", ncol(X1)) 

# create for loop to produce ggplot2 graphs

for (i in seq_along(grid.names)) { 

message(i)

#x and y limits for each grid
xy_min <- min(min(X1[[i]],X2[[i]])) - 100 
xy_max <- max(max(X1[[i]],X2[[i]])) + 100

# create plot for each county in df 
p[[i]] <- qplot(X1[[i]], X2[[i]]) + geom_point() +
  coord_cartesian(xlim = c(xy_min, xy_max),
                  ylim = c(xy_min, xy_max)) 
  geom_abline()
print(p[[i]])
  rm(xy_max, xy_min)


#save plots as .png
#ggsave(plot, file=paste('L:/Workspace/Trend/pcp/Seasonal',
                 enter image description here      # grid.names[i], ".png", sep=''), scale=2)enter code here

I get plots in the form of lists. But the problem is that all the plots are made of identical column, ie the last column of both dataframes. The xlim and ylim are Okay. Can someone help me figure out what's happening? I am adding sample data, but the original data has many more variables. enter image description here

enter image description here

enter image description here

UseR10085
  • 7,120
  • 3
  • 24
  • 54
Jedd
  • 11
  • 1
  • Provide the data in `dput()` format not in image format. – UseR10085 Apr 15 '20 at 14:10
  • Hi Jedd - screenshots of data are not that useful for people trying to reproducte your issue. You might want to check out this thread https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for some advice on the best way to format your question to get good help fast. Also your for loop appears to be missing it's closing curly brace in your example does it end before or after the `ggsave()` call? – Richard J. Acton Apr 15 '20 at 14:12
  • Hi Richard, thanks for the info i had no idea about dput(). Surely I will remember next time. Yes, curly braces and + sign before geom_abline() gone missing while posting. Sorry for that. Other than that can we not use list or vector indexing for plotting in for loop for plotting? Can u pls point out whats wrong in the loop. It will help me in understanding my mistake. – Jedd Apr 18 '20 at 00:23

1 Answers1

0

You can use facet_wrap with scales = "free"

library(tidyverse)
x2 <- read.table(text = "P1 P2 P3 P4 P5
                 494.7 517.6 545.6 510.0 484.9
                 526.3 532.8 581.0 588.7 542.8
                 728.0 549.8 733.4 589.8 603.4
                 864.7 717.3 755.9 607.8 636.9
                 906.5 746.5 769.5 644.9 642.2", head = T)

x1 <- read.table(text = "P1 P2 P3 P4 P5
                 1047.9 1120.4 1132.8 940.3 698.2
                 1133.1 1125.0 1287.1 985.4 715.8
                 1136.7 1210.9 1323.0 1019.6 787.3
                 1514.1 1392.0 1368.5 1030.9 794.2
                 1536.4 1435.5 1394.1 1066.5 811.2", head = T)

X1 <- x1 %>% pivot_longer(everything(), names_to = "name", values_to = "x1")

X2 <- x2 %>% pivot_longer(everything(), names_to = "name", values_to = "x2")

df <- cbind(X1,X2[-1])

df %>% ggplot(aes(x=x1, y=x2)) + geom_point() + 
facet_wrap(name~., scales = "free")

enter image description here

UseR10085
  • 7,120
  • 3
  • 24
  • 54
  • Hi Bappa, thanks for taking your time to reply, actualy, I tried that, but when i use faceting the 45 degree line is not formed correctly as the axes limits are not equal. I need to plot geom_abline(). Hence I used for loop to plot individually and combine them using ggarrange. I would be thankful if you can find why my for loop plots the last column values in every iteration (on every plot). – Jedd Apr 17 '20 at 18:05
  • See this solution to add [geom_abline in facet_wrap](https://stackoverflow.com/questions/18555431/geom-abline-and-facet-wrap-seem-incompatible). – UseR10085 Apr 17 '20 at 18:50