R Help needed
R Help needed
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:
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 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:
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"
Re: R Help needed
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.
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
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"
Re: R Help needed
Sent a PM with updated code to generate the above figure.
Re: R Help needed
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"
Re: R Help needed
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
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
it's okay to say "I don't know"
- Bird on a Fire
- Princess POW
- Posts: 10142
- Joined: Fri Oct 11, 2019 5:05 pm
- Location: Portugal
Re: R Help needed
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.
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.
Re: R Help needed
Oh, thanks for the tips, I'll definitely check both those out
I hope you're safe in the mudflat! Isn't it a bit dark for that sort of thing?
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"
- Stupidosaurus
- Buzzberry
- Posts: 51
- Joined: Tue Feb 11, 2020 11:11 am
Re: R Help needed
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...