2017-04-16 5 views
2

私はggplot2でいくつかのプロットを作成しているより大きなプロジェクトに取り組んでいます。プロットは、いくつかの控えめなカテゴリー(国、種、種類)を考慮して、いくつかの異なる結果をプロットすることに関係しています。 Type = Aは常に赤で表示され、Type = Bは常に青で表示され、その他の要素が何であるかにかかわらず、すべてのプロットで同様に表示されるように、離散タイプのマッピングを色に完全に修正したいと思います。私はscale_fill_manual()について知っています。私は手動で色彩値を提供してから、未使用の因子レベルを扱うのに役立つdrop = FALSEで作業します。しかし、私は、すべてのプロットが正しい方法で因子を並べ替えること、因子のソートに合うように色の値をソートすること、未使用のレベルを落とすことなどを扱うために手作業を必要とするので、これは非常に面倒であることがわかります。ggplot2:因子レベルへの色の修正

私が探しているものは私はを一度、特定の色(A =緑、B =青、C =赤、...)にグローバルにの因子レベルをマップして、それから私が喜ばれるものをプロットして、適切な色を選択する方法について説明します。

ここにポイントを説明するコードがあります。

# Full set with 4 categories 
df1 <- data.frame(Value = c(40, 20, 10, 60), 
        Type = c("A", "B", "C", "D")) 

ggplot(df1, aes(x = Type, y = Value, fill = Type)) + geom_bar(stat = "identity") 


# Colors change complete because only 3 factor levels are present 
df2 <- data.frame(Value = c(40, 20, 60), 
        Type = c("A", "B", "D")) 

ggplot(df2, aes(x = Type, y = Value, fill = Type)) + geom_bar(stat = "identity") 


# Colors change because factor is sorted differently 
df3 <- data.frame(Value = c(40, 20, 10, 60), 
        Type = c("A", "B", "C", "D")) 
df3$Type <- factor(df3$Type, levels = c("D", "C", "B", "A"), ordered = TRUE) 

ggplot(df3, aes(x = Type, y = Value, fill = Type)) + geom_bar(stat = "identity") 

答えて

3

あなたは、コードの繰り返しを避けるために、(scale_fill_manualかつ合理的なデフォルト色を含む)のカスタムプロット関数を作ることができます:

library(ggplot2) 
custom_plot <- function(.data, 
    colours = c("A" = "green", "B" = "blue", "C" = "red", "D" = "grey")) { 
    ggplot(.data, aes(x=Type, y=Value, fill= Type)) + geom_bar(stat="identity") + 
    scale_fill_manual(values = colours) 
} 

df1 <- data.frame(Value=c(40, 20, 10, 60), Type=c("A", "B", "C", "D")) 
df2 <- data.frame(Value=c(40, 20, 60), Type=c("A", "B", "D")) 
df3 <- data.frame(Value=c(40, 20, 10, 60), Type=c("A", "B", "C", "D")) 
df3$Type <- factor(df3$Type, levels=c("D", "C", "B", "A"), ordered=TRUE) 

custom_plot(df1) 
custom_plot(df2) 
custom_plot(df3) 
+0

AH!したがって、scale_fill_manual()*は名前付きベクトルを扱うことができます!それはまさに私が探していたものです。 Kristofferに感謝します。 – Chris

2

あなたが好きな場合は、独自のカスタムスケールを定義することができます。あなたがscale_fill_manual

scale_fill_manual 
#> function (..., values) 
#> { 
#>  manual_scale("fill", values, ...) 
#> } 
#> <environment: namespace:ggplot2> 

のソースを見れば、それは実際には非常に簡単です:

library(ggplot2) 

scale_fill_chris <- function(...){ 
    ggplot2:::manual_scale('fill', 
          values = setNames(c('green', 'blue', 'red', 'orange'), 
              LETTERS[1:4]), 
          ...) 
} 

df1 <- data.frame(Value = c(40, 20, 10, 60), 
        Type = c("A", "B", "C", "D")) 

ggplot(df1, aes(x = Type, y = Value, fill = Type)) + 
    geom_col() + 
    scale_fill_chris() 

df2 <- data.frame(Value = c(40, 20, 60), 
        Type = c("A", "B", "D")) 

ggplot(df2, aes(x = Type, y = Value, fill = Type)) + 
    geom_col() + 
    scale_fill_chris() 

df3 <- data.frame(Value = c(40, 20, 10, 60), 
        Type = c("A", "B", "C", "D")) 
df3$Type <- factor(df3$Type, levels = c("D", "C", "B", "A"), ordered = TRUE) 

ggplot(df3, aes(x = Type, y = Value, fill = Type)) + 
    geom_col() + 
    scale_fill_chris() 

関連する問題