How to make one word italic in legend in base R - r

I have this piece of code, I cant not crack. I want to make one word italic in my legend and use this in a loop. The line of code is in bold.
legend('top', paste(italic(increase$Gene[i]), increase$Sex[i], increase$Drug[i]), bty='n', text.font=2, cex=4.0)
This would be extremely helpful. So far online has not been helpful.
See attached code:
pdf(file= "/Users/cahighfi/Desktop/MaleCocaineConsumptionPlots_UbiInc.pdf", width = 30, height = 30);
par(mfrow = c(4, 4), # 2x2 layout
oma = c(2, 2, 0, 0), # two rows of text at the outer left and bottom margin
mar = c(5, 5, 2, 1)+0.1, # space for one row of text at ticks and to separate plots
mgp = c(2, 1, 0), # axis label at 2 rows distance, tick labels at 1 row
xpd = NA)
x<-1:3
for (i in 1:nrow(increase)) {
plot(x=c(1, 3), increase[i,2:3], type="o", col= "red1", ylim=c(-40,40), cex.main=3.0, ylab ="", xlab="", axes=F, lwd=8)
axis(side=1, at=c(1,3), labels=c(1,3), cex.axis=3.0, font = 2)
axis(side=2, cex.axis=3.0, font = 2)
box()
lines(x=c(1, 3), increase[i,4:5], type="o", col= "blue1", lwd=8)
legend('top', paste(italic(increase$Gene[i]), increase$Sex[i], increase$Drug[i]), bty='n', text.font=2, cex=4.0)
legend('bottom', paste("P", "=", increase$p.value_chng_pref[i]), text.font=4, cex=4.0, bty='n')
mtext("Preference", side=2, line=3.25, cex=2.5, font = 2);
mtext("Exposure", side=1, line=2, cex=2.5, font = 2);
}
dev.off()
graphics.off()

Related

How to fix axis title overlapped with axis labels in barplot?

I have generated a bar plot/histogram for my data which shows the number of transactions for pack size. However, labels on x axis for the bars are out of the margin. The plot is presented below.
I have tried to fix this by setting the outer margin to par(oma=c(3,3,0,0)). Here is my new plot.
Although, the labels are inside the graph margin, but the x-axis title is still overlapped with the labels. How should I adjust the axis title so it is not overlapped with the labels?
Any suggestions would be very much appreciated!
Use axis and mtext.
par(mar=c(11, 6, 4, 2))
b <- barplot(data$v, ylim=c(0, 2e4), yaxt='n',
main='Number of Transactions by Life Stage Histogram'
)
mtext(data$l, 1, .5, at=b, las=2, cex=.7)
axis(2, labels=F)
mtext(axTicks(2), 2, .75, at=axTicks(2), las=2)
mtext('Life Stage', 1, 9)
mtext('Freequency', 2, 4)
Or maybe that might be better:
par(mar=c(3, 5, 4, 3))
b <- barplot(data$v, ylim=c(0, 15e3), ylab='Frequency', yaxt='n',
main='Number of Transactions by Life Stage'
)
axis(2, labels=F)
mtext(axTicks(2), 2, .75, at=axTicks(2), las=2, cex=.8)
y <- (data$v) * c(1, 1, 1, 0, 0, 0, 0) + 500
text(b, y, data$l, srt=90, adj=0, cex=.7)
mtext('Life Stage', 1, 1)
Data:
data <- structure(list(l = c("MIDAGE SINGLES/COUPLES", "NEW FAMILIES",
"OLDER FAMILIES", "OLDER SINGLES/COUPLES", "RETIREES", "YOUNG FAMILIES",
"YOUNG SINGLES/COUPLES"), v = c(7500, 2500, 1000, 15000, 15100,
10000, 15000)), class = "data.frame", row.names = c(NA, -7L))
Taking jay.sf example as one for any plot we could add:
title(xlab = "My Label", line = 10)
Where line is adaptable: 10 or 9 or 8 etc...
plot.new()
par(mar=c(11, 6, 4, 2))
b <- barplot(data$v, ylim=c(0, 2e4), yaxt='n',
main='Number of Transactions by Life Stage Histogram'
)
mtext(data$l, 1, .5, at=b, las=2, cex=.7)
axis(2, labels=F)
mtext(axTicks(2), 2, .75, at=axTicks(2), las=2)
mtext('Life Stage', 1, 9)
mtext('Freequency', 2, 4)
title(xlab = "My Label", line = 10)

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)

How to plot in a loop using par(new=T)?

I would like to plot two sets of values (from different datasets but for the same countries) with two x axes for several countries in two loops using par(new=T). R however overlays the second set of data starting from the last country.
Here you can see the result
https://www.dropbox.com/s/9x0dn7oom5czpwm/Rplot02.jpg?dl=0
Here is my code as an example for 2 countries only (I have 28).
par(mfrow=c(2,2))
par(mar = c(5,5,5,5), cex.axis=1)
for (i in colnames(DPreachdifft[,2:3])) {
plot(DPreachdifft$Diff, DPreachdifft[[i]],
type="p",col="red", xaxt="n", yaxt="n",
ylim = range(c(-25, 25)),
xlim = rev(range(DPreachdifft$Diff)),
ylab="delay(+) or advance(-) in years", xlab=NA, pch=16,
main = i, adj=0)
axis(side = 1, at=ticks,labels=ticks)
axis(side = 2)
abline(h=0)
}
par(new=T)
for (i in colnames(NPreachdifft[,2:3])) {
plot(NPreachdifft$Diff1, NPreachdifft[[i]],
type="p",col="blue", xaxt="n", yaxt="n",
ylim = range(c(-40, 40)),
xlab=NA, ylab=NA, pch=16)
axis(side = 3, at=ticks1)
mtext(side = 1, text="difference in DP", line = 2, adj = 0.5)
mtext(side = 3, text="difference in NP", line = 2, adj = 0.5)
}

R Graphs: Decreasing extra space between main title and graph

I am trying to remove space between the title of my histogram and where the y axis begins. I have edited top, bottom, and side margins, but the space between title and y axis has remained the same. Any suggestions? Here is my code...
par(mfrow=c(1,3), mar = c(4, 4, 4, 1) + 0.1, oma = c(1, 1, 3, 1))
hist(data$variable1, xlim = c(1,5),
main="Title here", breaks=seq(1,5,1),
freq=TRUE, xlab=" ", ylim = c(0,18),
border="white", col="gray", cex.main = 2)
hist(data$variable2, xlim = c(1,5),
main="Title here", breaks=seq(1,5,1),
freq=TRUE, xlab=" ", ylim = c(0,18),
border="white", col="gray", cex.main = 2)
hist(data$variable3, xlim = c(1,5),
main="Title here", breaks=seq(1,5,1),
freq=TRUE, xlab=" ", ylim = c(0,18),
border="white", col="gray", cex.main = 2)
Thank you!
One might wish that line.main would work as an argument to hist (analogous to cex.main and similar), but unfortunately that does not appear to be implemented. You can pass line=0 to hist, but it will affect x- and y-axis titles (and the subtitle, if there is one), not just the main title. To adjust only the main title, plot it separately:
hist(rnorm(50), main=NA)
title("A close-set title", line=0)

Plots side by side in R. Controling size accurately

I need to put 2 columns of plots side by side (only one row in the example) and I can't manage to control the size of the plots and the position of the labels so that both plots are exactly aligned. This is the code the have now:
split.screen(c(1,2))
screen(1)
par(oma=c(0,1,0,0), mai=c(0.6,0.36,0.5,0.7), cex=0.5, mgp = c(0.5,0.1, 0), tck = -0.05)
plot(datos$UN.CJF*1000, datos$Methane.Produced.CJF, pch = 16, cex = 0.5, col ="black",
xlab = "UN (g/d)", ylab = expression('CH'[4]*'(g/d)'))
title(main = "a)", cex=0.8, line=0.5, adj=0, cex.lab=1.2)
datos$LWchangeD.CJF <- datos$LWchange.CJF/15
screen(2)
par(oma=c(0,1,0,0), mai=c(0.6,0.36,0.5,0.7), cex=0.5, mgp = c(0.5,0.1, 0), tck = -0.05)
scatter2D(datos$UN.CJF*1000, datos$Methane.Produced.CJF,
pch = 16,
xlab = "UN (g/d)", ylab = "",
colvar = datos$LWchangeD.CJF, clab = c("Liveweight change (kg/d)")
)
title(main = "b)", cex=0.8, line=-0.7, adj=0, cex.lab=1.2)
Notice how the size of the 2 figs is different
There must be a better way to do it.
Any help would be MUCH appreciated
Alvaro
grid.arrange did the trick, but I first re-did all my plots in ggplot2.
Thanks a lot for your suggestions
Alvaro

Resources