R Help needed

Get your science fix here: research, quackery, activism and all the rest
Post Reply
User avatar
Fishnut
After Pie
Posts: 2447
Joined: Mon Nov 11, 2019 1:15 pm
Location: UK

R Help needed

Post by Fishnut » Mon May 04, 2020 5:38 pm

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 2282 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 2282 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).
it's okay to say "I don't know"

geejaytee
Clardic Fug
Posts: 185
Joined: Mon Nov 11, 2019 1:32 pm
Location: Norf Landan, mate

Re: R Help needed

Post by geejaytee » Mon May 04, 2020 5:51 pm

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.

User avatar
Fishnut
After Pie
Posts: 2447
Joined: Mon Nov 11, 2019 1:15 pm
Location: UK

Re: R Help needed

Post by Fishnut » Mon May 04, 2020 7:09 pm

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!
it's okay to say "I don't know"

geejaytee
Clardic Fug
Posts: 185
Joined: Mon Nov 11, 2019 1:32 pm
Location: Norf Landan, mate

Re: R Help needed

Post by geejaytee » Mon May 04, 2020 8:09 pm

Rplot.png
Rplot.png (21.45 KiB) Viewed 2240 times
Sent a PM with updated code to generate the above figure.

User avatar
Fishnut
After Pie
Posts: 2447
Joined: Mon Nov 11, 2019 1:15 pm
Location: UK

Re: R Help needed

Post by Fishnut » 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.
it's okay to say "I don't know"

geejaytee
Clardic Fug
Posts: 185
Joined: Mon Nov 11, 2019 1:32 pm
Location: Norf Landan, mate

Re: R Help needed

Post by geejaytee » Mon May 04, 2020 8:16 pm

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

User avatar
Fishnut
After Pie
Posts: 2447
Joined: Mon Nov 11, 2019 1:15 pm
Location: UK

Re: R Help needed

Post by Fishnut » Mon May 04, 2020 8:25 pm

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
it's okay to say "I don't know"

User avatar
Bird on a Fire
Princess POW
Posts: 10137
Joined: Fri Oct 11, 2019 5:05 pm
Location: Portugal

Re: R Help needed

Post by Bird on a Fire » Mon May 04, 2020 8:26 pm

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.
We have the right to a clean, healthy, sustainable environment.

User avatar
Fishnut
After Pie
Posts: 2447
Joined: Mon Nov 11, 2019 1:15 pm
Location: UK

Re: R Help needed

Post by Fishnut » Mon May 04, 2020 8:32 pm

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?
it's okay to say "I don't know"

User avatar
Stupidosaurus
Buzzberry
Posts: 51
Joined: Tue Feb 11, 2020 11:11 am

Re: R Help needed

Post by Stupidosaurus » Tue May 05, 2020 12:45 pm

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...

Post Reply