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)
Related
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
I want to create a simple one-dimensional plot in R ranging from 0 to ten, with a scale (small lines for integer values 1,2, etc.), a slightly higher line for 5 (the median) and slightly higher than all of the other for 0 and 10. Then I want to fill this plot with a few points representing values like 2, 4, 5, 6, 8, and a text above each one of them with corresponding labels (like "party voted", "closest party", "individual 1", "expert", "individual 2"). It can be smaller labels, like "PV", "CP", etc.
I would like to have control over shape and color (say in data-points 4 and 6 I have a circle filled in black but in position 2 I have a not filled square, in position 5 I have a green circle filled-in, and in position 8 I have a black triangle, also filled). I would like to have 0, 10 and 5 marked in the labels as well.
A very rough representation of what I am trying to draw is in the image below (it has all the elements I want, at least).
In this Stack Overflow question there is some code on one-dimension plot, I have tried to adapt it to what I need but didn't get to it
I've assumed from the link in the question that you are looking for a base R solution.
There may be more efficient solutions but this seems to get you where you want.
I've avoided the need for arrows by forcing the labels to run over two lines and reducing the text size on the plot so they do not overlap.
You could manage this with arrows if need be, but this seems it will need a lot of extra code.
# data
df <- data.frame(desc = c("Party voted", "Closest party", "Individual 1", "Expert", "Individual 2"),
score = c(2, 4, 5, 6, 8),
y = 1)
# add line break to labels
df$desc <- gsub("\\s", "\n", df$desc)
plot(df$score,
df$y,
# type = "o",
xlim = c(0, 10),
pch = c(1, 21,21,21, 24),
col = c("black", "black", "green", "black", "black"),
bg = c("black", "black", "green", "black", "black"),
cex = 1.5,
xaxt = "n", #remove tick marks
yaxt = "n",
ylab = '', # remove axis labels
xlab = '',
bty = "n") # remove bounding box
axis(side = 1,
0:10,
pos = df$y,
labels = FALSE,
tck = 0.02)
axis(side = 1,
0:10,
pos = df$y,
labels = c(0, rep("", 4), 5, rep("", 4), 10),
tck = -0.02)
axis(side = 1,
c(0, 5, 10),
pos = df$y,
labels = FALSE,
tck = 0.05)
axis(side = 1,
c(0, 5, 10),
pos = df$y,
labels = FALSE,
tck = -0.05)
text(x = df$score,
y = df$y,
labels = df$desc,
pos = 3,
offset = 1,
cex = 0.75)
Created on 2021-04-28 by the reprex package (v2.0.0)
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()
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)
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)