2017-06-11 13 views
0

次のデータの棒グラフをプロットします。次の棒グラフをプロットするにはどうすればよいですか?

year fips Emissions 
1 1999 06037 68.4060000 
2 2002 06037 78.0598486  
3 2005 06037 85.7657985 
4 2008 06037 85.1871200 
5 1999 24510 0.5600000 
6 2002 24510 10.5183944 
7 2005 24510 10.2240684 
8 2008 24510 0.4772056 

ここfipsyearが要因です。

私は、これは以下のグラフ生成ggplot2で

g <- ggplot(df, aes(year, Emissions, fill=fips)) 
g + 
    geom_bar(stat = 'identity', position = position_dodge()) + 
    labs(title = "Comparative pollution levels in San Fransisco and Baltimore", 
     x = "Year", xlab = unique(df$year)) + 
    scale_color_manual(labels = c("San Fransisco", "Baltimore"), values = c("#999999", "#E69F00")) + 
    theme_bw() + 
    guides(color = guide_legend("Counties")) 

を次のコードを試してみました:

enter image description here

を私はまた、コードを追加するにもかかわらず、凡例の名前を変更することはできませんよそれのための。

ベースプロットシステムを使用してプロットするにはどうすればよいですか?次のコードを使ってggplot2を使ってプロットすることができました。

+0

正しくあなたの質問をフォーマットしてください。 –

+0

申し訳ありませんが、私はプラットフォームに新しいです、また、私はインラインイメージを追加する特権を持っていませんでした。 –

答えて

1

aesでは、fill argを使用しました。したがって、fillの動作を編集するには、scale_color_manualの代わりにscale_fill_manualを使用します(これはcolorに渡されたものの動作をaesに編集します)。

guidesは、凡例のタイトルを編集するためのコードを使用していることを前提としています。以下のコードではをscale_fill_manualに編集しています。

library(ggplot2) 
df <- read.table(text=" year fips Emissions 
         1 1999 06037 68.4060000 
         2 2002 06037 78.0598486  
         3 2005 06037 85.7657985 
         4 2008 06037 85.1871200 
         5 1999 24510 0.5600000 
         6 2002 24510 10.5183944 
         7 2005 24510 10.2240684 
         8 2008 24510 0.4772056", header=T) 
df$year <- factor(df$year) 
df$fips <- factor(df$fips) 

g <- ggplot(df, aes(year, Emissions, fill=fips)) 
g + 
    geom_bar(stat = 'identity', position = position_dodge()) + 
    labs(title = "Comparative pollution levels in San Fransisco and Baltimore", 
     x = "Year", xlab = unique(df$year)) + 
    scale_fill_manual(name = "Counties", 
        labels = c("San Fransisco", "Baltimore"), 
        values = c("#999999", "#E69F00")) + 
    theme_bw() 

enter image description here

関連する問題