引用ggplot图层中的管道数据集以进行子设置 [英] Referencing piped dataset in ggplot layers for subsetting
本文介绍了引用ggplot图层中的管道数据集以进行子设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
试图找到一种方法来引用不同ggplot2 geom图层的数据集的不同部分,而不必先在全局环境中保存数据集。
Trying to find a way to reference different parts of the dataset for different ggplot2 geom layers without having to save the dataset first in the global environment.
Ex不起作用
read_excel("Book1.xlsx",sheet = "Sheet2") %>%
ggplot(aes(x,y)) +
geom_col() +
geom_point(data=subset($ID == "1"),colour="red")
以上似乎没有用,因为我没有以R可以识别的方式引用管道(magrittr)数据集。我已经搜索过,但是唯一能看到的解决方案是基于这样的解决方案:首先将数据集保存在全球环境中
Above does not seem to work since i am not referencing the piped(magrittr) dataset in a way that R can recognize it. I have searched but the only solutions i could see are based of the one where i first save the dataset in the global environment as such
先保存数据集的工作解决方案。
Ex working solution with saving the dataset first.
df <- read_excel("Book1.xlsx",sheet = "Sheet2")
ggplot(df,aes(x,y)) +
geom_col() +
geom_point(data=subset(df,ID == "1"),colour="red")
推荐答案
您可以尝试使用 dplyr
:
library(dplyr)
library(ggplot2)
read_excel("Book1.xlsx",sheet = "Sheet2") %>%
ggplot(aes(x, y)) +
geom_col() +
geom_point(data = . %>% filter(ID == "1"), colour = "red")
这篇关于引用ggplot图层中的管道数据集以进行子设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文