2017-01-18 22 views
3

aesfill引数を指定すると、円グラフの逆順になります。したがって、改行/ラベルは、もはや円の部分と一致しません。以下の例とその結果のプロットを参照してください。ggplot2 coord_polar fillを使用するときの順序を保持する

df = data.frame(Var1 = letters[1:5], Var2 = c(6, 31, 34, 66, 77))  
df$Var1 = factor(df$Var1, levels = df$Var1, ordered = T) 

# just fine, but no colors 
ggplot(df, aes(x = 1, 
       y = Var2)) + 
    geom_bar(width = 1, stat = "identity") + 
    coord_polar(theta = "y") + 
    scale_fill_manual(values = c("red","green","yellow","black","white"), 
        guide_legend(title = "My_Title")) + 
    scale_y_continuous(breaks = (cumsum(df$Var2) - 
           df$Var2/2), 
        labels = df$Var1) 

# reverse order appears 
ggplot(df, aes(x = 1, 
       y = Var2, 
       fill = Var1)) + 
    geom_bar(width = 1, stat = "identity") + 
    coord_polar(theta = "y") + 
    scale_fill_manual(values = c("red","green","yellow","black","white"), 
        guide_legend(title = "My_Title")) + 
    scale_y_continuous(breaks = (cumsum(df$Var2) - 
           df$Var2/2), 
        labels = df$Var1) 

just fine reverse order

+2

[2.2.0](https://blog.rstudio.org/2016/11/14/ggplot2-2-2-0/)のセクションのスタッキングバーのリリースノートを参照してください。 – Haboryme

答えて

2

スタッキングは逆係数順序(バージョン2.2.0あたり)で発生するので、我々は、元の順にスタックするために、次のコードを使用することができます。また

ggplot(df, aes(x = 1, 
       y = Var2, 
       fill = forcats::fct_rev(Var1))) + 
    geom_bar(width = 1, stat = "identity", col = 1) + 
    coord_polar(theta = "y") + 
    scale_y_continuous(breaks = (cumsum(df$Var2) - 
            df$Var2/2), 
         labels = df$Var1) 

geom_bar(stat = "identity")の代わりにgeom_colを使用することができます。

関連する問題