r - aligning x-axis image and base plot - r

When I try to align an image plot with a xy plot by their x-axis there is a small misalignment between the x-values. Can't figure out how to get rid of it!
Notice the misalignment of the vertical lines from the bottom plot with the x-axis on the top plot.
par(mfrow=c(2,1))
par(mar=c(0,5,5,5))
image(x=1:100,z=replicate(10, rnorm(100)))
par(mar=c(5,5,0,5))
par(xaxs="i")
plot(1:100,rnorm(100))
abline(v=1:100)
Gurus, help!! Thank you!!
PS: Could not figure it out with the post "plot-time-series-and-image-in-r-so-that-x-axis-labels-line-up-perfectly", still having issues!

The problem is that image draws data as a grid of cells which have a given width. If the length of x in image equals to nrow(z) then it specifies the midpoints of the cells. In your example this gives you rectangles centered around 1:100, effectively resulting in the x-axis covering the range from 0.5 to 100.5, which gives the observed misalignment.
In order to match the ranges in both plots you need to specify xlim to plot accordingly. In the following example I use n = 10 to make things more obvious.
par(mfrow=c(2,1))
par(mar=c(0,5,5,5))
image(x=1:n,z=replicate(10, rnorm(n)))
par(mar=c(5,5,0,5))
par(xaxs="i")
plot(1:n,rnorm(n), xlim=c(.5,n+.5))
abline(v=1:n)

Okay, so this is one of those things I had probably figured out on the past and then completely forgot about.
The trick is not only calling par(xaxs="i") but also enforcing both xlims!
par(mfrow=c(2,1))
par(mar=c(0,5,5,5))
image(x=1:100,z=replicate(10, rnorm(100)),xlim=c(0,100))
par(mar=c(5,5,0,5))
par(xaxs="i")
plot(1:100,rnorm(100),xlim=c(0,100))
abline(v=1:100)

Related

how to increase labels distance from main plot in heatmaply r

I have a simple issue in interactive heatmap in R. I couldn't figure out how to increase margins (distance between x and y labels with main plot) in heatmaply?
I want to add a little bit of space in ylab (province) from heatmap. Could anybody help me?
enter image description here
You can change the margins with heatmaply by using margins
Since you did not provide any code in your question, I will use an example plot using mtcars and changing the y-axis margins
heatmaply(mtcars, margins = c(0, 200, 0, 0))
If you want to move the y-axis labels away from the graph, then that will be trickier. Currently, there doesn't appear to be a way to do this, however, here is a workaround. There is currently now way to move the y-axis labels in heatmaply. Overall, this is a poor workaround but it appears to be the best solution as of right now.
df <- mtcars
#extract the labels on the y axis. In this example mtcars y-axis labels are the row names
df2 <- as.data.frame(rownames(mtcars))
#you then have to add spaces to the end of the labels and then apply this to the dataframe you are using.
rownames(df) <- paste0(df2$`rownames(mtcars)`, " ")
#you can now run heatmaply on the new labels with added spaces
heatmaply(df)
Overall, plotly has some great features however it lacks in tuneability. heatmaply has even fewer features than plotly so it makes it even harder to tune your graph the way you want to.

Plotting a scaled version of a variable using ggplot2. Need to show the scale too

I am trying to plot a variable over time using ggplot2.
My current plot looks like this:
However, I want the scaled values with significant numbers shown on the axis. The scale needs to be shown at the top left corner. Something like:-
I don't want to scale the plot. The axis needs to show the significant numbers and the exponential scale needs to be shown at the top left.
Is this what you had in mind (my own MWE):
library(ggplot2)
df <- data.frame( x=rnorm(10), y=seq(1e8,10e8,by=1e8))
p <- ggplot(df)+
geom_point(aes(x=x,y=y/1e8))+
geom_text(aes(x=-Inf,y=Inf,label=as.character(paste("10^8"))), parse=T, hjust=1, vjust=0.8, size=5/14*10)+
scale_y_continuous(name="y")
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)
Basically divide by the exponent, and then write it manually at the top left. The parse=T renders the power as an actual power. That factor of 5/14 was mentioned on another post as being the rough factor by which geom_text size relates to the size you set text to be elsewhere on the graph (no idea why but I've found doing 5/14*text-size-you-want gives a decent looking geom_text). Finally the last three lines prevent the off-plot-panel text being cropped out (you wouldn't see the geom_text otherwise). Hope this gives you something to work with.

Two y-axis plot without overlapping

I am trying to make a graphic with to y-axis that the values of each y-axis start at the middle of the box so the 2 lines that I am plotting do not overlap. I have search for some time but have not found any way to do that with the basic X-Y graphics from R and i am adding some other things to the graphic so i don't want to do it with other package.
With the graphic in mind what I need it that the top one "merges" with the bottom and that the values on the left y-axis of the top graph change to the right but start at the middle, that way the lines wont overlap.
Do you mean something like this?
d1 <- data.frame(x=1:10, y=sin(1:10))
d2 <- data.frame(x=1:10, y=sin(1:10)+10)
plot(rbind(d1,d2), t="n", yaxt="n")
lines(d1)
lines(d2)
axis(2, at=pretty(d1$y))
axis(4, at=pretty(d2$y))

setting the length and width of scatter plots using R

I am trying to construct 3 scatter plots together using:
par(mfrow=c(3,1)),pty='s')
but they appear rather small on an A4 size paper. When I used:
par(mfrow=c(3,1)) only, the scatter plots were too stretched out (length wise). Is there anything that can be used to allow me to adjust the size so that they are large enough (3 plots) on an A4 size paper?
This is what I am currently using to plot the fist one:
(apirt$irt12[apirt$surv==1],
apirt$prtemp[apirt$surv==1],
xlab="ave.base of ears (°C)",
ylab="rectal (°C)",
xlim=c(26,42),
ylim=c(30,42),
col='blue')
Thanks,
Baz
I would suggest using par(mfrow=c(2,2), pty='s') instead. Then you will have a 2 by 2 layout of the graphs instead (with one empty quadrent), which will give more room for the square plotting regions. You should also make sure that your plotting device is set up to have the same dimensions as your paper. Reducing margins will also give you bigger plotting areas (just be sure to leave enough room for the annotations).
You should specify the size of the page, somewhere...
pdf(file="tmp.pdf",width=21/2,height=29.7/2)
op <- par(mfrow=c(3,1), pty="s") # or c(2,2)
plot(1:100,cumsum(rnorm(100)))
plot(1:100,cumsum(rnorm(100)))
plot(1:100,cumsum(rnorm(100)))
par(op)
dev.off()

color2D.matplot Legend in R

Does anyone know how to change the placement of the legend in color2D.matplot (plotrix)? I have a12 x 12 correlation matrix. I noticed I had to reverse the row names and change the margin to get the long names to fit. But now I am stumped on how to move the legend (without increasing the margin even more and making the graph look odd with so much white space at the bottom. Thanks!
cors<-cor(train)cellcol<-color.scale(cbind(cors,c(-1,rep(1,11))),c(0,1),0,c(1,0))[,1:12]
par(mar = c(10,8,4,2) + 0.1)
color2D.matplot(cors,cellcolors=cellcol,show.legend=TRUE,show.values=2,
axes=FALSE, xlab="",ylab="")
axis(1,at=0.5:11.5,las=2,labels=colnames(cors))
axis(2,at=0.5:11.5,las=2,labels=rev(rownames(cors)))
The help page says:
"If the default is not suitable, call color.legend separately." And then gives an example.
Based on a guess (in the absence of any information about "train" or "cors", you can try:
color.legend(0,13,6.5,14,legend=c(-1,-0.5,0,0.5,1),
rect.col=color.scale(cbind(cors,c(-1,rep(1,11))),c(0,1),0,c(1,0))[,1:12],
align="rb")

Resources