使用ggplot2在堆积条形图中设置边框

时间:2018-02-22 20:47:19

标签: r ggplot2

我想创建一个堆积条形图,并为所有'D'元素将边框颜色设置为黑色。使用

data

enter image description here

我能够突出显示一列,但不能突出显示所有列中的一个元素。 任何想法如何设置D的边框颜色?

2 个答案:

答案 0 :(得分:1)

当然,只需映射color,就像这样:

library(ggplot2)
ggplot(diamonds) +  
  geom_bar(aes(clarity, fill = color, 

               # 1) set the border (i.e. the color aesthetic) based on whether the value
               # of the relevant variable (which also happens to be called color) is D
               color = color=='D')) +

  # 2) use a scale such that FALSE is no color and TRUE is black, 
  # but don't include this in the legend
  scale_color_manual(values = c(NA, 'black'), guide=F)

enter image description here

答案 1 :(得分:0)

你可以通过使用scale_color_manual()并指定颜色来做到这一点,在这种情况下" D",黑色和所有其他NA:

library(ggplot2)
d <- ggplot(diamonds) +  geom_bar(aes(clarity, fill=color, colour = color)) +
  scale_color_manual(values = c("J" = NA,
                            "I" = NA,
                            "H" = NA,
                            "G" = NA,
                            "F" = NA,
                            "E" = NA,
                            "D" = "black"))

d