Page 1 of 1

R Help needed

Posted: Mon May 04, 2020 5:38 pm
by Fishnut
I'm trying to plot some graphs using R. The graphs are of trace elements over time for individual fish from different sites. I want to make a plot for each site showing all the fish. Like this:
LAR.jpg
LAR.jpg (209.96 KiB) Viewed 2352 times
It's bl..dy tedious to do it all manually and I'm now doing a species which has a lot more specimens so I'm trying to automate things a bit. I've managed to use a for loop to generate the plots individually but I'm struggling to get them to make a multiplot so I can see all the fish from the same site in one go. I've got some code that will do it but it gives each plot their own individual legend and I can't find a way to make a single legend like the plot above. So I'm currently getting this:
LC.jpeg
LC.jpeg (102.72 KiB) Viewed 2352 times
I've tried smashing the two bits of code together but at best it does nothing and at worst it creates error messages.

I feel like this should be a trivial problem to solve but I've been trying all afternoon (and some of friday) and am failing miserably. If this does sound trivially easy to you could you help me please! I'm happy to share the code, it's mostly stuff I've copy-pasted off the interwebs anyway. I would paste it here already but I don't want everyone to laugh at how sh.t it is. (I know I could go to stack overflow but everyone there is really scary so I thought I'd try here first).

Re: R Help needed

Posted: Mon May 04, 2020 5:51 pm
by geejaytee
There's an answer here on stackoverflow (yes, I know*), using layout (and base graphics):

https://stackoverflow.com/questions/103 ... plots-in-r

or if you're using ggplot2:

https://stackoverflow.com/questions/136 ... 0#28594060

*(Oh, and regarding your exclusion - stackoverflow is a sh.tty place, I agree)

If that doesn't help, just PM me the code and some example data and I'll see what I can knock up.

Re: R Help needed

Posted: Mon May 04, 2020 7:09 pm
by Fishnut
The grid.arrange code is similar to the code I used to generate the first image. But I can't get it to work with the loop so will PM you, thanks!

Re: R Help needed

Posted: Mon May 04, 2020 8:09 pm
by geejaytee
Rplot.png
Rplot.png (21.45 KiB) Viewed 2310 times
Sent a PM with updated code to generate the above figure.

Re: R Help needed

Posted: Mon May 04, 2020 8:13 pm
by Fishnut
You're a bl..dy genius! Thank you!!! I'm too tired to be able to follow the code now but I'll have a look first thing tomorrow.

Re: R Help needed

Posted: Mon May 04, 2020 8:16 pm
by geejaytee
Fishnut wrote:
Mon May 04, 2020 8:13 pm
You're a bl..dy genius! Thank you!!! I'm too tired to be able to follow the code now but I'll have a look first thing tomorrow.
The only thing I can think of is that the legend is only at the size of a mini plot, so if you need it bigger, you need to fix your theme (or overwrite it in the original plots) to make the text larger as it takes all the attributes from the first plot.

HTH

Re: R Help needed

Posted: Mon May 04, 2020 8:25 pm
by Fishnut
I couldn't wait til morning so I took a look. The code is so short! I don't understand much of it but I tried it and it works. It helps hugely, I really can't thank you enough :D :D :D

Re: R Help needed

Posted: Mon May 04, 2020 8:26 pm
by Bird on a Fire
Are those plots with ggplot2?

If so, the cowplot package adapts ggplot defaults for scientific publications, including bigger text etc. I think you just add theme_cowplot() to your object, but I'm on my phone in a mudflat so can't check.

It also has functions for arranging multiplot, but looks like gjt has sorted you out.

Another option for future reference is the lattice package, which is designed for making these kinds of replicated miniature plots, and has much simpler syntax than ggplot2.

Re: R Help needed

Posted: Mon May 04, 2020 8:32 pm
by Fishnut
Oh, thanks for the tips, I'll definitely check both those out :D

I hope you're safe in the mudflat! Isn't it a bit dark for that sort of thing?

Re: R Help needed

Posted: Tue May 05, 2020 12:45 pm
by Stupidosaurus
Nice graphs there. I've dabbled in R a tiny bit, mostly for data wrangling rather than plots (I usually go to JMP or Prism for that). It's satisfying when stuff works but hard on the old noggin when it doesn't...