Control bar border (color) thickness with ggplot2 stroke

lawyeR picture lawyeR · Jan 19, 2016 · Viewed 33.5k times · Source

Is it possible to use the stroke argument introduced with ggplot2 2.0 to adjust the thickness of borders around bars? If not, is there a way to control bar-border thickness along the lines of point-border thickness? Stroke applies to borders around certain shapes -- see the second answer

A very modest MWE, showing fill only:

factor <- c("One", "Two", "Three", "Four")
value <- c(1, 2, 3, 4)
factor2 <- c("A", "B", "A", "B")

df <- data.frame(factor = factor(factor, levels = factor),
                 value = value, factor2 = factor2) 

ggplot(df, aes(x = factor, y = value, color = factor2)) +
  geom_bar(stat = "identity")

Colored borders, but what about size of borders

EDIT after COMMENT OK, thanks to MLavoie's comment, it was so simple. Here is the code I have ended with, and, no, I am not actually using this plot other than to teach about ggplot and its capabilities.

ggplot(df, aes(x = factor, y = value, color = factor2)) +
  scale_color_manual(values = c("darkgreen", "slateblue4")) +
  geom_bar(stat = "identity", aes(fill = "transparent", size = ifelse(factor2 == "A", 2, 1))) +
  guides(fill = FALSE) +
  guides(size = FALSE) +
  guides(color = FALSE)

Modifying bar borders by thickness and shade

Answer

MLavoie picture MLavoie · Jan 19, 2016

well as I suggested by the OP, I will just recopy my comment as an answer.

you just need to set size in your geom_bar() expression:

geom_bar(stat = "identity", aes(fill = "transparent", size = ifelse(factor2 == "A", 2, 1)), size=2)