使用R和ggplot2的堆叠面积图有孔
我正在尝试使用r和ggplot2创建堆叠面积图。我想看看 ,但实际上是。我正在努力确保这些区域被堆叠,以便最近一个月(本例中为2016-05年)价值最大的区域位于底部 相关帖子的数据似乎有漏洞,这似乎不是问题所在 下面是重新创建问题的示例代码:使用R和ggplot2的堆叠面积图有孔,r,ggplot2,R,Ggplot2,我正在尝试使用r和ggplot2创建堆叠面积图。我想看看 ,但实际上是。我正在努力确保这些区域被堆叠,以便最近一个月(本例中为2016-05年)价值最大的区域位于底部 相关帖子的数据似乎有漏洞,这似乎不是问题所在 下面是重新创建问题的示例代码: sample.data <- structure( list( rank = structure( c(34L, 34L, 34L, 35L, 35L, 35L, 34L, 34L, 34L, 34L, 35L, 35L,
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()
sample.dataTry:+geom\u区域(position=“dodge”,stat=“identity”)Try:+geom\u区域(position=“dodge”,stat=“identity”)<
ggplot(data = sample.data[order(sample.data$vendor),],
aes(x = year.month, y = value, group = vendor, color = vendor,
reorder(-value), fill=vendor)) + geom_area()
您只需订购数据:sample.data[订购(sample.data$vendor),]
如果要更改图形的顺序,必须“重新设置”供应商变量,该变量存储为一个因子:
sample.data$vendor <- relevel(sample.data$vendor, ref="35")
以下工作:
ggplot(data = sample.data[order(sample.data$vendor),],
aes(x = year.month, y = value, group = vendor, color = vendor,
reorder(-value), fill=vendor)) + geom_area()
您只需订购数据:sample.data[订购(sample.data$vendor),]
如果要更改图形的顺序,必须“重新设置”供应商变量,该变量存储为一个因子:
sample.data$vendor <- relevel(sample.data$vendor, ref="35")
谢谢你的快速回复!我认为这真的很接近,但我正试图确保这些区域被堆叠起来,以便最近一个月(本例中为2016-05年)价值最大的区域位于底部。我试着在几个地方用“value”代替“vendor”,但这似乎不起作用。因为您的vendor变量是一个因子,所以需要使用relevel
来实现这一点。请参阅上面的代码。感谢您的快速响应!我认为这真的很接近,但我正试图确保这些区域被堆叠起来,以便最近一个月(本例中为2016-05年)价值最大的区域位于底部。我试着在几个地方用“value”代替“vendor”,但这似乎不起作用。因为您的vendor变量是一个因子,所以需要使用relevel
来实现这一点。请参阅上面的代码。