2016-05-12 10 views
1

私はrとggplot2を使って積み上げ領域グラフを作成しようとしています。私は like thisのように見えますが、代わりにareas overlap and have holesとしてください。直近の月(ここでは2016-05)の中で最大の価値を持つ地域が最下位になるように、地域が積み重ねられていることを確認しようとしています。Rとggplot2に穴があるスタック領域グラフ

関連する投稿like this oneはデータに穴があいているように見えますが、ここで問題はないようです。あなたの助けのために事前に

sample.data <- structure(
    list(
    rank = structure(
     c(34L, 34L, 34L, 35L, 35L, 35L, 34L, 34L, 34L, 34L, 35L, 35L, 35L, 35L, 35L, 34L), 
     .Label = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"), 
     class = "factor"), 
    vendor = structure(
     c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L), 
     .Label = c("34", "35"), 
     class = "factor"), 
    year.month = c("2015-12", "2016-01", "2015-11", "2015-12", "2016-01", "2015-10", "2016-03", "2016-02", "2015-10", "2016-04", "2015-11", "2016-05", "2016-04", "2016-03", "2016-02", "2016-05"), 
    value = c(431616L, 272224L, 229288L, 195284L, 155168L, 154194L, 149784L, 137302L, 126612L, 117408L, 94141L, 56161L, 54606L, 53173L, 49898L, 45348L)), 
    .Names = c("rank", "vendor", "year.month", "value"), 
    row.names = c(6L, 8L, 4L, 5L, 7L, 1L, 12L, 10L, 2L, 14L, 3L, 15L, 13L, 11L, 9L, 16L), 
    class = "data.frame" 
) 

ggplot(data = sample.data, aes(x = year.month, y = value, group = vendor, color = vendor, reorder(-value), fill=vendor)) + 
    geom_area() 

ありがとう:

は、ここで問題を再現するサンプルコードです。

答えて

0

試してください:+ geom_area(位置= "かわす"、STAT = "アイデンティティ")

0

次の作品は:sample.data[order(sample.data$vendor),]

ggplot(data = sample.data[order(sample.data$vendor),], 
     aes(x = year.month, y = value, group = vendor, color = vendor, 
      reorder(-value), fill=vendor)) + geom_area() 

あなたは自分のデータを注文しなければなりませんでした。

あなたはグラフの順序を変更したい場合は、「relevel」に要因として格納されているベンダーの変数があります。

sample.data$vendor <- relevel(sample.data$vendor, ref="35") 

をここのように設定するかをベンダー把握するためにいくつかのコードがありますあなたの基準に基づいてベースレベル:

with(sample.data, sample.data[year.month=="2016-05", 
         "vendor"][which.max(sample.data[year.month=="2016-05", "value"])]) 
+0

お返事ありがとうございました!私はそれが本当に近いと思いますが、最近の月(この場合2016-05)で最大の価値を持つエリアが底にくるように、エリアが積み重ねられているようにしています。私はカップルの場所で "ベンダー"の代わりに "値"を試しましたが、それはうまくいかないようです。 – Matt

+0

あなたのベンダー変数は要素なので、これを達成するには 'relevel'を使う必要があります。上記の私のコードを参照してください。 – lmo

関連する問題