如何修复条形图中轴标题与轴标签重叠的问题?
How to fix axis title overlapped with axis labels in barplot?
我为我的数据生成了一个条形图 plot/histogram,它显示了包大小的交易数量。但是,条形的 x 轴上的标签超出了边距。该图如下所示。
我试图通过将外边距设置为 par(oma=c(3,3,0,0))
来解决这个问题。这是我的新情节。
虽然标签在图边距内,但 x-axis 标题仍然与标签重叠。我应该如何调整轴标题使其不与标签重叠?
如有任何建议,我们将不胜感激!
使用axis
和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)
或者这样可能更好:
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 <- 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))
以jay.sf为例,我们可以添加任何情节:
title(xlab = "My Label", line = 10)
其中 line
可适应:10 或 9 或 8 等...
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)
我为我的数据生成了一个条形图 plot/histogram,它显示了包大小的交易数量。但是,条形的 x 轴上的标签超出了边距。该图如下所示。
我试图通过将外边距设置为 par(oma=c(3,3,0,0))
来解决这个问题。这是我的新情节。
虽然标签在图边距内,但 x-axis 标题仍然与标签重叠。我应该如何调整轴标题使其不与标签重叠?
如有任何建议,我们将不胜感激!
使用axis
和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)
或者这样可能更好:
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 <- 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))
以jay.sf为例,我们可以添加任何情节:
title(xlab = "My Label", line = 10)
其中 line
可适应:10 或 9 或 8 等...
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)