ggplot2:使用 geom_area() 函数

发布于 2024-08-11 08:17:30 字数 890 浏览 3 评论 0原文

我有一个数据框,显示每年的四个类别,以及它们各自占当年总数的份额。

> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2024-08-18 08:17:30

我刚刚遇到这个问题。看来这

theme_set(theme_bw(base_size=9))

会导致报告错误。但

base_size <- 9
theme_set(theme_bw(base_size=base_size))

有效。

我用谷歌搜索并在 learnr 博客

我不知道第一个例子不起作用?

I just had this problem. It seams that

theme_set(theme_bw(base_size=9))

results in the error reported. But

base_size <- 9
theme_set(theme_bw(base_size=base_size))

works.

I googled and found the example at the learnr blog

I dont know what the first example does not work though?

时光礼记 2024-08-18 08:17:30

问题出在我设置的一些主题参数中,所以一旦我开始构建一个可运行的示例以在此处重现,它就消失了。感谢您的帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文