Border subplots in in subplot in R with Layout - r

I have a question. I know it is not clear. However, I have 8 figures in 4 subplots. I want to border a line for each two of them and put the number around them. I attached a figure as follows. Could you please help me with that? I used the following simple example to plot this figures:
m1 <- matrix(c(
1, 2, 3, 4,
5, 6, 7, 8), nrow = 2, ncol = 4, byrow = TRUE)
layout(m1, widths = c(2.2, 1.5,2.2, 1.5))
par(mar=c(1,7,4,2))

Related

R: Exclude granges overlapped by another range

I would compute the remained part from a query range after excluding another range, is there a way to do it? Thanks.
query <- IRanges(1, 10)
ranges2exclude <- IRanges(c(2, 6), c(3, 7))
The output I want:
IRanges(c(1, 4, 8), c(1, 5, 10))

Automatically insert whitespace in RStudio script

In the same ways that lines can be correctly indented using Ctrl + I or Cmd + I, is there a shortcut to automatically insert correct whitespace in RStudio scripts?
For example, for this:
df<-data.frame(x=c(1,2,3,4,5),y=c(3,4,5,6,7))
RStudio gives information saying "expected whitespace around '<-' operator" and "expected whitespace around '=' operator". Is there a shortcut to get this:
df <- data.frame(x = c(1, 2, 3, 4, 5), y = c(3, 4, 5, 6, 7))
Under RStudio you can select the code and type ctrl+shift+A for code reformatting, see RStudio shortcuts.
Result:
df <- data.frame(x = c(1, 2, 3, 4, 5), y = c(3, 4, 5, 6, 7))

How do I manually assign the location of bars along the x-axis using barplot()?

I have a list of means.
means=c(anc3mean,l3mean,anc5mean,l5mean,anczmean,z12mean);means
[1] 0.07025897 0.42328670 0.05697524 0.53915431 0.01893219 0.10878638
I have created a barplot from those means.
bars=barplot(means,ylim=c(0,.8))
I would like to have the "bars" be centered at the values 1, 2, 4, 5, 7, 8 along the x axis.
I have tried the following:
bars=barplot(means,ylim=c(0,.8),at=c(1,2,4,5,7,8))
This does not work however because "at" is not an argument for "barplot()".
Any help would be greatly appreciated.
You could insert "empty" elements
# x position of the means
loc <- c(1, 2, 4, 5, 7, 8)
# Fill with empty elements
hght <- setNames(rep(0, max(loc)), 1:max(loc))
hght[loc] <- means
# Plot
x = barplot(hght, ylim = c(0, 0.8))
axis(1, at = x, labels = names(hght))
If you don't want the x-axis, simply remove the axis(...) line.
Sample data
means <- c(0.07025897, 0.42328670, 0.05697524, 0.53915431, 0.01893219, 0.10878638)

Line in R plot should start at a different timepoint

I have the following example data set:
date<-c(1,2,3,4,5,6,7,8)
valuex<-c(2,1,2,1,2,3,4,2)
valuey<-c(2,3,4,5,6)
now I plot the date and the valuex variable:
plot(date,valuex,type="l")
now, I want to add a line of the valuey variable, but it should start with the 4th day, so not at the beginning, therefore I add NA values:
valuexmod<-c(rep(NA,3),valuex)
and I add the line with:
lines(date,valuexmod,type="l",col="red")
But this does not work? R ignores the NA values and the valuexmod line starts with the first day, but it should start with th 4th day?
Given that date and valuex have the same length, I am assuming that you have a typo above.
Try this instead:
date <- c(1, 2, 3, 4, 5, 6, 7, 8)
valuex <- c(2, 1, 2, 1, 2, 3, 4, 2)
valuey <- c(2, 3, 4, 5, 6)
valueymod <- c(rep(NA, 3), valuey)
plot(date, valuex, type = "l", ylim = range(c(valuex, valuey)))
lines(date, valueymod, type = "l", col = "red")
Here's the resulting plot:
Related to your question is a point made in help("lines")...
The coordinates can contain NA values. If a point contains NA in either its x or y value, it is omitted from the plot, and lines are not drawn to or from such points. Thus missing values can be used to achieve breaks in lines.

Exporting multiple panels of plots and data to *.png (in the style layout() works within R)

I'm trying to export multiple panels of data and 1 plot as a single image using either the .png device or .pdf device and I'm not meeting with any success. I can produce the image that I want within R using the R native plotting device, but when I try to produce the same image directly, I get results that I do not anticipate.
Here is my example code
testMat <- matrix(1:20, ncol = 5)## create data
testMatDF <- as.data.frame(testMat)
names(testMatDF) <- c("Hey there", "Column 2",
"Some * Symbols", "And ^ More",
"Final Column")
rownames(testMatDF) <- paste("Group", 1:4)
library(gplots) ## gplots needed for textplot()
layout(matrix(c(1, 1, 2, 3, 3, 3), 2, 3, byrow = TRUE))
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
## produces what I want within R
layout(matrix(c(1, 1, 2, 3, 3, 3), 2, 3, byrow = TRUE))
png(file='plot1.png')
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
dev.off()
## only the last function texplot(testMatDF) gets output, not what I anticipated
I've also tried the mfrow() graphical parameter without success.
par(mfrow= c(3, 1))
png(file='plot2.png')
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
dev.off()
## only the last function texplot(testMatDF) gets output
If you move your calls to par or layout after you open your graphics device, it should work correctly.
png(file='plot2.png')
par(mfrow= c(3, 1))
curve(dnorm, -3, 4)
textplot(testMat)
textplot(testMatDF)
dev.off()

Resources