Presenting results of logistic regression
[This article was first published on Recology, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
So my advisor pointed out this ‘new’ (well, 2004), way of plotting results of logistic regression results. The idea was presented in a 2004 Bulletin of the Ecological Society of America issue (here). I tried to come up with a solution using, what else, ggplot2. I don’t have it quite all the way down – I am missing the second y-axis values for the histograms, but someone smarter than me can figure that part out (note that Hadley doesn’t want to support second y-axes in ggplot2, but they can probably be hacked on).
Here’s the code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the function | |
loghistplot <- function(data) { | |
require(ggplot2); require(gridExtra) # load packages | |
names(data) <- c('x','y') # rename columns | |
# get min and max axis values | |
min_x <- min(data$x) | |
max_x <- max(data$x) | |
min_y <- min(data$y) | |
max_y <- max(data$y) | |
# get bin numbers | |
bin_no <- max(hist(data$x)$counts) + 5 | |
# create plots | |
a <- ggplot(data, aes(x = x, y = y)) + | |
theme_bw(base_size=16) + | |
geom_smooth(method = "glm", family = "binomial", se = TRUE, | |
colour='black', size=1.5, alpha = 0.3) + | |
# scale_y_continuous(limits=c(0,1), breaks=c(0,1)) + | |
scale_x_continuous(limits=c(min_x,max_x)) + | |
opts(panel.grid.major = theme_blank(), | |
panel.grid.minor=theme_blank(), | |
panel.background = theme_blank()) + | |
labs(y = "Probability\n", x = "\nYour X Variable") | |
b <- ggplot(data[data$y == unique(data$y)[1], ], aes(x = x)) + | |
theme_bw(base_size=16) + | |
geom_histogram(fill = "grey") + | |
scale_y_continuous(limits=c(0,bin_no)) + | |
scale_x_continuous(limits=c(min_x,max_x)) + | |
opts(panel.grid.major = theme_blank(), | |
panel.grid.minor=theme_blank(), | |
axis.text.y = theme_blank(), | |
axis.text.x = theme_blank(), | |
axis.ticks = theme_blank(), | |
panel.border = theme_blank(), | |
panel.background = theme_blank()) + | |
labs(y='\n', x='\n') | |
c <- ggplot(data[data$y == unique(data$y)[2], ], aes(x = x)) + | |
theme_bw(base_size=16) + | |
geom_histogram(fill = "grey") + | |
scale_y_continuous(trans='reverse') + | |
scale_y_continuous(trans='reverse', limits=c(bin_no,0)) + | |
scale_x_continuous(limits=c(min_x,max_x)) + | |
opts(panel.grid.major = theme_blank(),panel.grid.minor=theme_blank(), | |
axis.text.y = theme_blank(), axis.text.x = theme_blank(), | |
axis.ticks = theme_blank(), | |
panel.border = theme_blank(), | |
panel.background = theme_blank()) + | |
labs(y='\n', x='\n') | |
grid.newpage() | |
pushViewport(viewport(layout = grid.layout(1,1))) | |
vpa_ <- viewport(width = 1, height = 1, x = 0.5, y = 0.5) | |
vpb_ <- viewport(width = 1, height = 1, x = 0.5, y = 0.5) | |
vpc_ <- viewport(width = 1, height = 1, x = 0.5, y = 0.5) | |
print(b, vp = vpb_) | |
print(c, vp = vpc_) | |
print(a, vp = vpa_) | |
} | |
# Examples | |
# loghistplot(mtcars[,c("mpg","vs")]) | |
# loghistplot(movies[,c("rating","Action")]) | |
logpointplot <- function(data) { | |
require(ggplot2); require(gridExtra) # load packages | |
names(data) <- c('x','y') # rename columns | |
# get min and max axis values | |
min_x <- min(data$x) | |
max_x <- max(data$x) | |
min_y <- min(data$y) | |
max_y <- max(data$y) | |
# create plots | |
ggplot(data, aes(x = x, y = y)) + | |
theme_bw(base_size=16) + | |
geom_point(alpha = 0.5, position = position_jitter(w=0, h=0.02)) + | |
geom_smooth(method = "glm", family = "binomial", se = TRUE, | |
colour='black', size=1.5, alpha = 0.3) + | |
scale_x_continuous(limits=c(min_x,max_x)) + | |
opts(panel.grid.major = theme_blank(), | |
panel.grid.minor=theme_blank(), | |
panel.background = theme_blank()) + | |
labs(y = "Probability\n", x = "\nYour X Variable") | |
} | |
# Examples | |
# logpointplot(mtcars[,c("mpg","vs")]) | |
# logpointplot(movies[,c("rating","Action")]) |
Here’s a few examples using datasets provided with the ggplot2 package:
loghistplot(mtcars[,c("mpg","vs")])
loghistplot(movies[,c("rating","Action")])
To leave a comment for the author, please follow the link and comment on their blog: Recology.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.