ggplot:子集使用管道传递数据的图层

tch*_*rty 5 r ggplot2 dplyr

我正在尝试对我ggplot通过管道传递数据的绘图层进行子集化。

下面是一个例子:

library(dplyr)
library(ggplot2)
library(scales)

set.seed(12345)
df_example = data_frame(Month = rep(seq.Date(as.Date("2015-01-01"),
                                             as.Date("2015-12-31"), by = "month"), 2),
                        Value = sample(seq.int(30, 150), size = 24, replace = TRUE),
                        Indicator = as.factor(rep(c(1, 2), each = 12)))

df_example %>% 
  group_by(Month) %>% 
  mutate(`Relative Value` = Value/sum(Value)) %>% 
  ungroup() %>% 
  ggplot(aes(x = Month, y = Value, fill = Indicator, group = Indicator)) + 
  geom_bar(position = "fill", stat = "identity") + 
  theme_bw()+ 
  scale_y_continuous(labels = percent_format()) + 
  geom_line(aes(x = Month, y = `Relative Value`))
Run Code Online (Sandbox Code Playgroud)

这给出:

在此处输入图片说明

我希望只出现其中一行,如果这样的事情在geom_line图层中起作用,我将能够做到这一点:

  geom_line(subset = .(Indicator == 1), aes(x = Month, y = `Relative Value`))
Run Code Online (Sandbox Code Playgroud)

编辑:

会话信息:

R 版本 3.2.1 (2015-06-18) 平台:x86_64-w64-mingw32/x64 (64-bit) 运行环境:Windows Server 2012 x64 (build 9200)

语言环境:2 LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C [5] LC_TIME=English_United States.1252

附加的基础包:2统计图形 grDevices utils
数据集方法基础

其他附加包: 2 scales_0.3.0 lubridate_1.3.3 ggplot2_1.0.1 lazyeval_0.1.10 dplyr_0.4.3 RSQLite_1.0.0
readr_0.2.2 [8] RJDBC_0.2-5 DBI_0.3.1 rJava_0.9-9

通过命名空间加载(且未附加): 2 Rcpp_0.12.2
knitr_1.11 magrittr_1.5 MASS_7.3-40 munsell_0.4.2lattice_0.20-31
[7] colorspace_1.2-6 R6_2.1.1 stringr_1.0.8.3r_1 tools_3.2.1 parallel_3.2.1 [13] grid_3.2.1
gtable_0.1.2 htmltools_0.2.6 yaml_2.1.13 assertthat_0.1
digest_0.6.8 [19] reshape2_1.4.1 memoise_0.2.1
memoise_0.2.1 rmarkdown_0.1.1.01 string zooing_1.371 string zooing_1.37 -12
[25] proto_0.3-10

mgi*_*nti 9

tl;dr:将数据作为函数传递到该层,该函数根据您的标准对绘图数据进行子集化。


根据ggplots 关于图层的文档,将数据传递给新图层时,您有 3 个选项:

  1. 如果为NULL(默认值),则数据继承自调用 ggplot() 中指定的绘图数据。
  2. data.frame或其他对象将覆盖绘图数据。所有对象都将被强化以生成数据框。请参阅 fortify() 将创建哪些变量。
  3. 函数会带一个参数,情节数据被调用。返回值必须是一个data.frame,并将用作图层数据。

前两个选项是最常用的选项,但是当通过 pyps 修改数据时,第三个选项非常适合我们的需求。

在您的示例中,添加data = function(x) subset(x,Indicator == 1)到 可以解决geom_line问题:

library(dplyr)
library(ggplot2)
library(scales)

set.seed(12345)
df_example = data_frame(Month = rep(seq.Date(as.Date("2015-01-01"),
                                             as.Date("2015-12-31"), by = "month"), 2),
                        Value = sample(seq.int(30, 150), size = 24, replace = TRUE),
                        Indicator = as.factor(rep(c(1, 2), each = 12)))

df_example %>% 
  group_by(Month) %>% 
  mutate(`Relative Value` = Value/sum(Value)) %>% 
  ungroup() %>% 
  ggplot(aes(x = Month, y = Value, fill = Indicator, group = Indicator)) + 
  geom_bar(position = "fill", stat = "identity") + 
  theme_bw()+ 
  scale_y_continuous(labels = percent_format()) + 
  geom_line(data = function(x) subset(x,Indicator == 1), aes(x = Month, y = `Relative Value`))
Run Code Online (Sandbox Code Playgroud)

这是结果图

  • 我怎么不知道有这个功能选项! (3认同)

Mik*_*ise 2

library(dplyr)
library(ggplot2)
library(scales)

set.seed(12345)
df_example = data_frame(Month = rep(seq.Date(as.Date("2015-01-01"),
                                             as.Date("2015-12-31"), by = "month"), 2),
                        Value = sample(seq.int(30, 150), size = 24, replace = TRUE),
                        Indicator = as.factor(rep(c(1, 2), each = 12)))

df_example %>% 
  group_by(Month) %>% 
  mutate(`Relative Value` = Value/sum(Value)) %>% 
  ungroup() %>% 
  ggplot(aes(x = Month, y = Value, fill = Indicator, group = Indicator)) + 
  geom_bar(position = "fill", stat = "identity") + 
  theme_bw()+ 
  scale_y_continuous(labels = percent_format()) + 
  geom_line(aes(x = Month, y = `Relative Value`,linetype=Indicator)) +
  scale_linetype_manual(values=c("1"="solid","2"="blank"))
Run Code Online (Sandbox Code Playgroud)

产量:

在此输入图像描述