r - ggplot2 lm lines with categorical variables -
i have info r course of study class. professor adding each line kind of manually using base of operations graphics. i'd using ggplot2
.
so far i've created facet
'd plot in ggplot
scatter plots
of hunger
in different regions , separately fitted model data. specific model has interaction terms between x
variable in plot , group/colour
variable.
what want plot lines resulting model 1 per panel. using geom_abline
, defining slope
, intercept
sum of 2 of coefficients (as categorical variables grouping have 0/1 values , in each panel values multiplied 1) - seems not easy.
i tried same equation used in lm in stat_smooth
no luck, error.
ideally, i'd think 1 can set equation somehow stat_smooth
, have ggplot
work. how 1 go it?
download.file("https://sparkpublic.s3.amazonaws.com/dataanalysis/hunger.csv", "hunger.csv", method = "curl") hunger <- read.csv("hunger.csv") hunger <- hunger[hunger$sex!="both sexes",] hunger_small <- hunger[hunger$who.region!="who non members",c(5,6,8)] q<- qplot(x = year, y = numeric, info = hunger_small, color = who.region) + theme(legend.position = "bottom") q <- q + facet_grid(.~who.region)+guides(col=guide_legend(nrow=2)) q # add together standard lm line stat_smooth, dont want # q <- q + geom_smooth(method="lm",se=f) #i want add together line(s) lm fit below, 1 line per panel lmregion <- lm(hunger$numeric ~ hunger$year + hunger$who.region + hunger$year *hunger$who.region) # used loop it, below, in 1 panel # not able # facets, used function found colors ggplotcolours <- function(n=6, h=c(0, 360) +15) { if ((diff(h)%%360) < 1) h[2] <- h[2] - 360/n hcl(h = (seq(h[1], h[2], length = n)), c = 100, l = 65) } n <- length(levels(hunger_small$who.region)) q <- qplot(x = year, y = numeric, info = hunger_small, color = who.region) + theme(legend.position = "bottom") q <- q + geom_abline(intercept = lmregion$coefficients[1], slope = lmregion$coefficients[2], color = ggplotcolours(n=n)[1]) (i in 2:n) { q <- q + geom_abline(intercept = lmregion$coefficients[1] + lmregion$coefficients[1+i], slope = lmregion$coefficients[2] + lmregion$coefficients[7+i], color = ggplotcolours(n=n)[i]) }
r ggplot2
No comments:
Post a Comment