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)
Related
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)
Need to move rotated labels on the top of the bar using barplot. What parameter am I missing here?
code:
df<- data.frame(a=strrep(letters[1:20], 10) , b=runif(20, min=1, max=30))
df<- df[order(df$b, decreasing = TRUE),]
row.names(df)<- df$a
par(mar=c(15, 5, 3, 2)+ 0.2)
x<- barplot(df$b, c(2, 4, 1, 6), ylim = c(0, 30), ylab="statistics", col = heat.colors(20), xaxt="n")
label = row.names(df)
text(cex=0.2, x=x, y=-1.25, label, xpd=TRUE, srt=45)
You need to change the position of y-axis in text with a suitable offset. (I have used + 1 here).
x <- barplot(df$b, c(2, 4, 1, 6), ylim = c(0, 30), ylab="statistics",
col = heat.colors(20), xaxt="n")
label = row.names(df)
text(cex=0.2, x=x, y=df$b + 1, label, xpd=TRUE, srt=45)
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()
So i have this numeric variables which reflect percentages
data1.pct<-19
data2.pct<-5
data3.pct<-76
class1.pct<-35
class2.pct<-18
class3.pct<-47
Now i am using this code to generate barplot
CairoPDF(paste('data1/', data, '_plot1.pdf', sep=''), family='sans', pointsize=9, width=6, height=3.25)
par(mar=(c(4, 4, 1, 13) + 0.1), mgp=c(3, 2, 0), xpd=TRUE)
barplot(cbind(
c(data1.pct, data2.pct, data3.pct),
c(class1.pct, class2.pct, class3.pct)), col=c("firebrick3", "dodgerblue3", "mistyrose1"), ylim=c(0,100), space=c(0,1)
)
legend("topright", inset=c(-0.55, 0), legend=c("not attend", "refused", "attend"), col=c("mistyrose1", "dodgerblue3", "firebrick3"), lty=1, lwd=2, bty='n')
dev.off()
and the result is
I would like to add corresponding percentages inside barplot, that is numbers/percentages in my variables. So My output should be:
I would like to use barplot funcion to do this and NOT ggplot2
I have tried adding percentages with
text(mydata, 0, round(data1.pct), 1),cex=1,pos=3) but this is not right.
To get the y-values for the text, you can use cumsum along with tail and head to get the midpoints of each bar section.
par(mar=(c(4, 4, 1, 13) + 0.1), mgp=c(3, 2, 0), xpd=TRUE)
## Make the matrix for barplot
mat <- cbind(c(data1.pct, data2.pct, data3.pct), c(class1.pct, class2.pct, class3.pct))
## Get the y-values for text
ys <- apply(mat, 2, function(x) c(x[1]/2, head(cumsum(x),-1) + tail(x,-1)/2))
## Make barplot, store x data
xs <- barplot(mat, col=c("firebrick3", "dodgerblue3", "mistyrose1"), ylim=c(0,100), space=c(0,1))
## Add text
text(rep(xs, each=nrow(ys)), c(ys), labels=c(mat))
legend("topright", inset=c(-0.55, 0), legend=c("not attend", "refused", "attend"), col=c("mistyrose1", "dodgerblue3", "firebrick3"), lty=1, lwd=2, bty='n')
Hi everybody I have 3 plots with density bars on either axis which I have done this way (here is a simpler form presented with only 3 ordinary plots but the other parts are necessary as required for a more complicated function which I have left off here just for the ease of viewing)
scatterBar.Norm <- function(x,y) {
zones <- matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
layout(zones, widths=c(5/7,2/7), heights=c(2/7,5/7))
title("My Title", outer=TRUE);
par(mar=c(3,3,1,1),mgp=c(2,1,0))
plot(1:10, xlab="Magnification", ylab="residue", col=2)
par(mar=c(0,3,1,1))
plot(1:10, xlab="Magnification", ylab="residue",col=3)
par(mar=c(3,0,1,1))
plot(1:10, xlab="Magnification", ylab="residue", col=4)}
scatterBar.Norm(2,3)
The problem :
Firstly the The plot title the "My Title" part is going out of the canvas , how to fix it ?
Thanks for the much needed help in advance.
You've instructed R to plot the title in the outer margin, but (at least in your example) you haven't set up that margin. The following should work:
scatterBar.Norm <- function(x, y) {
zones <- matrix(c(2, 0, 1, 3), ncol=2, byrow=TRUE)
layout(zones, widths=c(5, 2), heights=c(2, 5))
par(mar=c(3, 3, 1, 1), mgp=c(2, 1, 0), oma=c(0, 0, 3, 0))
plot(1:10, xlab="Magnification", ylab="residue", col=2)
par(mar=c(0, 3, 1, 1))
plot(1:10, xlab="Magnification", ylab="residue", col=3)
par(mar=c(3, 0, 1, 1))
plot(1:10, xlab="Magnification", ylab="residue", col=4)
title("My Title", outer=TRUE)
}
plot.new()
scatterBar.Norm(2, 3)