正如我所说,您使用错误的工具来获得您想要的东西。您正在设想一个无法直接从您的数据中获得的图(见底部)。
相反,您需要对数据进行建模。具体来说,您希望将每个类别中的预期支出部分预测为总支出的函数。然后,您所设想的图显示了该模型的拟合值(即,任何区域的预测支出比例)作为总支出的函数。这是一些使用loess
曲线执行此操作的代码。我绘制了原始数据和拟合值,以向您展示发生了什么。
# setup the data
data("household", package = "HSAUR2")
household$total <- rowSums(household[,1:4])
household <- within(household, {
housing <- housing/total
food <- food/total
goods <- goods/total
service <- service/total
})
# estimate loess curves
l_list <-
list(loess(housing ~ total, data = household),
loess(food ~ total, data = household),
loess(goods ~ total, data = household),
loess(service ~ total, data = household))
# stack fitted curves on top of one another
ndat <- data.frame(total = seq(min(household$total), max(household$total), 100))
p <- lapply(l_list, predict, newdata = ndat)
for(i in 2:length(l_list))
p[[i]] <- p[[i]] + p[[i-1]]
# plot
plot(NA, xlim=range(household$total), ylim = c(0,1), xlab='Total', ylab='Percent', las=1, xaxs='i')
# plot dots
with(household, points(total, housing, pch = 20, col = palette()[1]))
with(household, points(total, housing + food, pch = 20, col = palette()[2]))
with(household, points(total, housing + food + goods, pch = 20, col = palette()[3]))
with(household, points(total, housing + food + goods + service, pch = 20, col = palette()[4]))
# plot fitted lines
for(i in 1:length(p))
lines(ndat$total, p[[i]], type = 'l', lwd = 2, col = palette()[i])
结果:
如果您尝试根据原始数据创建这样的图,它看起来会有些奇怪,但也许这就是您想要的:
plot(NA, xlim=range(household$total), ylim = c(0,1), xlab='Total', ylab='Percent', las=1, xaxs='i')
with(household, lines(total[order(total)], housing[order(total)], pch = 20, col = palette()[1]))
with(household, lines(total[order(total)], (housing + food)[order(total)], pch = 20, col = palette()[2]))
with(household, lines(total[order(total)], (housing + food + goods)[order(total)], pch = 20, col = palette()[3]))
with(household, lines(total[order(total)], (housing + food + goods + service)[order(total)], pch = 20, col = palette()[4]))
结果: