-
Notifications
You must be signed in to change notification settings - Fork 5
ggplot alternatives to logi.hist.plot
Chris Stubben edited this page Feb 9, 2017
·
5 revisions
A few ggplot alternatives to the logi.hist.plot
function.
data(aq.trans)
aq.trans$survived<-aq.trans$fate!="dead"
a<-subset(aq.trans, leaf<50 & stage!="recruit", c(leaf,survived))
ggplot(a, aes(x = leaf, y = as.numeric(survived) ) ) +
geom_jitter(height = 0.02, alpha=0.3, color="blue") +
geom_smooth(method="glm", method.args = list(family = "binomial") ) +
xlab("Number of leaves") +
ylab("Survival probability")

To plot histograms, get the coordinates for each histogram bin following this answer
binned <-
mutate(a, bin = cut(leaf, pretty(leaf, n=10))) %$%
table(bin, survived) %>%
prop.table() %>%
as.data.frame() %>%
separate(bin, c("xmin","xmax"), ",") %>%
mutate_each(funs(parse_number(.)), xmin, xmax) %>%
mutate(ymin = ifelse(survived=="FALSE", 0, 1 - Freq),
ymax = ifelse(survived=="TRUE", 1, Freq))
ggplot() +
geom_rect(data = binned, fill="blue",
aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax)) +
geom_smooth( data=a, aes(x = leaf, y = as.numeric(survived) ),
method="glm", method.args = list(family = "binomial") ) +
xlab("Number of leaves") +
ylab("Survival probability")
