Label on X-axis for each group of points - r

I'm am creating a script which allows me to get data from a database, and visualises it in a graph.
As you can see, there are 8 groups of data, indicated on the X-axis. Each group always contains 90 values.
Right now, the place of the labels is hard-coded like this:
axis(1, at = c(31.25, 93.75, 156.25, 218.75, 281.25, 343.75, 406.25, 468.75),
labels = c("ss oligo 1", "ss oligo 2", "ss oligo 3", "ss oligo 4",
"ss oligo 4", "ss oligo 5", "ss oligo 6", "ss oligo 7"))
It works perfectly fine, but I was wondering if there is a way to do this more dynamically, by just telling R to assign a label to each set of 90 values.
Example:
# Generate data ###################################################################################
x <- vector()
y <- vector()
# y[length(y)+1] <- sample(10:12, 1, replace = TRUE)
oligo_1 <- runif(62, 10.5, 11.5)
oligo_2 <- runif(62, 14, 15)
oligo_3 <- runif(62, 17, 18)
oligo_4a <- runif(64, 20.5, 22)
oligo_4b <- runif(64, 20.5, 22)
oligo_5 <- runif(62, 24, 25)
oligo_6 <- runif(62, 27, 28)
oligo_7 <- runif(62, 30, 31)
y <- c(oligo_1, oligo_2, oligo_3, oligo_4a, oligo_4b, oligo_5, oligo_6, oligo_7)
x <- c(1:500)
example <- data.frame(x, y)
# Define variables ################################################################################
xmin <- 10
xmax <- 36
# Generate graph ###################################################################################
png(filename = "graph.png", width = 1500, height = 833)
plot(x = example[,2], type="l",
xlim = c(0, nrow(example)), ylim = c(xmin, xmax),
xaxt="n", yaxt="n",
xlab = "", ylab = "")
rect(xleft = par("usr")[1], ybottom = 9.8, xright = par("usr")[2], ytop = 12.2, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 13.2, xright = par("usr")[2], ytop = 15.5, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 16.5, xright = par("usr")[2], ytop = 18.9, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 19.9, xright = par("usr")[2], ytop = 22.3, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 23.3, xright = par("usr")[2], ytop = 25.5, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 26.5, xright = par("usr")[2], ytop = 28.7, border = "lightgrey", col = "lightgrey")
rect(xleft = par("usr")[1], ybottom = 29.7, xright = par("usr")[2], ytop = 32.1, border = "lightgrey", col = "lightgrey")
axis(1, at = c(31.25, 93.75, 156.25, 218.75, 281.25, 343.75, 406.25, 468.75),
labels = c("ss oligo 1", "ss oligo 2", "ss oligo 3", "ss oligo 4",
"ss oligo 4", "ss oligo 5", "ss oligo 6", "ss oligo 7"))
axis(2, at = c(11, 14.35, 17.7, 21.1, 24.4, 27.6, 30.9), las = 1)
lines(x = example[,2])
box()
mtext(paste("QC-check for", "TEST"), side = "3", line = 1, cex = 2, font = 2)
mtext("Samples" , side = "1", line = 3, cex = 1, font = 1)
legend(x = par("usr")[1]+10, y = par("usr")[4]-1, legend = c("Cq", "Ccq"), cex=1.5, lwd = 2, col = c("black","red"))
dev.off()

Why not:
axis(1, at=31.25+(0:7)*62.5, labels=paste("ss oligo",1:8) )

Alright, I made it much more difficult than it was. So in combo with the answer from #user449060, I changed the code to:
count <- nrow(data)/8
axis(1, at = count/2+(0:7)*count, labels = paste("ss oligo",c(1:4, 4, 5:7)))
which makes it a lot more dynamic!

Related

change second y axis color in base R

Change secondary line axis color changes send color for ggplot, but I chose to go with base R, and would like to be able to select the second y axis color.
I have the following data:
df = structure(list(A = c("Q4-17", "Q1-18", "Q2-18", "Q3-18", "Q4-18",
"Q1-19", "Q2-19", "Q3-19", "Q4-19", "Q1-20", "Q2-20", "Q3-20",
"Q4-20", "Q1-21", "Q2-21", "Q3-21", "Q4-21", "Q1-22", "Q2-22",
"Q3-22"), B = c(69.45, 71.1, 74.94, 73.87, 93.61, 91.83,
95.38, 109.8, 133.75, 125.26, 118.22, 145.65, 144.9757185, 155.3464032,
184.367033, 179.8121721, 187.235487, 189.1684376, 184.3864519,
161.5300056), C = c(70.73, 71.73, 74.33, 73.27,
95.94, 94.38, 95.38, 109.8, 115.32, 116.92, 115.9, 113.87, 106.108147,
96.84273563, 111.5150869, 110.1228567, 110.7448835, 194.9684376,
187.7241152, 167.7665553), D = c(260.3, 216.02, 203.72,
203.52, 300.96, 320.77, 330.5, 413.52, 436.7, 474.96, 463.6,
501.87, 493.8865461, 497.1760767, 514.9903459, 503.7601267, 510.8362938,
614.9915546, 603.5761107, 593.660831), E = c(NA,
NA, NA, NA, NA, NA, NA, NA, 39.237, 35.621, 32.964, NA, 152.137,
140.743023, 167.809, 170.877, 117.517, 102.691723, 88.8, 76.2445528
)), class = "data.frame", row.names = c(NA, -20L))
df = df %>%
rowwise() %>%
mutate(sums = sum(D,E, na.rm = TRUE))
df = df[8:nrow(df),]
and this to generate my plot
x <- seq(1,nrow(df),1)
y1 <- df$B
y2 <- df$D
par(mar = c(5, 4, 4, 4) + 0.3)
plot(x, y1, col = "#000000",
type = "l",
main = "title",
ylim = c(0, max(df[,2:3])),
ylab = "Y1",
xlab = "",
xaxt = "n")
axis(1,
at = seq(from = 13, by = -4, length.out = 4),
labels = df$A[seq(from = 13, by = -4, length.out = 4)])
lines(x, df$C, lty = "dashed", col = "#adadad", lwd = 2)
par(new = TRUE)
plot(x, df$sums, col = "#ffa500",
axes = FALSE, xlab = "", ylab = "", type = "l")
axis(side = 4, at = pretty(range(y2)),
ylim = c(0,max(df[,3:5], na.rm = TRUE)),
col = "#00aa00") # Add colour selection of 2nd axis
par(new = TRUE)
plot(x, df$D , col = "#0000ff",
axes = FALSE, xlab = "", ylab = "", type = "l", lwd = 1)
mtext("y2", side = 4, line = 3)
but this does not colour my complete second y axis, nor labels, nor title
does any one have any suggestions to be able to set entire y2 axis to be #00AA00 - ticks, labels, and title?

Difficulty getting a legend to add to my graph

I have tried many ways to add a legend to the bottom two graphs but for some reason it either gives an error or doesn't show the legend.
Here is my code:
lg <- function(x, a = 1, b = 1){
exp(a+b*x) / (1+exp(a+b*x))
}
plot(NA, xlim=c(-5,5), ylim=c(0,1), xlab = "x", ylab = "y", legend(2, 0.4, legend=c("b = 1", "b = 2", "b = 3", "b = 4", "b = 5"))
for (b in c(1:5)){
curve(expr = lg(x, 1, b), from = -5, to = 5, n = 100, add= TRUE, col = b)
}
plot(NA, xlim=c(-5,5), ylim=c(0,1), xlab = "x", ylab = "y", legend(2, 0.4, legend=c("a = 1", "a = 2", "a = 3", "a = 4", "a = 5"))
for (a in c(1:5)){
curve(expr = lg(x, a, 1), from = -5, to = 5, n = 100, add= TRUE, col = a)
}
Is there something wrong with my placement of the legend within the code?
Put legend function in new row without comma before.
And close parenthesis of plot:
plot(NA, xlim=c(-5,5), ylim=c(0,1), xlab = "x", ylab = "y")
legend(2, 0.4, legend=c("b = 1", "b = 2", "b = 3", "b = 4", "b = 5"))
for (b in c(1:5)){
curve(expr = lg(x, 1, b), from = -5, to = 5, n = 100, add= TRUE, col = b)
}
same for plot2
plot(NA, xlim=c(-5,5), ylim=c(0,1), xlab = "x", ylab = "y")
legend(2, 0.4, legend=c("a = 1", "a = 2", "a = 3", "a = 4", "a = 5"))
for (a in c(1:5)){
curve(expr = lg(x, a, 1), from = -5, to = 5, n = 100, add= TRUE, col = a)
}

Move hazard ratio values (and confidence intervals) left in forest() in metafor package

I am just trying to move the hazard ratio/outcome column with its corresponding CI values to the left so that I can add a p-value to the right of the outcomes column, Is this possible?
Is there an easy way to find this argument and others like it in the R documentation for forest()?
#Forest Plot
label1 <- as.character(c("<50", "50 to <60", "60"))
label1 <- factor(label1, levels = unique(label1))
hazards1 <- c(1.42, 1.66, 2.85)
lower1 <- c(1.34, 1.50, 2.59)
upper1 <- c(1.51, 1.85, 3.13)
patient_num1 <- c(240000, 180000, 220000)
event_num1 <- c(2600, 1300, 2900)
forestplot1 <- data.frame(label1, hazards1, lower1, upper1, event_num1, patient_num1)
forestplot1$patient_num1 <- rev(forestplot1$patient_num1)
par(mar=c(4,4,1,2))
pdf(file = "figureex.pdf", width = 15, height = 8.5)
pdf.options(encoding='ISOLatin2.enc')
forest(rev(hazards1), ci.lb = rev(lower1), ci.ub = rev(upper1), slab = rev(label1), xlim = c(-4,0),
xlab = "Adjusted Hazard Ratio for Event",
refline = 1, annotate = T, ylim = c(-1, 29), ilab = cbind(forestplot1$patient_num1, rev(event_num1)),
ilab.xpos=c(-1,-0.25), at = c(0.25, 0.5, 1, 2, 4, 6), rows = c(1:3),
cex = 1, yaxs = "i")
text(-4, 27.29, pos = 4, "Variable")
text(-1.25, 27.67, pos = 4, "No. of \nPatients")
text(-0.45, 27.67, pos = 4, "No. of \nEvents")
text(5.05, 27.69, pos = 4, "Hazard Ratio \n(95% CI)")
dev.off()

r hyphen in label of x axis

The x axis label has hyphen. This is not what I wrote in my code:
temp = table(res$Methode, res$Bewertung)
tempf = ftable(res$Methode, res$Bewertung)
barplot(tempf , xlab="Bewertung", ylab="absolute Häufigkeit", cex.lab = 1.4, beside=TRUE, ylim = c(0,600), xlim=c(11.75,1.7), col = colour2)
legend("top", legend=c("ohne LOD", "mit LOD"), horiz = TRUE, inset = c(0, 0), fill = rev(colour2), cex=1.4)
axis(1, at=c(11, 8, 5, 2),labels=c("sehr relevant", "mit­tel­mä­ßig relevant", "wenig relevant", "nicht relevant"), cex.axis=1.4)
How can I solve this?

gain plot margins in R

I'm trying to fit a second ylab on the right of my plot but I'm not able to gain the extra space needed. Drawing the scale values inside the graph could be a solution if this was not possible.
My code (simulated data but the problem with the right margin is there):
dev.new( width= 16, height= 7 )
par( cex= 0.9 )
plot( 1:100, type="n", xlab = "", ylab = "", axes = F, las = 2, cex = 0.7 )
axis( 4, at = c ( 0, 30, 60, 90, 120, 150, 180 ), labels = c( "0", "30", "60", "90", "120", "150", "180" ), las = 0, cex.axis = .8,col.lab = "gray80",
tck = 0.01 )
rect( 153, -5.5, 169, 185, col = "gray80", border = NA)
rect( 246, -5.5, 272, 185, col = "gray80", border = NA)
lines( 1:200, col = "gray20" )
#text("Moon phase (º)", 330, 90, cex = .9 )
mtext(side = 4, text = "Moon phase (º)", line = 2, cex = .9 )
mtext(side = 1, text = "Dates", line = 4, cex = .9 )
par( new = T )
plot( 1:220, type = "n", lwd= 1.2, xlab= "", ylab= "Photoperiod (h)", axes = F, cex = .8, col.lab = "red" )
axis( side= 1, at = c ( 1, 20, 51, 82, 112, 143, 173, 204, 235, 264, 295, 324 ), labels = c ( "12 Jun'07", "1 Jul'07", "1 Aug'07", "1 Sep'07", "1 Oct'07", "1 Nov'07", "1 Dec'07", "1 Jan'08", "1 Feb'08", "1 Mar'08", "1 Apr'08", "30 Apr'08" ), las = 2, cex.axis = .75 )
axis ( side = 2, at = c( 12, 13, 14, 15, 16 ), labels = c( "12", "13", "14", "15", "16" ), cex.axis = .8, las = 2)
box()
Thank you for your help,
Santi
See argument mar of par. This set the margins of your plot area (bottom, left, top, right; see ?par for details).
dev.new( width= 16, height= 7 )
## set outer margins
par( mar=c(5, 4, 4, 4), cex= 0.9 )
plot( 1:100, type="n", xlab = "", ylab = "", axes = F, las = 2, cex = 0.7 )
axis( 4, at = c ( 0, 30, 60, 90, 120, 150, 180 ), labels = c( "0", "30", "60", "90", "120", "150", "180" ), las = 0, cex.axis = .8,col.lab = "gray80",
tck = 0.01 )
rect( 153, -5.5, 169, 185, col = "gray80", border = NA)
rect( 246, -5.5, 272, 185, col = "gray80", border = NA)
lines( 1:200, col = "gray20" )
#text("Moon phase (º)", 330, 90, cex = .9 )
mtext(side = 4, text = "Moon phase (º)", line = 2, cex = .9 )
mtext(side = 1, text = "Dates", line = 4, cex = .9 )
par( new = T )
plot( 1:220, type = "n", lwd= 1.2, xlab= "", ylab= "Photoperiod (h)", axes = F, cex = .8, col.lab = "red" )
axis( side= 1, at = c ( 1, 20, 51, 82, 112, 143, 173, 204, 235, 264, 295, 324 ), labels = c ( "12 Jun'07", "1 Jul'07", "1 Aug'07", "1 Sep'07", "1 Oct'07", "1 Nov'07", "1 Dec'07", "1 Jan'08", "1 Feb'08", "1 Mar'08", "1 Apr'08", "30 Apr'08" ), las = 2, cex.axis = .75 )
axis ( side = 2, at = c( 12, 13, 14, 15, 16 ), labels = c( "12", "13", "14", "15", "16" ), cex.axis = .8, las = 2)
box()
In your code use on line #2:
par( cex= 0.9,mar=c(6,4,6,4) )

Resources