r - Graphing solutions for over-plotted data, faceting, and adding line of best fit -
originally, thinking need 2 plots - 1 plot "female_life_growth" vs "rate_median_income" "census region" , 1 if red/blue state (red_blue). i've been messing ggplot can't figure out plot complicated of data.
update: here's i've tried; way make better visual?. 
update: added faceting. how add line of best fit each?. 
sample data:
df <- data.frame(female_life_growth=c(-3.48,-3.48,-3.39,-3.39,-3.17,-3.17, -2.73,-2.57,-2.57,-2.57,-2.57,-2.51), red_blue=c('red','red','red','blue','red','blue', 'blue','red','blue','red','blue','red'), rate_median_income=c(33378,45726,31211,20206,29050,33799, 38664,32538,39171,41403,34217,35789), census_region=c('south','south','south','midwest','midwest', 'west','northeast','west','west','northeast', 'midwest','northeast'))
here's 1 way:
require(ggplot2) ggplot(df) + geom_point(aes(x=rate_median_income,y=female_life_growth, col=red_blue,shape=census_region),size=2.5) + scale_color_manual(breaks=c("blue","red"),values=c("blue","red")) + xlab("rate median income") + ylab("female life growth") + guides(shape=guide_legend("census region"), col=guide_legend("political leaning\nof state")) output:
edit: in case of over-plotting, try faceting data:
ggplot(df) + geom_point(aes(x=rate_median_income,y=female_life_growth, col=red_blue,shape=census_region),size=2.5) + facet_wrap(~ census_region) + scale_color_manual(breaks=c("blue","red"),values=c("blue","red")) + xlab("rate median income") + ylab("female life growth") + guides(shape=guide_legend("census region"), col=guide_legend("political leaning\nof state")) output:
note: can remove legends if you'd like. included them educational purposes. remove shape legend, example, use guides(shape=false).
edit: add line of best fit each, try this:
ggplot(df) + geom_point(aes(x=rate_median_income,y=female_life_growth, col=red_blue,shape=census_region),size=2.5) + geom_smooth(aes(x=rate_median_income,y=female_life_growth), method="lm", se=false, col="black") + facet_wrap(~ census_region) + scale_color_manual(breaks=c("blue","red"),values=c("blue","red")) + xlab("rate median income") + ylab("female life growth") + guides(shape=guide_legend("census region"), col=guide_legend("political leaning\nof state")) output:



Comments
Post a Comment