3

ggplot 縦棒グラフの色を変更しようとしています。グーグルで調べたところ、次のコードが機能すると思いました。

require(ggplot2)
require(RColorBrewer)
State <- c(rep("NSWTC",5), rep("TCV",5), rep("QTC",5), 
           rep("WATC",5), rep("SAFA",5), rep("Other",5))

Year  <- rep(c("11-12","12-13","13-14","14-15","15-16"),6)

##some random data
Funding.Programme <- abs(rnorm(30))

df <- data.frame(State, Year, Funding.Programme)

##this line makes the graph in the order you inputted it, rather than alphabetical
df$State <- factor(df$State, levels=unique(df$State))

##ugly coloured
bars <- ggplot(df) + 
            aes(x=Year ,y=Funding.Programme, fill=Year) + 
            geom_bar(stat='identity') + 
            facet_grid(facets=~State) + 
            scale_y_continuous('Gross Issuance Requirements')

##nicely coloured
blues <- brewer.pal(5, "Blues")
blues <- rev(blues)
##the following two graphs have the same colours
bars <- ggplot(df) + 
            aes(x=Year ,y=Funding.Programme, fill=Year) + 
            geom_bar(stat='identity') + 
            facet_grid(facets=~State) + 
            scale_y_continuous('Gross Issuance Requirements') + 
            scale_fill_brewer(blues)
bars
bars <- ggplot(df) + 
            aes(x=Year ,y=Funding.Programme, fill=Year) + 
            geom_bar(stat='identity') + 
            facet_grid(facets=~State) + 
            scale_y_continuous('Gross Issuance Requirements') + 
            scale_fill_brewer(blues.rev)
bars

##and this does not adjust the default colours
bars <- ggplot(df)+ 
            aes(x=Year,y=Funding.Programme, fill=Year) + 
            geom_bar(stat='identity') + 
            facet_grid(facets=~State) + 
            scale_y_continuous('Gross Issuance Requirements') + 
            scale_colour_manual(values = blues.rev)
bars

しかし、最後の方法は機能せず、生成された 2 番目と 3 番目のチャートは、オブジェクト内で色の順序が逆になっているにもかかわらず、同一です。

4

1 に答える 1

3

あなたが望むscale_fill_manual(values = blues)か、逆にblues.rev(サンプルコードで実際に作成しなかったので、タイプミスだと思います)。

デフォルト パレットの 1 つを名前で選択する場合にのみ使用scale_*_brewerします。それ以外の場合はscale_*_manual、このようなことに使用してください。

塗りつぶしの代わりに色を使用していたため、最後のものは機能しません。

最後に、キャリッジ リターンとタブです。これらを愛し、大切にし、使用してください。

于 2012-06-27T01:02:36.753 に答える