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

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)

Related

Moving Bar charts in R away from each other

par(mfrow=c(1,2)) #Plot the two bar charts together
par(mar = c(4, 7, 1, 1)) # Adjust the margins on the four sides of the plot
barplot(height = table(s50_1995$Smoking_status),
main = "Smoking status ", xlab = "", ylab = "", font.lab = 2, col.lab = "Red", cex.lab = 1.5, xlim=c(0,40) ,col = 1:3, horiz = TRUE, las = 2)
# Plot the bar chart indicating the title,the color,
#size and font of the label, the correct x limits,the colors
#for the bars and set the y-axis labels perpendicular to the y axis
mtext("Smoking Status", side=2, line=6) #Set the label for the y axis
mtext("Number of Pupils", side=1, line=2) #Set the label for the x axis
par(mar = c(4, 7, 1, 3)) # Adjust the margins on the four sides of the plot
barplot(height = table(s50_1995$Sport_participation),
main = "Sport participation ",
xlab = "", ylab = "",
font.lab = 2, col.lab = "Red",
cex.lab = 1.5,
xlim=c(0,40) ,
col = 1:3, horiz = TRUE, las = 2)
mtext("Sport participation Status ", side=2, line=6)
mtext("Number of Pupils", side=1, line=2)
par(mfrow=c(1,2))
How do I put these bar charts further away from each other? They are way too close to each other right now
Using some dummy data. it's a balancing act between the allowance for the left margin for the second plot and the location of the axis title for the second plot.
In this case I've increased the size of the left margin in the call to par(mar...) for the second plot to 11 and reduced the mtext position to line 4.
Alternatively you could increase the size of the right margin for the left hand plot.
fruit1 <- c(apple = 40, kiwi = 15, grape = 30)
fruit2 <- c(apple = 20, kiwi = 5, grape = 40)
par(mfrow=c(1,2))
#optionally you could increase the value of the end value to increase the size of the right margin.
par(mar = c(4, 7, 1, 1))
barplot(fruit1,
col="blue",
horiz=TRUE,
las=1)
mtext("Fruit 1", side=2, line=6)
#this is the key line, adjust the second value to increase the gap between the charts
par(mar = c(4, 11, 1, 1))
barplot(fruit2,
col="green",
horiz=TRUE,
las=1)
mtext("Fruit 2", side=2, line=4)
Created on 2021-11-26 by the reprex package (v2.0.1)

does anyone know why the plot is left with a box like that?

When I changed the scale, the box on the left got messed up.
boxplot((dados$Turbidez..ppm.~dados$Período*dados$Estado), xaxt= "n",yaxt="n",type ="b", log="y", ylim=c(0.1,1000),
ylab = "Turbidez [ppm]",range=0,col=c("yellow","orange","red","yellow","orange","red"),
xlab = "Regiões")
axis(2, at=10^(0:6), labels=formatC(10^(0:6),format="f", digits=0),cex.axis=0.8,las=2 )
axis(1, at = seq(2, 5, by = 3), labels=c("PB","RN"))
Look at the boxplot below:
See if this works for you
boxplot(Turbidez..ppm. ~ Período*Estado,
data = dados[dados$Turbidez..ppm. > 0,],
xaxt= "n",yaxt="n",type ="b", log="y", ylim=c(0.1,1000),
ylab = "Turbidez [ppm]",range=0,
col=c("yellow","orange","red","yellow","orange","red"),
xlab = "Regiões")
axis(2, at=10^(0:6), labels=formatC(10^(0:6),format="f",
digits=0),cex.axis=0.8,las=2 )
axis(1, at = seq(2, 5, by = 3), labels=c("PB","RN"))

Assigning labels only to some datapoints in a plot

I am using the following couple of lines to produce the below plot from rows of two 4X10 Matrix d1, and d2 in one graph:
plot(as.matrix(d1[2,]), as.matrix(d2[2,]), type="o", col="firebrick", ann=FALSE, pch=17, log = 'y',lty=4, xaxt = "n", yaxt="n", las=2, ylim = c(50, 2e6))+
lines(as.matrix(d1[1,]),as.matrix(d2[1,]), type="o", col="deepskyblue", ann=FALSE, pch=15, lty=4)+
lines(as.matrix(d1[3,]),as.matrix(d2[3,]), type="o", col="darkorange", ann=FALSE, pch=18, lty=4)+
lines(as.matrix(d1[4,]),as.matrix(d2[4,]), type="o", col="lawngreen", ann=FALSE, pch=16, lty=4)
x_axis_range <- c(0, 1, 2, 4, 6, 8, 10, 12, 14)
axis(1,at = x_axis_range, labels = x_axis_range)
axis(1,at = x_axis_range, labels = x_axis_range)
y_axis_range <- c(1e+2, 4e+2, 1e+3, 5e+3, 3e+4, 2e+5, 5e+5, 2e+6)
y_axis_labels <- c("100", "400", "1K", "5K", "30K", "200K", "500", "2M")
axis(2,at = y_axis_range, labels = y_axis_labels, las=2)
And it produces the following graph:
Is there a way to label the first and the last datapoints on each line with an arbitrary label (i.e. "F", and "L")?
Here is an example that should give you the idea.
DF <- data.frame(x = 1:10, y = (1:10)^2)
plot(DF$x, DF$y, type = "b")
last <- nrow(DF)
text(DF$x[c(1, last)], DF$y[c(1, last)], labels = c("F", "L"), pos = c(3, 4))
For future reference, this answers it:
text(as.matrix(d1[3,2]),as.matrix(d2[3,2]) , labels=c("F"), pos = c(4), cex=0.8)
text(as.matrix(d1[3,10]),as.matrix(d2[3,10]) , labels=c("L"))), pos = c(4), cex = 0.8)

How to plot (almost) the same function at both sides of the "y" axis in R?

I have a function that depends on distance and behaves different according to the direction (east or west) to where you evaluate it. Now I have two plots side by side, but I need to have them as one, where the labels of the axis are shared and the x axis label is centered. Here is an example of how the code looks right now:
x = (0:300)
par(mfrow=c(2,2))
Idriss70 = function(x){
exp(5.6315-0.4104*7-(2.9832-0.2339*7)*log(x+10)+0.00047*x+0.12)
}
plot(Idriss70(x), log = "x", type="l",xlim = c(300,1), xlab="Distancia [km]",ylab="PGA [g]", main="Aroma y Humayani extendida, Mw 7,0", col="green", panel.first = grid(equilogs = TRUE))
Idriss70 = function(x){
ifelse (x >= 0 & x<=3, exp(5.6315-0.4104*7-(2.9832-0.2339*7)*log(0+10)+0.00047*0+0.12),
exp(5.6315-0.4104*7-(2.9832-0.2339*7)*log(x+10)+0.00047*x+0.12))
}
plot(Idriss70(x), log = "x", type="l", xlab="Distancia [km]",ylab="PGA [g]", main="Aroma y Humayani extendida, Mw 7,0", col="green", panel.first = grid(equilogs = TRUE))
As you can see, the log scale of the plots don't allow "negative" values to be evaluated so I haven't been able to use only one plot.
How can I get this plot as one without using Illustrator or another graphics software, as I have to create lots of this ones for differente areas?
I haven't used ggplot in the past but I am willing to learn if necessary.
You can basically make one plot and mess around with fig so that you restrict the first plot to the left half of the device and the second to the right half
x <- 0:300
Idriss70 = function(x){
exp(5.6315-0.4104*7-(2.9832-0.2339*7)*log(x+10)+0.00047*x+0.12)
}
Idriss71 = function(x){
ifelse(x >= 0 & x<=3,
exp(5.6315-0.4104*7-(2.9832-0.2339*7)*log(0+10)+0.00047*0+0.12),
exp(5.6315-0.4104*7-(2.9832-0.2339*7)*log(x+10)+0.00047*x+0.12))
}
par(fig = c(0, .5, 0, 1), mar = c(5, 4, 4, 0), xaxs = 'i', ann = FALSE)
plot(Idriss70(x), log = "x", type="l", xlim = c(300,1),
col="green", axes = FALSE, panel.first = grid(equilogs = TRUE))
xx <- axis(1)
axis(2)
par(fig = c(.5, 1, 0, 1), new = TRUE, mar = c(5, 0, 4, 2))
plot(Idriss71(x), log = "x", type="l", col="green",
panel.first = grid(equilogs = TRUE), axes = FALSE)
axis(1, at = xx, labels = c('', xx[-1]))
title(xlab = "Distancia [km]", main = 'Aroma y Humayani extendida, Mw 7,0',
ylab = 'PGA [g]', outer = TRUE, line = -1.5)
good luck with ggplot. you'd probably have to summon #baptiste
well you could still use mfrow I suppose
graphics.off()
par(mfrow = c(1, 2), mar = c(5, 4, 4, 0), xaxs = 'i', ann = FALSE)
plot(Idriss70(x), log = "x", type="l", xlim = c(300,1),
col="green", axes = FALSE, panel.first = grid(equilogs = TRUE))
xx <- axis(1)
axis(2)
par(mar = c(5, 0, 4, 2))
plot(Idriss71(x), log = "x", type="l", col="green",
panel.first = grid(equilogs = TRUE), axes = FALSE)
axis(1, at = xx, labels = c('', xx[-1]))
title(xlab = "Distancia [km]", main = 'Aroma y Humayani extendida, Mw 7,0',
ylab = 'PGA [g]', outer = TRUE, line = -1.5)

Why the y axis in my barplot doesn't show completely?

I don't know how to change the code so that the y-axis in the barPlot shows completely? I expect it to show up to 10 as I have a 9.2 in my data points but it shows only up to 8. Any idea what's the hack to this?
Here's the code:
And here's what it shows:
just set the ylim = c(0, 10) like the way you changed the xlim
Whatever the axis that the groups go on isn't drawn by default, so a vertical bar plot won't have x-axis; horizontal won't have y-axis. You can add that of course. Use the return value of barplot:
par(mfrow = c(2, 1))
bp <- barplot(c(8, 5), width = .5, main = 'Feature Exploration', xlim = c(0,4), ylim = c(0, 10),
ylab = 'Errors (%)', xlab = 'ML Models', col = c('gray27','orangered4'))
## this will draw the x-axis but at points 1, 2, 3, ... which is not
## where the centers of your bars are plotted; you get that info in bp
axis(1)
bp <- barplot(c(8, 5), width = .5, main = 'Feature Exploration', xlim = c(0,4), ylim = c(0, 10),
ylab = 'Errors (%)', xlab = 'ML Models', col = c('gray27','orangered4'))
## so try again with a fancy axis, bp is a matrix containing the centers
## of the plotted bars
axis(1, at = bp, labels = c('Model1','Model2'), lwd = 0, lwd.ticks = 1, tcl = -.5)

Resources