Reputation: 345
I am trying to show different colors for coefficients that are not significant (p>0.05) and the ones that are. Plus, if someone has a way to show the legend or signify the colors that would also be nice..
Any ideas?
Sample code:
library(nycflights13)
library(dplyr)
library(dotwhisker)
library(MASS)
flights <- nycflights13::flights
flights<- sample_n (flights, 500)
m1<- glm(formula = arr_delay ~ dep_time + origin+ air_time+ distance , data = flights)
#m1<- glm(formula = arr_delay ~ . , data = flights)
m1<- stepAIC(m1)
p<- dotwhisker::dwplot(m1)
z<- p +
geom_vline(xintercept=0, linetype="dashed")+
geom_segment(aes(x=conf.low,y=term,xend=conf.high,
yend=term,col=p.value<0.05)) +
geom_point(aes(x=estimate,y=term,col=p.value<0.05)) +
xlab("standardized coefficient") +
ylab("coefficient") +
ggtitle("coefficients in the model and significance")
print(z)
Upvotes: 1
Views: 168
Reputation: 174468
Your code already kind of does what you want. The problem is that the object p
produced by dwplot
already has a geom_segment
layer and a geom_point
layer with a number of aesthetic mappings. Their colors are currently mapped to the variable model
, which is just a factor level allowing for different colorings when comparing models side by side. It is possible to over-write them though:
p$layers[[1]]$mapping[5] <- aes(color = p.value < 0.05)
p$layers[[2]]$mapping[4] <- aes(color = p.value < 0.05)
And you can change the legend label with
p$labels$colour <- "Significant"
By default, dwplot
also hides the legend, but we can reset that with:
p$theme <- list()
So without adding any new geoms or creating the object z
, we have:
p
Note that p
is still a valid and internally consistent ggplot, so you can continue to style it as desired, for example:
p + theme_bw() + geom_vline(xintercept = 0, lty = 2)
Upvotes: 4