Plotting in R multiple graphs without spaces - r

I’m aware that how to plot multiple graphs in a same window has been solved already and it’s pretty straight forward. But I can’t find how to remove the space between the different graphs.
I use this script to arrange these three graphs in the same window:
mat<-(matrix(1:3,ncol=3))
layout(mat,widths = rep.int(1, ncol(mat)),heights = rep.int(1,nrow(mat)),respect =F)
layout.show(n = 3)
With this script I can generate this graph:
These three violin plots where obtained from a large dataset. I don’t publish the script to get them since I think is not relevant for the purpose of this question.
As you can see this figure can be improved. First the labels at the x-axis are cut from the figure, I don’t know why this is happening. Also I want to remove the space between the three plots. Thanks!

Related

geom_bspline across multiple plots combined into a single figure

I would like to create a ggplot2 layer that includes multiple geom_bspline(), or something similar, to point to regions on different plots after combining them into a single figure. A feature in the data seen in one plot appears in another plot after a transformation. However, it may not be clear to a non-expert they are due to the same phenomenon. The plots are to be combined into a single figure using ggarrange(), cowplot(), patchwork() or something similar.
I can get by using ggforce::geom_ellipse() on each plot but it's not as clean. Any suggestions?
Of course, after asking the question and staring at the figure in question, it came to me that I simply need to add a geom_bspline() to the combined figure. Tried that earlier but didn't give enough thought to the coordinates on the new layer. The coordinates of the spline are given in the range of 0 to 1 for both the x and y values on this new layer. Simple and obvious.

Scatter plots in R. Why am I getting boxes around certain points?

Apologies, this is probably the simplest question. I'm having trouble making a scatterplot in R Studio. I am trying to see if amphipod counts are correlated to oxygen content. Whenever I plot this using:
plot(Amphipod~Oxygen...ml.l.)
I get a graph with boxes around certain points and I have no idea why. Only 5 points and I can't see anything different about those.

Dealing with Large Legends in R Plots - Complicated Heatmap Example

I'm working on a really complicated heatmap figure in R. heatmap.2 from the gplots package is not enough for me, because I want multiple sidebar annotations, such as the heatmap.3 function permits: https://www.biostars.org/p/18211/
Here's my specific plot so far:
When I have multiple sidebar annotations on this heatmap, though, they need to be properly labeled with a legend. The legend quickly becomes unwieldy and starts bleeding off the plot or into other labels on the plot, depending on where I choose to place the legend.
I've tried using the ncols option when placing the legend at the bottom of the plot but the legend contains information about several factors worth of metadata, and I want a separate column in the legend to denote each sidebar's worth of metadata. As far as I know there is no option in the legend command to permit this functionality, so I'm interested in hearing potential ways around this.
Alternatively, I am also open to the idea of simply generating a legend image with R separately if anyone knows how to do this.
Thanks!

R - Adding series to multiple plots

I have the following plot:
plot.ts(returns)
I have another dataframe ma_sd which contains the rolling SD from moving averages of the above returns. The df is structured exactly like returns. Is there a simple way to add each line to the corresponding plots?
lines(1:N, ma_sd) seemed intuitive, but it does not work.
Thanks
The only way I can see you doing this is to plot them separately. This code is a bit clunky but will allow you full flexibility to be able to specify labels and axis ranges. You can build on this.
par(mfrow=c(3,1),oma=c(5,4,4,2),mar=c(0,0,0,0))
time<-as.data.frame(matrix(c(1:length(returns[,1])),length(returns[,1]),3))
plot(time[,1],returns[,1],type='l',xaxt='n')
points(time[,1],ma_sd[,1],type='l',col='red')
plot(time[,2],returns[,2],type='l',xaxt='n')
points(time[,2],ma_sd[,2],type='l',col='red')
plot(time[,3],returns[,3],type='l')
points(time[,3],ma_sd[,3],type='l',col='red')

Population Pyramid in Plotrix freezes r [duplicate]

I am trying make a pyramid plot with R. The I found a example code in the internet that does what I want. The problem is that I am not working with small numbers as in the example. My plot has values of 3,000,000 to 12,000,000 but only 10 bars per side. Never the less it takes for ever create the plot with the larger numbers and output pdf file is about 800mb of size.
pyramid.plot(x,y,labels=groups,main="Performance",lxcol=mcol,rxcol=fcol,gap=0.5,show.values=TRUE)
Why is the performance so bad? Shouldn't get scaled automatically?
Update:
pdf(file='figure1.pdf')
library(plotrix)
x <-c(3105000,3400001,4168780,2842764,3543116,4224601,4222222,6432105,9222222,12345596)
y <-c(3105000,3400001,4168780,2842764,3543116,4224601,4222222,6432105,9222222,12345596)
groups <-c("g1","g2","g3","g4","g5","g6","g7","g8","g9","g11")
pyramid.plot(x,y,labels=groups,main="Performance",gap=0.5,show.values=TRUE)
dev.off()
Both the export to pdf as well as the plotting screen takes multiple minutes.
Internally, pyramid.plot is trying to do some stuff to finagle the axes accounting for the gap in the middle: if you do debug(pyramid.plot) and step through line-by-line you find where the problem is:
if (is.null(laxlab)) {
laxlab <- seq(xlim[1] - gap, 0, by = -1)
axis(1, at = -xlim[1]:-gap, labels = laxlab)
}
in other words, pyramid.plot is trying to make an axis with ticks every 1 (!) unit.
Something like this works OK:
pyramid.plot(x,y,labels=groups,
main="Performance",gap=5e5,show.values=TRUE,
laxlab=seq(0,1e7,by=1e6),raxlab=seq(0,1e7,by=1e6))
there are a few other vestiges of the fact that pyramid.plot was designed for demographic plots ... you might write to the package maintainer and ask him to think about generalizing the design of the axes a little bit ...

Resources