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

时间:2015-12-26 06:13:26

标签: 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`))

这给出了:

enter image description here

我希望只显示其中一行,如果这样的内容在geom_line图层中有效,我就能做到这一点:

  geom_line(subset = .(Indicator == 1), aes(x = Month, y = `Relative Value`))

编辑:

会话信息:

  

R version 3.2.1(2015-06-18)平台:x86_64-w64-mingw32 / x64(64位)运行于:Windows Server 2012 x64   (建立9200)

     

locale: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 stats graphics 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-7

     

通过命名空间加载(而不是附加):2 Rcpp_0.12.2
  knitr_1.11 magrittr_1.5 MASS_7.3-40 munsell_0.4.2
  lattice_0.20-31 [7] colorspace_1.2-6 R6_2.1.1 stringr_1.0.0   plyr_1.8.3 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
  rmarkdown_0.8.1 labeling_0.3 stringi_1.0-1 zoo_1.7-12
  [25] proto_0.3-10

3 个答案:

答案 0 :(得分: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"))

的产率:

enter image description here

答案 1 :(得分:2)

tl; dr :将数据作为函数传递给该图层,该函数可根据您的条件对绘图数据进行子集化。


根据ggplots documentation on layers,将数据传递到新层时,您有3个选择:

  
      
  1. 如果默认值为 NULL ,则数据是从ggplot()调用中指定的绘图数据继承而来的。
  2.   
  3. 数据框或其他对象将覆盖绘图数据。将强化所有对象以产生数据框。参见fortify()   将创建哪些变量。
  4.   
  5. 一个函数将使用单个参数(绘图数据)进行调用。返回值必须是data.frame,并将用作   图层数据。
  6.   

前两个选项是最常用的选项,但是当通过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`))

This is the resulting plot

答案 2 :(得分:0)

您可能会受益于stat_subset(),这是我为个人使用而制作的数据metRhttps://eliocamp.github.io/metR/articles/Visualization-tools.html#stat_subset

它有一种称为subset的美学,它采用逻辑表达式并相应地对数据进行子集化。


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()) + 
   metR::stat_subset(aes(x = Month, y = `Relative Value`, subset = Indicator == 1), 
               geom = "line")