对于这个问题,我已经能够将数据分成两个直方图,一个是收入高于中位数,另一个是收入低于中位数。到目前为止,以下代码是我所做的:
library(openintro)
data("countyComplete")
attach("countyComplete")
median(median_household_income, na.rm = FALSE)
x<-subset(countyComplete,median_household_income > 42445)
y<-subset(countyComplete,median_household_income < 42445)
par(mfrow=c(1,2))
hist(x$median_household_income, main="Income Above Median" )
hist(y$median_household_income,main = "Income Below Median")
但是,我对如何迫使直方图在y轴上使用相同的限制以及中断感到有些困惑。有人可以指出我正确的方向。我尝试这样做:
par(mfrow=c(1,2))
hist(x$median_household_income,
breaks=seq(0,100,by=5),
freq = FALSE,
ylim=c(0,.15),
xlim = range(breaks),
main="Income Above Median")
hist(y$median_household_income, main = "Income Below Median")
但是我的绘图屏幕上只显示一个直方图,控制台说 “ hist.default(x $ median_household_income,breaks = seq(0,100,: 一些“ x”不计算在内;也许“中断”不跨越“ x”的范围。”
我该怎么办?
答案 0 :(得分:0)
我会忘记breaks
的论点。这没有任何意义,您正在绘制中位数以下和上方的值,它们不会相交。
对于直方图,我已经预先计算了中位数和密度的最大值。
library(openintro)
data("countyComplete")
med <- median(countyComplete$median_household_income, na.rm = FALSE)
x <- subset(countyComplete, median_household_income > med)
y <- subset(countyComplete, median_household_income < med)
hx <- hist(x$median_household_income, plot = FALSE)
hy <- hist(y$median_household_income, plot = FALSE)
MaxY <- max(c(hx$density, hy$density))
op <- par(mfrow = c(1, 2))
hist(x$median_household_income, main = "Income Above Median",
freq = FALSE, ylim = c(0, MaxY))
hist(y$median_household_income, main = "Income Below Median",
freq = FALSE, ylim = c(0, MaxY))
par(op)