1

I am trying to color estimates and intervals as either black (if lower bound of interval is above 1) or gray (if lower bound of interval is below 1). Here is my code:

p <- ggplot(plot.data)+ scale_y_continuous(breaks = c(0, 1, 2),labels = c("1","2.7","7.4"),name="Odds Ratio")
p <- p + geom_hline(yintercept = 0, colour = gray(1/2), lty = 2)
p <- p + geom_pointrange(aes(x = DV, y = Estimate, ymin = Lower,
                             ymax = Upper),
                         lwd = 1/2, position = position_dodge(width = 1/2),
                         shape = 16,color=ifelse(plot.data$Lower<0,"gray60","black")) #+ facet_grid(Missing ~.)+ theme_classic()
p <- p + xlab(label="Predictor Variable") + theme(axis.text.x=element_text(angle=45,hjust=1,vjust=.6))+theme(strip.text.x = element_blank())
p <- p +  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + facet_grid(vars(Missing),vars(Number),scales = "free_x",space="free_x")
p

For some reason, this code only partially works:

enter image description here

Specifically, there are a couple cases that don't seem to be adhering to the rule. For example, look at "PT" as a predictor variable in the lower row. This is colored black even though the lower bound of the interval crosses zero.

There doesn't seem to be any issues with the upper row, so is this maybe some problem with applying the rule with facet_grid? That is, it doesn't apply the rule equally to all facets?

EDIT: This has been solved. See the comment section below.

user2917781
  • 273
  • 2
  • 10
  • 2
    This has to do with you using dollar sign notation for `color`; referring to the variable directly should fix things. See [this question](https://stackoverflow.com/questions/32543340/issue-when-passing-variable-with-dollar-sign-notation-to-aes-in-combinatio). Looks like this is going to change in the [next release of ggplot2](https://github.com/tidyverse/ggplot2/pull/2694). – aosmith Aug 22 '18 at 18:14
  • 1
    Yes, this solved my problem. Thanks. – user2917781 Aug 22 '18 at 18:58

0 Answers0