R在X轴上添加不需要的标签到刻度线

时间:2017-01-30 01:40:37

标签: r

我试图绘制四个近似泊松变量的分布。我已经做了很多次,但是R会随机添加一个“0”标签到其中一个刻度线,我无法弄清楚原因。

以下是模拟数据的示例:

# Simulating data
set.seed(1)
x1 = rpois(100, .2)
x2 = rpois(100, 2.8)
x3 = rpois(100, 4.9)
x4 = rpois(100, 9.2)

# Now, I only want 11 bars because I want to aggregate all values above 10
x1 = sapply(0:10, function(k) ifelse(k < 10, sum(x1 == k), sum(x1 >= k)))
x2 = sapply(0:10, function(k) ifelse(k < 10, sum(x2 == k), sum(x2 >= k)))
x3 = sapply(0:10, function(k) ifelse(k < 10, sum(x3 == k), sum(x3 >= k)))
x4 = sapply(0:10, function(k) ifelse(k < 10, sum(x4 == k), sum(x4 >= k)))

# Merging vectors into a matrix
m = rbind(x1, x2, x3, x4)

# Naming columns to use as labels to the X-axis
colnames(m) = c(0:9, ">= 10")

# Vector of colors to use in the legend
cols = rgb(t(col2rgb(c("red", "blue", "yellow", "green"))), alpha = 255/4, maxColorValue = 255)

# Plotting
pdf("m.pdf")
barplot(m, beside = T, col = cols, xlim = c(0, 10), ylim = c(0, 100), xlab = "", axes = F, width = 1, space = c(.2, 5))
box()
legend("topright", c("x1", "x2", "x3", "x4"), lty = rep(1, 4), lwd = rep(10, 4), col = cols)
axis(1, at = 0:10, labels = colnames(m))
axis(2)
title(main = "Distribution of counts", xlab = "Values", ylab = "observations", outer = T)
dev.off()

这是输出

enter image description here

0 个答案:

没有答案