我正在嘗試使用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()
在此先感謝您的幫助。
感謝您的快速響應!我認爲這非常接近,但我試圖確保這些區域是堆疊的,以便在最近一個月(本例中爲2016-05)具有最大價值的區域位於底部。我嘗試用幾個地方的「供應商」代替「價值」,但似乎並不奏效。 – Matt
由於您的供應商變量是一個因素,因此您需要使用''repvel'來實現此目的。看到我上面的代碼。 – lmo