Control bar border (color) thickness with ggplot2 stroke

。_饼干妹妹 提交于 2019-12-09 05:21:45

问题


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")

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)


回答1:


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)


来源:https://stackoverflow.com/questions/34878848/control-bar-border-color-thickness-with-ggplot2-stroke

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!