GGPlot2 - 绘图中的着色特定条形

时间:2017-12-03 12:02:12

标签: r plot ggplot2

想象一下数据集pts_allowed如下:

   Opp Gms    MBs     DSs Mins MBvOpp DSvOpp
24 PHO  24 5638.4 5495.00 5785   9.09   8.31
22 ORL  23 5313.1 5206.25 5520   7.73   7.54
3  BRK  22 5062.0 4944.00 5279   7.32   6.78
1  ATL  22 4933.6 4924.00 5280   4.58   6.33
4  CHI  21 4683.2 4628.75 5065   3.49   4.20
14 LAL  22 4920.5 4790.50 5330   3.33   2.48
13 LAC  21 4673.1 4582.00 5065   3.26   3.14

我按照以下方式绘制:

library(ggplot2)

plot <- ggplot(pts_allowed, aes(Opp, MBvOpp)) 
plot <- plot + geom_bar(stat = 'identity', fill = 'gray40')
plot <- plot + theme(text = element_text(size = 16), axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5), panel.background = element_rect(fill = "aliceblue"),plot.background = element_rect(fill = "aliceblue"))
plot <- plot + theme(axis.ticks.x = element_blank(), axis.ticks.y = element_blank())
plot <- plot + theme(axis.text.x = element_text(colour = "gray40"),axis.text.y = element_text(colour = "gray40"))
plot <- plot + theme(axis.title.x = element_text(colour = "gray40"),axis.title.y = element_text(colour = "gray40"))

enter image description here

然而,在此之后,我想为pts_allowed $ Opp在向量tms中的条形指定不同的颜色:

tms <- c("PHO","LAL","BRK","CHI")

是否有一行简单的代码我可以在更改这些特定条形图的颜色后添加?

1 个答案:

答案 0 :(得分:3)

在ggplot中完成所需的常用方法是在数据框中使用所需的映射创建一个额外的列。例如:

tms <- c("PHO","LAL","BRK","CHI")
df <- read.table(text = "   Opp Gms    MBs     DSs Mins MBvOpp DSvOpp
           24 PHO  24 5638.4 5495.00 5785   9.09   8.31
           22 ORL  23 5313.1 5206.25 5520   7.73   7.54
           3  BRK  22 5062.0 4944.00 5279   7.32   6.78
           1  ATL  22 4933.6 4924.00 5280   4.58   6.33
           4  CHI  21 4683.2 4628.75 5065   3.49   4.20
           14 LAL  22 4920.5 4790.50 5330   3.33   2.48
           13 LAC  21 4673.1 4582.00 5065   3.26   3.14", header = T)

library(tidyverse)

df %>%
  mutate(fill = as.factor(ifelse(Opp %in% tms, 1, 0))) %>% #make an additional column named fill which will be 1 if Opp is in tms and 0 otherwise. Instead of 0/1 one can specify legend key names for instance: "tms", "not tms"
  ggplot(aes(Opp, MBvOpp)) +
  geom_col(aes(fill = fill)) + #geom_col is the same as geom_bar("identity"), specify fill inside aes here.
  theme(text = element_text(size = 16),
        axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5, colour = "gray40"),
        panel.background = element_rect(fill = "aliceblue"),
        plot.background = element_rect(fill = "aliceblue"),
        axis.ticks.x = element_blank(),
        axis.ticks.y = element_blank(),
        axis.text.y = element_text(colour = "gray40"),
        axis.title.x = element_text(colour = "gray40"),
        axis.title.y = element_text(colour = "gray40")) +
scale_fill_manual(values = c("gray40", "gray80")) # to keep the grey pallete 

enter image description here

如果您在绘图后需要它,可以添加以下代码行:

plot + geom_col(fill = ifelse(df$Opp %in% tms, "gray40", "grey80"))

结果将是相同的,但由于没有在aes中指定填充

,因此将缺少图例

如果需要图例:

plot + 
  geom_col(aes(fill = as.factor(ifelse(Opp %in% tms, 1, 0))))+
  scale_fill_manual("fill", values = c("gray40", "gray80"))