是``总结()
大家好,这里是一个示例代码
library(palmerpenguins)
mypenguins = penguins %>% drop_na()
penguins_df <- mypenguins %>%
group_by(species) %>%
summarize(mean_g = mean(bill_length_mm))
penguins_df %>%
ggplot(aes(x = mean_g, y = species)) +
geom_col(width = 0.5) +
theme(
axis.text.y = element_text(
color = if_else(penguins_df$mean_g > 48, "red", "black"),
face = if_else(penguins_df$mean_g > 48, "bold", "plain"),
size = 20
)
)
,但是当我想整合两个部分“汇总”和“ ggplot”时,
penguins_df <- mypenguins %>%
group_by(species) %>%
summarize(mean_g = mean(bill_length_mm)) %>%
ggplot(aes(x = mean_g, y = species)) +
geom_col(width = 0.5) +
theme(
axis.text.y = element_text(
color = if_else(mean_g > 48, "red", "black"),
face = if_else(mean_g > 48, "bold", "plain"),
size = 20
)
)
我被警告了:“ nare_g”找不到,但是我确实定义了“ sane_g”,这使我感到非常困惑。 有人会介意提供一些建议吗?谢谢。
Hello everyone Here is one example code
library(palmerpenguins)
mypenguins = penguins %>% drop_na()
penguins_df <- mypenguins %>%
group_by(species) %>%
summarize(mean_g = mean(bill_length_mm))
penguins_df %>%
ggplot(aes(x = mean_g, y = species)) +
geom_col(width = 0.5) +
theme(
axis.text.y = element_text(
color = if_else(penguins_df$mean_g > 48, "red", "black"),
face = if_else(penguins_df$mean_g > 48, "bold", "plain"),
size = 20
)
)
However when I want to integrate two parts "summarize" and "ggplot" like this
penguins_df <- mypenguins %>%
group_by(species) %>%
summarize(mean_g = mean(bill_length_mm)) %>%
ggplot(aes(x = mean_g, y = species)) +
geom_col(width = 0.5) +
theme(
axis.text.y = element_text(
color = if_else(mean_g > 48, "red", "black"),
face = if_else(mean_g > 48, "bold", "plain"),
size = 20
)
)
I was warned: "mean_g" not found, but I do define "mean_g" before, which confused me a lot.
Would someone mind offering some advice kindly? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们可以使用卷曲牙套的围绕GGPLOT表达式的“技巧”。唯一的缺点是我们需要用点明确指的是管道对象。
这是代码:
We can use the "trick" of surrounding the ggplot expression with curly braces. The only downside is that we'll need to explicitly refer to the piped object with a dot.
Here's the code:
评论太久了。
用
ggplot(AES(...))定义的美学
,仅适用于GEOM
s,而不是theme
。这就是为什么这不起作用。因此,例如,您可以编写:
这将起作用,因为
mean_g
是一种美学,因此ggplot
在data.frame (它查找
Mean_g
列)。主题(...)
函数不尊重美学,因此它在调用环境中查找mean_g
,并且由于没有这样的对象而失败。Too long for a comment.
The aesthetics defined with
ggplot(aes(...))
, apply togeom
s only, not to thetheme
. That's why this doesn't work.So for instance, you can write:
This will work because
mean_g
is an aesthetic, soggplot
looks for it in the context of thedata.frame
(it looks for themean_g
column). Thetheme(...)
function does not respect aesthetics, so it looks formean_g
in the calling environment, and fails because there is not such object.