Reputation: 306
I have been trying to plot my 2-way interactive boxplots with ggplot2 and I was quite successful so far. Since I want the viewers to be able to compare the results based on them being negative and positive, I figured it would be neat to change the color of gridline at y=0; I searched a lot and I could not find any similar questions online. I even tried to manipulate the theme() feature myself.
I realized that the the minor gridlines cannot have labels and in case minor and major gridlines overlap, the line would inherit the theme features for the major lines. So, I could transform y=0 into a minor gridline without any labels or I could have y=0 as a major line but then the rest would be minor and thus, no labels are shown. (I obviously don't want all of the gridlines to be darker than y=0 because then it would not be significantly obvious.
The chart below shows the first scenario:
p + scale_y_continuous(expand = c(0, 0), breaks = c(-40, 40, -20, 20), minor_breaks = c(0))
+theme(panel.grid.minor.y = element_line(color="#54545B"))
And This chart shows the second scenario:
p + scale_y_continuous(expand = c(0, 0), breaks = seq(-40, 40, 40), minor_breaks = c(-20, 20))
+theme(panel.grid.major.y = element_line(color="#54545B"))
Did anyone have the same issue? Is there any good solution to do this without using Photoshop (manually manipulating the charts!)?
Upvotes: 0
Views: 1429
Reputation: 1
In addition to neilfws's response, add your colour code to the color
argument in geom_hline()
. I've edited neilfw's code to include this:
set.seed(123)
data.frame(x = rep(c("A", "B"), each = 10),
y = sample(-100:100, 20, replace = TRUE)) %>%
ggplot(aes(x, y)) +
geom_hline(yintercept = 0,color="#54545B") +
geom_boxplot()
Upvotes: 0
Reputation: 33782
I'd use geom_hline(yintercept = 0)
. Specify it before geom_boxplot
so as it is in the layer underneath the boxes.
Example data:
set.seed(123)
data.frame(x = rep(c("A", "B"), each = 10),
y = sample(-100:100, 20, replace = TRUE)) %>%
ggplot(aes(x, y)) +
geom_hline(yintercept = 0) +
geom_boxplot()
Upvotes: 1