【发布时间】:2021-11-02 22:06:31
【问题描述】:
我正在学习 R 和 ggplot2,但真正让我困惑的一件事是 aes 函数可用的参数。
我精通 Python 和 Java 等编程语言。在这样的编程语言中,您定义了一个函数,并且它的参数也是预定义的,并且您期望一个函数可以采用如此多的参数。
但是这里使用 aes 函数似乎很不一样,除了它的 'x' 和 'y' 参数。例如:
ggplot(forestarea, aes(income))+geom_bar(aes(fill=region))+ labs(x="Regions", y="Number of countries",
title="Number of countries by income level from each region in the world",
caption="The WDI Forest Area Indicator")
在上面的代码中,在第二个aes函数中,'fill'参数似乎与'geom_bar'函数相关联。真的是geom_bar的参数吗?
然后:
ggplot(forestarea, aes(factor(1), fill= income))+geom_bar()+ coord_polar(theta="y")+ theme(axis.line = element_blank(), panel.background = element_blank()) + theme(axis.text = element_blank()) + theme(axis.ticks = element_blank()) + labs(x=NULL, y=NULL, fill="Income level",
title="Proportion of countries by income level",
caption="WDI Forest Area Indicator")
这段代码创建了一个饼图,但是你可以看到'fill'参数在geom_bar函数之外的aes函数内部,我很困惑。是不是aes的参数?
然后:
ggplot(land_and_agrpc, aes(area = AG.LND.FRST.K2, fill = AG.LND.AGRI.ZS, label=country)) +
geom_treemap() + geom_treemap_text() +
labs(title="Countries by land area",
fill="% of agriculture land",
caption="WDI country land area and forest land percentage datasets")
此代码用于创建树形图,您可以看到 aes 函数采用 'area' 参数,树形图的文档中对此进行了说明:https://cran.r-project.org/web/packages/treemapify/vignettes/introduction-to-treemapify.html。我更加困惑了。
那么,我如何解释 aes 函数的参数,在哪里使用它们(在 'ggplot' 或 'geom_XXX' 函数内)?
【问题讨论】:
-
每个 geom 的文档都列出了一个“美学”部分,概述了该 geom 理解的美学。也许这会有所帮助?文档的该部分还指向
vignette("ggplot2-specs"),以获取有关美学规格的更多详细信息。另请注意,在ggplot()层内使用aes()指定美学全局,这意味着它们会传递到图中的所有后续几何层。在 geom 中的aes()中指定美学意味着您只为该特定层指定美学。 -
谢谢,非常感谢,这帮助我理解了函数