ggplot2堆积区域图表未填满年份 [英] ggplot2 stacked area chart not filling between years
问题描述
我的数据结构如下(这只是一个例子):
年份公司汽车
2011丰田609
2011 honda 710
2011 ford 77
2011 nissan 45
2011 chevy 11
2012丰田152
2012 honda 657
2012 ford 128
2012日产159
2012 chevy 322
2013丰田907
2013本田656
2013福特138
2013日产270
2013雪佛兰106
2014丰田336
2014本田957
2014 ford 204
2014日产219
2014雪佛兰282
我想制作一个堆积区域图表。如果一个数据集的格式与上面完全一样,则公式 ggplot(data,aes(x = year,y = cars,fill = company))+ geom_area()
很好的年份之间的区域,如下所示:
然而,对于另一个数据集格式完全相同的方式,并使用完全相同的ggplot代码生成,仅使用新数据源 ggplot(data2,aes(x = year,y = cars,fill =公司))+ geom_area()
,图表没有填写年份之间的区域并创建一个混乱,如下所示:
< a href =https://i.stack.imgur.com/uuWVi.png =nofollow noreferrer>
每年你都会注意到所有的点连接在一起。奇怪的差距只是在几年之间。
有人对这个错误的可能来源有任何建议吗?
您需要根据列 company
和 year
。下面的例子说明了这一点。
library(ggplot2)
library(dplyr)
data < - data.frame(years = rep(1991:2000,times = 10),
company = as.factor(rep(1:10,each = 10)),$ b $ )b $ cars = runif(n = 100,min = 500,max = 1000))
ggplot(data,aes(x = years,y = cars,fill = company))+
geom_area()
#随机订购数据
data2 < - data [sample(x = 1:100,size = 100,replace = F)]]
ggplot(data2,aes(x = years,y = cars,fill = company))+
geom_area()
#重新排列数据
data3< - arrange( data2,company,years)
ggplot(data3,aes(x = years,y = cars,fill = company))+
geom_area()
I have data structured as follows (this is merely an example):
year company cars
2011 toyota 609
2011 honda 710
2011 ford 77
2011 nissan 45
2011 chevy 11
2012 toyota 152
2012 honda 657
2012 ford 128
2012 nissan 159
2012 chevy 322
2013 toyota 907
2013 honda 656
2013 ford 138
2013 nissan 270
2013 chevy 106
2014 toyota 336
2014 honda 957
2014 ford 204
2014 nissan 219
2014 chevy 282
I want to make a stacked area chart. With one data set formatted exactly as above, the formula ggplot(data, aes(x=year,y=cars, fill=company)) + geom_area()
fills in the areas between the years nicely, like so:
However, with another data set formatted exactly the same way and generated using exactly the same ggplot code, only using the new data source, ggplot(data2, aes(x=year,y=cars, fill=company)) + geom_area()
, the chart does not fill in the area between the years and creates a mess, like so:
You'll notice at each year, all the points connect. The odd gaps are only between years.
Does anyone have any suggestions about the possible source of this error?
You need to order the data according to the column company
and year
. The following example illustrates this.
library("ggplot2")
library("dplyr")
data <- data.frame(years = rep(1991:2000, times = 10),
company = as.factor(rep(1:10, each = 10)),
cars = runif(n = 100, min = 500, max = 1000))
ggplot(data, aes(x = years, y = cars, fill = company)) +
geom_area()
# Randomly order data
data2 <- data[sample(x = 1:100, size = 100, replace = F), ]
ggplot(data2, aes(x = years, y = cars, fill = company)) +
geom_area()
# Reordering the data
data3 <- arrange(data2, company, years)
ggplot(data3, aes(x = years, y = cars, fill = company)) +
geom_area()
这篇关于ggplot2堆积区域图表未填满年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!