2017-12-03 18 views
0

に特定のバーのカラーリングはpts_allowedデータセットを想像してみて:GGPlot2 - 次のようにプロット

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

これらの特定のバーの色を変更するために事実の後に追加できる簡単なコード行はありますか?

答えて

3

ggplotで目的を達成するための通常の方法は、データフレーム内に追加の列を目的のマッピングで作成することです。例:結果は同じになりますが、伝説がする

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

:あなたは、プロットした後、それを必要とする場合

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(aes(fill = as.factor(ifelse(Opp %in% tms, 1, 0))))+ 
    scale_fill_manual("fill", values = c("gray40", "gray80")) 
+0

優れた、まさに私が何をしたか、非常に感謝しています – Morts81

関連する問題