我正在尝试使用ggplot2绘制时间序列数据。具体来说,我正在尝试生成一个折线图,其中包含五年绘制的三组数据(年度数据点)。我可以毫无问题地做到。见这里
我的问题出现在我尝试应用颜色时,在这种情况下使用我首选的方法:scale_fill_brewer()。当我试图绘制时,如下:
> p + geom_line() + scale_fill_brewer(palette = "Set1")
不幸的是,这会产生以下错误:
In brewer.pal(n, pal) :
minimal value for n is 3, returning requested palette with 3 different levels
这令人困惑a)因为我在这里至少有三个组,b)因为我可以成功地将scale_fill_brewer()应用于条形图。
任何人scale_fill_brewer()都足以理解我做错了什么?
更新:根据请求,这是我生成P的方式。
> osr.m
Date Type Revenue
1 5/31/07 New.software.licenses 5882
2 5/31/08 New.software.licenses 7515
3 5/31/09 New.software.licenses 7123
4 5/31/10 New.software.licenses 7533
5 5/31/11 New.software.licenses 9235
6 5/31/12 New.software.licenses 9906
7 5/31/07 Software.license.updates.and.product.support 8329
8 5/31/08 Software.license.updates.and.product.support 10328
9 5/31/09 Software.license.updates.and.product.support 11754
10 5/31/10 Software.license.updates.and.product.support 13092
11 5/31/11 Software.license.updates.and.product.support 14796
12 5/31/12 Software.license.updates.and.product.support 16210
13 5/31/07 Software.revenues 14211
14 5/31/08 Software.revenues 17843
15 5/31/09 Software.revenues 18877
16 5/31/10 Software.revenues 20625
17 5/31/11 Software.revenues 24031
18 5/31/12 Software.revenues 26116
p <- ggplot(osr.m, aes(x=Date, y=Revenue, group=Type))
答案 0 :(得分:2)
假设您真的希望使用fill
而不是colour
,这就是我要做的事情:
dat <- read.table(text = "Date Type Revenue
1 5/31/07 New.software.licenses 5882
2 5/31/08 New.software.licenses 7515
3 5/31/09 New.software.licenses 7123
4 5/31/10 New.software.licenses 7533
5 5/31/11 New.software.licenses 9235
6 5/31/12 New.software.licenses 9906
7 5/31/07 Software.license.updates.and.product.support 8329
8 5/31/08 Software.license.updates.and.product.support 10328
9 5/31/09 Software.license.updates.and.product.support 11754
10 5/31/10 Software.license.updates.and.product.support 13092
11 5/31/11 Software.license.updates.and.product.support 14796
12 5/31/12 Software.license.updates.and.product.support 16210
13 5/31/07 Software.revenues 14211
14 5/31/08 Software.revenues 17843
15 5/31/09 Software.revenues 18877
16 5/31/10 Software.revenues 20625
17 5/31/11 Software.revenues 24031
18 5/31/12 Software.revenues 26116",sep = "",header = TRUE)
dat$Date <- as.Date(dat$Date,"%m/%d/%y")
ggplot(dat,aes(x = Date,y = Revenue)) +
geom_area(aes(group = Type,fill = Type)) +
scale_fill_brewer(palette = "Set1") +
opts(legend.position = "none")