scale_color_grey() 的图例排序不正确
Incorrect Legend Ordering with scale_color_grey()
我正在进行 Monte Carlo 模拟,其中我必须在同一图上显示具有不同样本大小的模拟的系数估计密度。使用 scale_color_grey
时。我将我的系数估计放在同一个数据框中,样本大小作为一个因素。如果我用 levels()
查询因子,它的顺序是正确的(从最小到最大样本量)。然而,下面的代码给出了一个刻度,其中顺序在图例中是正确的,但是颜色以看似随机的顺序从浅灰色移动到深灰色
montecarlo <- function(N, nsims, nsamp){
set.seed(8675309)
coef.mc <- vector()
for(i in 1:nsims){
access <- rnorm(N, 0, 1)
health <- rnorm(N, 0, 1)
doctorpop <- (access*1) + rnorm(N, 0, 1)
sick <- (health*-0.4) + rnorm(N, 0, 1)
insurance <- (access*1) + (health*1) + rnorm(N, 0, 1)
healthcare <- (insurance*1) + (doctorpop*1) + (sick*1) + rnorm(N, 0, 1)
data <- as.data.frame(cbind(healthcare, insurance, sick, doctorpop))
sample.data <- data[sample(nrow(data), nsamp), ]
model <- lm(data=sample.data, healthcare ~ insurance + sick + doctorpop)
coef.mc[i] <- coef(model)["insurance"]
}
return(as.data.frame(cbind(coef.mc, nsamp)))
}
sample30.df <- montecarlo(N=1000, nsims=1000, nsamp=30)
sample100.df <- montecarlo(1000,1000,100)
sample200.df <- montecarlo(1000, 1000, 200)
sample500.df <- montecarlo(1000, 1000, 500)
sample1000.df <- montecarlo(1000, 1000, 1000)
montecarlo.df <- rbind(sample30.df, sample100.df, sample200.df, sample500.df, sample1000.df)
montecarlo.df$nsamp <- as.factor(montecarlo.df$nsamp)
levels(montecarlo.df$nsamp) <- c("30", "100", "200", "500", "1000")
##creating the plot
montecarlo.plot <- ggplot(data=montecarlo.df, aes(x=coef.mc, color=nsamp))+
geom_line(data = subset(montecarlo.df, nsamp==30), stat="density")+
geom_line(data = subset(montecarlo.df, nsamp==100), stat="density")+
geom_line(data = subset(montecarlo.df, nsamp==200), stat="density")+
geom_line(data = subset(montecarlo.df, nsamp==500), stat="density")+
geom_line(data = subset(montecarlo.df, nsamp==1000), stat="density")+
scale_color_grey(breaks=c("30", "100","200", "500", "1000"))+
labs(x=NULL, y="Density of Coefficient Estimate: Insurance", color="Sample Size")+
theme_bw()
montecarlo.plot
不使用 breaks
参数 scale_color_grey
returns 阴影顺序正确的图例,但样本量不会从最小到最大增加。
这是怎么回事?据我了解,ggplot2
在分配颜色和创建图例时应遵循因子的顺序(这是正确的)。我怎样才能使图例和灰色阴影从最小样本量增加到最低样本量?
您应该让 ggplot
处理为 nsamp
的每个级别绘制单独的线条:因为您已将 nsamp
映射到颜色审美,ggplot
将自动绘制每个级别都有不同的行,所以你可以这样做:
montecarlo.plot <- ggplot(data=montecarlo.df, aes(x=coef.mc, color=nsamp))+
geom_line(stat = "density", size = 1.2) +
scale_color_grey() +
labs(x=NULL, y="Density of Coefficient Estimate: Insurance", color="Sample Size")+
theme_bw()
montecarlo.plot
无需手动对数据进行子集化。
我正在进行 Monte Carlo 模拟,其中我必须在同一图上显示具有不同样本大小的模拟的系数估计密度。使用 scale_color_grey
时。我将我的系数估计放在同一个数据框中,样本大小作为一个因素。如果我用 levels()
查询因子,它的顺序是正确的(从最小到最大样本量)。然而,下面的代码给出了一个刻度,其中顺序在图例中是正确的,但是颜色以看似随机的顺序从浅灰色移动到深灰色
montecarlo <- function(N, nsims, nsamp){
set.seed(8675309)
coef.mc <- vector()
for(i in 1:nsims){
access <- rnorm(N, 0, 1)
health <- rnorm(N, 0, 1)
doctorpop <- (access*1) + rnorm(N, 0, 1)
sick <- (health*-0.4) + rnorm(N, 0, 1)
insurance <- (access*1) + (health*1) + rnorm(N, 0, 1)
healthcare <- (insurance*1) + (doctorpop*1) + (sick*1) + rnorm(N, 0, 1)
data <- as.data.frame(cbind(healthcare, insurance, sick, doctorpop))
sample.data <- data[sample(nrow(data), nsamp), ]
model <- lm(data=sample.data, healthcare ~ insurance + sick + doctorpop)
coef.mc[i] <- coef(model)["insurance"]
}
return(as.data.frame(cbind(coef.mc, nsamp)))
}
sample30.df <- montecarlo(N=1000, nsims=1000, nsamp=30)
sample100.df <- montecarlo(1000,1000,100)
sample200.df <- montecarlo(1000, 1000, 200)
sample500.df <- montecarlo(1000, 1000, 500)
sample1000.df <- montecarlo(1000, 1000, 1000)
montecarlo.df <- rbind(sample30.df, sample100.df, sample200.df, sample500.df, sample1000.df)
montecarlo.df$nsamp <- as.factor(montecarlo.df$nsamp)
levels(montecarlo.df$nsamp) <- c("30", "100", "200", "500", "1000")
##creating the plot
montecarlo.plot <- ggplot(data=montecarlo.df, aes(x=coef.mc, color=nsamp))+
geom_line(data = subset(montecarlo.df, nsamp==30), stat="density")+
geom_line(data = subset(montecarlo.df, nsamp==100), stat="density")+
geom_line(data = subset(montecarlo.df, nsamp==200), stat="density")+
geom_line(data = subset(montecarlo.df, nsamp==500), stat="density")+
geom_line(data = subset(montecarlo.df, nsamp==1000), stat="density")+
scale_color_grey(breaks=c("30", "100","200", "500", "1000"))+
labs(x=NULL, y="Density of Coefficient Estimate: Insurance", color="Sample Size")+
theme_bw()
montecarlo.plot
不使用 breaks
参数 scale_color_grey
returns 阴影顺序正确的图例,但样本量不会从最小到最大增加。
这是怎么回事?据我了解,ggplot2
在分配颜色和创建图例时应遵循因子的顺序(这是正确的)。我怎样才能使图例和灰色阴影从最小样本量增加到最低样本量?
您应该让 ggplot
处理为 nsamp
的每个级别绘制单独的线条:因为您已将 nsamp
映射到颜色审美,ggplot
将自动绘制每个级别都有不同的行,所以你可以这样做:
montecarlo.plot <- ggplot(data=montecarlo.df, aes(x=coef.mc, color=nsamp))+
geom_line(stat = "density", size = 1.2) +
scale_color_grey() +
labs(x=NULL, y="Density of Coefficient Estimate: Insurance", color="Sample Size")+
theme_bw()
montecarlo.plot
无需手动对数据进行子集化。