2017-01-11 10 views
0

年の間に変数の値の範囲をプロットしたいと思います。 私はプロットポイントを使用して行っていますが、ポイントでは表示が困難です。だからバーを使いたいです。誰もそれを行う方法を知っていますか?Barplot:プロット値の範囲

プロットポイントを使用して、私が望むものについてのコード例を下に示します。

Degrees <- c(20, 19, 18, 20, 19, 18, 17, 10, 9, 8) 
Year <- c("85", "85", "85", "86", "86", "86", "86", "87", "87", "87") 
df <- data.frame (Degrees=Degrees, Year=Year) 
p <- ggplot(df, aes(Year, Degrees)) 
p + geom_point() 
+1

私はあなたの箱ひげ図(度〜年、データ=のDFを)望んでいないと仮定します。 – rafaelvalle

答えて

1

プログラムで、年間最小値と最大値を見つけると列に適切な名前を付ける:

year_min <- setNames(aggregate(df$Degrees, by = list(df$Year), min), c("Year", "Degrees")) 
year_max <- setNames(aggregate(df$Degrees, by = list(df$Year), max), c("Year", "Degrees")) 

は、データフレームにそれらを連結:

df_merge <- rbind(year_min, year_max) 

は次に、ボックスプロット

boxplot(Degrees~Year, data=df_merge) 
を行います

Boxplot with min and max values per year only

0

は私が

p <- ggplot(df, aes(Year, Degrees)) 
p + geom_boxplot() 

enter image description here

バープロット...あなたが代わりにbarplotを、指定した年の値の範囲を描写する、すなわち箱ひげ図、何かをしたいかもしれないと思います(値は年ごとに合計されます)

p <- ggplot(df, aes(Year, Degrees)) 
p + geom_bar(stat="identity") 

enter image description here

+0

OPは特にボックスプロットを要求しませんでした。 – rafaelvalle

+0

ありがとうございます。でも、私は@rafaelvalleの答えに似たものがほしいと思っています。 – Ruben

2

それともこのようなものでしょうか?

ggplot(df, aes(Year, Degrees)) + 
    stat_summary(fun.y = mean, fun.ymin = min, fun.ymax = max, col = 'red') 

enter image description here

+0

ありがとうございます。しかし、私は四角形が視覚化にもっと役立つと思います。 – Ruben