ggplot:子集使用管道传递数据的图层 [英] ggplot: Subset a layer where data is passed using a pipe

查看:15
本文介绍了ggplot:子集使用管道传递数据的图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是一个例子:

库(dplyr)图书馆(ggplot2)图书馆(秤)set.seed(12345)df_example = data_frame(Month = rep(seq.Date(as.Date("2015-01-01"),as.Date("2015-12-31"), by = "月"), 2),值 = 样本(seq.int(30, 150),大小 = 24,替换 = TRUE),指标 = as.factor(rep(c(1, 2), each = 12)))df_example %>%group_by(月)%>%变异(`相对值`=值/总和(值))%>%取消分组()%>%ggplot(aes(x = Month, y = Value, fill = Indicator, group = Indicator)) +geom_bar(position = "fill", stat = "identity") +主题_bw()+scale_y_continuous(labels = percent_format()) +geom_line(aes(x = Month, y = `相对值`))

这给出:

我希望只出现其中一行,如果这样的操作在 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-bit) 运行环境:Windows Server 2012 x64(构建 9200)

语言环境:

I am trying to subset a layer of a plot where I am passing the data to ggplot through a pipe.

Here is an example:

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`))

This gives:

I would like only one of those lines to appear, which I would be able to do if something like this worked in the geom_line layer:

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

Edit:

Session info:

R version 3.2.1 (2015-06-18) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows Server 2012 x64 (build 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

attached base packages: 2 stats graphics grDevices utils
datasets methods base

other attached packages: 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

loaded via a namespace (and not attached): 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

解决方案

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"))

yields:

这篇关于ggplot:子集使用管道传递数据的图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆