Abline clip for secondary axis - r

With the ablineclip I can draw a straight line to any plot like:
ablineclip(h = 2, x1 = 0,x2 = 5,lty = 2, col = "green")
this draws a horizontal line where y=2 from x=0 to x=5.
How can I draw a line for a second y axis??
(meaning a horizontal line where y value on the right axis is 2).
I have no code yet, sorry for the non reproductive code.

Something like the following will work:
library(plotrix)
plot(1, type="n", xlim=c(-10,10), ylim=c(0,4))
ablineclip(h = 2, x1 = 0,x2 = 5,lty = 2, col = "green")
par(new=TRUE)
plot(1, type="n", xlim=c(-10,10), ylim=c(10,15), xaxt="n",yaxt="n",xlab="",ylab="")
axis(4)
ablineclip(h = 12, x1 = -4,x2 = 4,lty = 2, col = "red")

Related

r plot xlab for side 3 and side 4

In R is there a way to have titles for side 3 (top) = x2 and side 4 (right) = y2 like highlighted below?
Below is R code.
x<-1:4; y=x*x
plot(x, y, pch=18, col="red", type="b",
frame=FALSE, xaxt="n") # Remove x axis
axis(1, 1:4, LETTERS[1:4], col.axis="blue")
axis(3, col = "darkgreen", lty = 2, lwd = 0.5)
axis(4, col = "violet", col.axis = "dark violet", lwd = 2)
You can add labels to additional axes using mtext(). For the right y axis you will probably need to add extra space using par(mar = ...)
par(mar = c(5, 4, 4, 5)) # create extra space for label
plot(1:5)
axis(3)
axis(4)
mtext("top xaxis", side = 3, line = 2)
mtext("right y axis needs extra space", side = 4, line = 2)
Created on 2022-11-08 with reprex v2.0.2

Label of the secondary y axis lines on the secondary y axis values

I am trying to plot a dual y axis plot using the following code:
plot(dataset1, col="black", ylab="Height (ft)", main = "W7R001")
par(new = TRUE)
points(dataset5, col="red", pch = ".", xlab="", ylab="", axes = FALSE)
axis(side = 4)
mtext(side = 4, "Height (m)")
My issue now is that, on the secondary plot, the label of the secondary y axis lies on the secondary y axis values. How do I fix or avoid this?
You can shift the label on the second y-axis with the line parameter. You also need to increase the right margin of the plot.
par(mar=c(5, 4, 4, 4) + 0.1) #increase plot margins to the right
plot(x = 1:5, y = 1:5, xlab = "", ylab="LABEL")
axis(side = 4)
mtext(side = 4, "LABEL", line = 3) #shift position of the label

R -Smoothscatter plot curve and diagonal axis

I have file from that wanted plot the smoothscatterplot using R. plot must have the dots, diagonal axis and a curve for that I have formula, I am creating smoothscatterplot but not able plot diagonal and curve any suggestion and help will be appreciated
https://drive.google.com/file/d/1KknqYcRBCGm8Xrj1XKh3mE7rb7LK9iny/view?usp=sharing
what I tried
diagonal axis
df$P0+df$P2 =1
curve
p2 = (√df$P0 − 1)^2
df=read.table("scale_out",sep='\t', header=TRUE)
df = data.frame(df)
smoothScatter(df$P0,df$P2, cex=10)
what I got
what I want
Thank you
The data doesn't seem to contain what you think it contains.
For example, if we just do a straight plot of P0 and P2, we get this:
plot(df$P0, df$P2, pch = 18, cex = 0.5)
The desired plot that you show in your question suggests that this should be a scatter plot of noisy data, but it isn't. If we plot all the numeric variables in your data frame against each other, we get this:
The only plot here that looks like a scatter plot of the correct shape is P0 versus B.prop.
Assuming that this is what you want, you can create the desired plot like this:
smoothScatter(df$P0, df$B.prop, cex = 2, xlab = "P0", ylab = "P2")
curve((sqrt(x) - 1)^2, 0, 1, lty = 2, lwd = 5, col = "red", add = TRUE)
lines(0:1, 1:0, lty = 2, lwd = 5, col = "deepskyblue4")
legend(0.35, 1, c("Coordination", "Independence"),
col = c("deepskyblue4", "red"), bg = "#FFFFFFAA",
lty = 2, lwd = 5, box.col = "#FFFFFF00")

R is plotting y axis upside down. How to flip rightside up?

I've inherited this R code that plots a simple line graph. However, it does it so that the y axis values are plotted downwards below 0 (plots it in the 4th quadrant with 0 at the top and +3600 at the bottom). I want to plot the data right-side up (1st quadrant) so the y axis data goes from 0 up to +3600 at the top like a typical grade-school plot.
I've tried ylim = rev(y) but it returns an error...
I've also tried flipping the seq() command but no luck there.
list.vlevel = numeric(9) # placeholder
plot(
rep(0, length(list.vlevel)),
seq(1, length(list.vlevel)),
type = "n",
xlim = biaslim,
axes = F,
main = paste(list.var.bias[vv], list.score.bias[vv]),
xlab = "",
ylab = ""
)
abline(h = seq(1, length(list.vlevel)),
lty = 3,
col = 8)
axis(2,
labels = list.vlevel,
at = seq(length(list.vlevel), 1, -1),
las = 1)
axis(1)
box()
legend(
x = min(biasarray.var.runhour),
y = length(list.vlevel),
legend = expname,
lty = 3,
lwd = 3,
col = expcol
)
for (exp in seq(length(expname), 1, -1)) {
lines(
biasarray.var.runhour[exp, ],
seq(length(list.vlevel), 1, -1),
col = expcol[exp],
lwd = 3,
lty = 3
)
}
abline(v = 0, lty = 3)
The plot should end up in the first quadrant with yaxis values increasing from 0 upwards to +###.
The axis(2, ...) line draws the y axis. You can see that is the labels follow a descending sequence: seq(length(list.vlevel), 1, -1). seq(1, length(list.vlevel))
Similarly, inside lines(), probably you need to make the same change from seq(length(list.vlevel), 1, -1) to ``seq(1, length(list.vlevel))`
That's as much as we can tell with the info you've provided - can't run any of yoru code without values for all the constants you use, e.g., biasarray.var.runhour, list.var.bias, vv, etc.

Scale secondary y-axis in combination graph of barplot and line

I made a graph in which a barplot and a line plot are combined. The problem is that the scale of my secondary y-axis isn't how it should be.
This is the code I used:
barplot <- barplot(covpatient[[1]]$cov, names.arg = covpatient[[1]]$exon, xlab = covpatient[[1]]$gene[1] , ylab = "read depth" , border = gray.colors(length(unique(covpatient[[1]]$exon)))[as.factor(covpatient[[1]]$exon)])
par(new = TRUE)
lines(x = barplot, y = covpatient[[1]]$amplicon, bty = "n")
axis(side = 4, at = pretty(range(covpatient[[1]]$amplicon)))
And this is how my plot looks like:
The values of the lines plot are OK, but you see that the y-axis is not fully expanded. I want it to look the same as the y-axis on the left
Can someone help me with this?
Without a reproducible example and a clear question all answers will at best be gueswork but have a look at the following:
x <- 1:10
y <- c(1, 3, 5, 6, 2, 7, 11, 3, 2, 13)
z <- runif(10, min=1000, max=10000)
par(mar = c(5, 4, 4, 4) + 0.3)
barplot(y, col=rainbow(length(x)))
par(new = TRUE)
plot(x, z, type = "l", axes = FALSE, bty = "n", xlab = "", ylab = "")
axis(side=4, at = pretty(range(z)))
mtext("z", side=4, line=3)
library(plotrix)
twoord.plot(x,y,x,z,
xlab="x",
ylab="y",
rylab="z",
main="Main",
type=c("bar","l"),lcol=rainbow(length(x)),rcol=4)

Resources