ggplot2:使用 geom_area() 函数
我有一个数据框,显示每年的四个类别,以及它们各自占当年总数的份额。
> head(df)
class year share
1 class1 1975 0.806
2 class2 1975 0.131
3 class3 1975 0.018
4 class4 1975 0.045
5 class1 1976 0.788
6 class2 1976 0.151
当我在没有指定 fill
的情况下运行 ggplot 时,我得到了一个统一的灰色框,正如预期的那样。
> ggplot(df, aes(x=year, y=share, group=class)) + geom_area() + scale_fill_brewer()
所以我尝试添加 fill=class
,但它不起作用。
> ggplot(df, aes(x=year, y=share, group=class, fill=class)) + geom_area() + scale_fill_brewer()
Error in inherits(x, "factor") : object "base_size" not found
In addition: Warning message:
In inherits(x, "factor") : restarting interrupted promise evaluation
>
我可以对 class
因子进行哪些操作才能使其与 scale_fill_brewer()
正常工作?显然,这个想法是根据其类别对图表的每个区域进行着色。
谢谢。
I have a data frame showing four classes for each year, along with their respective shares of the total for that year.
> head(df)
class year share
1 class1 1975 0.806
2 class2 1975 0.131
3 class3 1975 0.018
4 class4 1975 0.045
5 class1 1976 0.788
6 class2 1976 0.151
When I run ggplot
with no fill
specified, I get a uniform gray box, as expected.
> ggplot(df, aes(x=year, y=share, group=class)) + geom_area() + scale_fill_brewer()
So I try to add fill=class
, and it doesn't work.
> ggplot(df, aes(x=year, y=share, group=class, fill=class)) + geom_area() + scale_fill_brewer()
Error in inherits(x, "factor") : object "base_size" not found
In addition: Warning message:
In inherits(x, "factor") : restarting interrupted promise evaluation
>
What can I do to the class
factor to get it working properly with scale_fill_brewer()
? The idea, obviously, is to shade each area of the graph according to its class.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚遇到这个问题。看来这
会导致报告错误。但
有效。
我用谷歌搜索并在 learnr 博客
我不知道第一个例子不起作用?
I just had this problem. It seams that
results in the error reported. But
works.
I googled and found the example at the learnr blog
I dont know what the first example does not work though?
问题出在我设置的一些主题参数中,所以一旦我开始构建一个可运行的示例以在此处重现,它就消失了。感谢您的帮助。
The problem was in some theme parameters I'd set, so it went away once I started building a runnable example to reproduce here. Thanks for the help.