2017-08-17 7 views
0

これは簡単なことだと思っていました。変数をグルーピングしてggplotのヒストグラムの面を塗りつぶす

私はファセットのヒストグラムのコードを持っています。彼らは標準的な色のテーマには問題はありませんが、色々なグループのドットを持つ単一の散布図ではうまく動作します。

そして、どこに赤が入っていますか?

ここデータ:参考https://pastebin.com/0p7SP005

library(ggplot2) 
library(ggthemes) 

ggplot(data = point_list, aes(x = lifetime, 
           y = ..density..)) + 
    geom_histogram() + 
    aes(fill = as.factor(cluster), 
     col = "black") + 
    scale_x_continuous(expand = c(0,0)) + 
    scale_y_continuous(expand = c(0,0)) + 
    coord_cartesian(xlim = c(-2.6,50), 
        ylim = c(0,0.16)) + 
    theme_tufte(base_size = 11, base_family = "Helvetica") + 
    theme(axis.text = element_text(color = "black"), 
      panel.border = element_rect(colour = "black", fill=NA, size=0.7), 
      legend.position = "none") + 
    facet_wrap(~cluster, scales = "free",) + 
    scale_color_brewer(palette = "Set1") 

enter image description here

、セット1は次のようになります。あなたが黒に赤を変更したい場合だけにあなたのgeom_histogram行を変更 enter image description here

+0

'aes'ではcolではなく' color' – loki

+2

あなたの 'fill'と' facet_wrap'変数は同じでなければなりません。 'cluster'と' as.factor(cluster) 'は異なるかもしれません。また、最後の行では、おそらく 'scale_fill_brewer'が必要です。 –

+2

ああ、それは私を逃れた 'scale_fill_brewer'だ!突然すべてが意味をなさない。ルーキーミス。 –

答えて

3

バーの輪郭が赤である理由を説明します。他のものはすべて既にカバーされているからです。

あなたのコードでは、審美的なマッピング機能の内側にcol = "black"がありました。つまり、アウトラインに使用されている色が変数にマップされていました。この場合の「黒」は1レベルの要素として解釈されます。コードにはscale_color_brewer(palette = "Set1")も含まれているため、結果の色はSet1パレットの最初の色、つまり明るい赤です。

(実際の言葉は問題ではありませんでした;。あなたはcol = "white"またはそのような何かを持っていた場合、それはまったく違いはありませんでしょう)

あなたが別のパレットを選択した場合は、アウトラインの色があまりにも違うだろう。たとえば:

ggplot(data = point_list, aes(x = lifetime, 
           y = ..density..)) + 
    geom_histogram(size = 3) + # thicker outline to make the color more obvious 
    aes(fill = as.factor(cluster), 
     col = "black") + 
    scale_x_continuous(expand = c(0,0)) + 
    scale_y_continuous(expand = c(0,0)) + 
    coord_cartesian(xlim = c(-2.6,50), 
        ylim = c(0,0.16)) + 
    # theme_tufte(base_size = 11, base_family = "Helvetica") + #commenting this out since I don't have this theme 
    theme(axis.text = element_text(color = "black"), 
     panel.border = element_rect(colour = "black", fill=NA, size=0.7), 
     legend.position = "none") + 
    facet_wrap(~cluster, scales = "free",) + 
    scale_color_brewer(palette = "Set2") 

example

SET2パレットから最初の色は、私たちがここで見るものである淡っぽい緑(#66c2a5)、です。

黒いアウトラインを得るには、上記のlokiのanswerに従って、geom_histogramに色を指定して、今度はaes()の外側にしてください。 :)

+0

これはどのように動作するのですか。感謝します、ありがとう! –

2

geom_histogram(color = "black") 

関連する問題