r - Incorrect Legend Ordering with scale_color_grey() -
i doing monte carlo simulation, in have display density of coefficient estimates simulations different sample sizes on same plot. when using scale_color_grey
. have put coefficient estimates in same dataframe, sample size factor. if query factor levels()
, in correct order (from smallest highest sample size). however, following code gives scale in order correct in legend, color moves light grey darker grey in seemingly random order
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 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
not using breaks
argument scale_color_grey
returns legend in shades in right order, not increase smallest highest sample size.
what going on here? far understand it, ggplot2
should follow factor's order (which correct) in assigning colors , creating legend. how can make both legend , shades of grey increase smallest lowest sample size?
you should let ggplot
handle drawing separate lines each level of nsamp
: because have mapped nsamp
colour aesthetic, ggplot
automatically draw different line each level, can do:
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
no need manually subset data.
Comments
Post a Comment