R:ggplot2:在两个类别中拆分y变量的值

时间:2018-01-03 15:17:31

标签: r ggplot2

我正在尝试根据y变量生成一个,其中包含两个geom_line。我的x轴是日期,我的y轴是从-5到5的情绪。对于每个日期,我得到了几个情感值。我想看看情绪如何在这些日子里发生变化。

我的数据看起来像这样(每一行都是不同的人): My Data:

这是我用来生成情节的代码:

ggplot(kernplot, aes(x=dates, y=Sentiment)) +
  geom_line()+
  scale_x_date(date_breaks = "1 day", date_labels = "%Y-%m-%d", limits = as.Date(c("2017-09-22","2017-10-10")) )+
  scale_y_continuous(breaks=seq(-5,5, by=1))+
  theme(axis.text.x = element_text(angle = 25, vjust = 1.0, hjust = 1.0))

以下是目前的情况: Sentimentplot

我已经尝试将变量拆分为两组,但我的Y轴标签不再正确。

我希望2行代表0到5以及0到-5的值。如何获得值的平均值以使线条更好看?因为目前我无法真实地说出情绪在这些日子里的变化。

1 个答案:

答案 0 :(得分:0)

尝试分解数据参数?


library(tidyverse)

kernplot <- tibble(dates = Sys.Date() + sample.int(15, size=100, replace = TRUE),
                   Sentiment = sample.int(10, size = 100, replace=TRUE)-5) %>% 
  group_by(dates) %>% 
  print()
#> # A tibble: 100 x 2
#> # Groups:   dates [15]
#>         dates Sentiment
#>        <date>     <dbl>
#>  1 2018-01-08        -2
#>  2 2018-01-14        -1
#>  3 2018-01-18        -4
#>  4 2018-01-12         2
#>  5 2018-01-06         2
#>  6 2018-01-07         2
#>  7 2018-01-14         1
#>  8 2018-01-16        -1
#>  9 2018-01-17        -3
#> 10 2018-01-04         3
#> # ... with 90 more rows
ggplot() +
  geom_smooth(data=filter(kernplot, Sentiment <= 0), mapping = aes(x=dates, y=Sentiment),se = FALSE) +
  geom_smooth(data=filter(kernplot, Sentiment >= 0), mapping = aes(x=dates, y=Sentiment),se = FALSE) +
  scale_x_date(date_breaks = "1 day", date_labels = "%Y-%m-%d")+
  scale_y_continuous(breaks=seq(-5,5, by=1)) +
  theme(axis.text.x = element_text(angle = 25, vjust = 1.0, hjust = 1.0))
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

devtools::session_info()
#> Session info --------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.3.2 (2016-10-31)
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2018-01-03
#> Packages ------------------------------------------------------------------
#>  package    * version    date       source                            
#>  assertthat   0.2.0      2017-04-11 cran (@0.2.0)                     
#>  backports    1.0.5      2017-01-18 CRAN (R 3.3.2)                    
#>  bindr        0.1        2016-11-13 cran (@0.1)                       
#>  bindrcpp   * 0.2        2017-06-17 CRAN (R 3.3.2)                    
#>  broom        0.4.2      2017-02-13 CRAN (R 3.3.2)                    
#>  cellranger   1.1.0      2016-07-27 CRAN (R 3.3.2)                    
#>  cli          1.0.0      2017-11-05 CRAN (R 3.3.2)                    
#>  colorspace   1.3-2      2016-12-14 CRAN (R 3.3.2)                    
#>  crayon       1.3.4      2017-09-16 CRAN (R 3.3.2)                    
#>  devtools     1.12.0     2016-12-05 CRAN (R 3.3.2)                    
#>  digest       0.6.12     2017-01-27 CRAN (R 3.3.2)                    
#>  dplyr      * 0.7.4      2017-09-28 CRAN (R 3.3.2)                    
#>  evaluate     0.10.1     2017-06-24 cran (@0.10.1)                    
#>  forcats    * 0.2.0      2017-01-23 CRAN (R 3.3.2)                    
#>  foreign      0.8-67     2016-09-13 CRAN (R 3.3.2)                    
#>  ggplot2    * 2.2.1.9000 2017-11-14 Github (tidyverse/ggplot2@41f154f)
#>  glue         1.2.0      2017-10-29 CRAN (R 3.3.2)                    
#>  gtable       0.2.0      2016-02-26 CRAN (R 3.3.2)                    
#>  haven        1.1.0      2017-07-09 CRAN (R 3.3.2)                    
#>  hms          0.3        2016-11-22 CRAN (R 3.3.2)                    
#>  htmltools    0.3.6      2017-04-28 cran (@0.3.6)                     
#>  httr         1.3.1      2017-08-20 CRAN (R 3.3.2)                    
#>  jsonlite     1.5        2017-06-01 cran (@1.5)                       
#>  knitr        1.17       2017-08-10 cran (@1.17)                      
#>  lattice      0.20-35    2017-03-25 CRAN (R 3.3.2)                    
#>  lazyeval     0.2.1      2017-10-29 cran (@0.2.1)                     
#>  lubridate    1.7.1      2017-11-03 CRAN (R 3.3.2)                    
#>  magrittr     1.5        2014-11-22 CRAN (R 3.3.2)                    
#>  memoise      1.0.0      2016-01-29 CRAN (R 3.3.2)                    
#>  mnormt       1.5-5      2016-10-15 CRAN (R 3.3.2)                    
#>  modelr       0.1.1      2017-07-24 CRAN (R 3.3.2)                    
#>  munsell      0.4.3      2016-02-13 CRAN (R 3.3.2)                    
#>  nlme         3.1-131    2017-02-06 CRAN (R 3.3.2)                    
#>  pkgconfig    2.0.1      2017-03-21 cran (@2.0.1)                     
#>  plyr         1.8.4      2016-06-08 CRAN (R 3.3.2)                    
#>  psych        1.6.12     2017-01-08 CRAN (R 3.3.2)                    
#>  purrr      * 0.2.4      2017-10-18 CRAN (R 3.3.2)                    
#>  R6           2.2.2      2017-06-17 cran (@2.2.2)                     
#>  Rcpp         0.12.13    2017-09-28 cran (@0.12.13)                   
#>  readr      * 1.1.1      2017-05-16 CRAN (R 3.3.2)                    
#>  readxl       1.0.0      2017-04-18 CRAN (R 3.3.2)                    
#>  reshape2     1.4.2      2016-10-22 CRAN (R 3.3.2)                    
#>  rlang        0.1.4      2017-11-05 CRAN (R 3.3.2)                    
#>  rmarkdown    1.6        2017-06-15 cran (@1.6)                       
#>  rprojroot    1.2        2017-01-16 CRAN (R 3.3.2)                    
#>  rstudioapi   0.7        2017-09-07 cran (@0.7)                       
#>  rvest        0.3.2      2016-06-17 CRAN (R 3.3.2)                    
#>  scales       0.5.0.9000 2017-10-09 Github (hadley/scales@d767915)    
#>  stringi      1.1.5      2017-04-07 cran (@1.1.5)                     
#>  stringr    * 1.2.0      2017-02-18 CRAN (R 3.3.2)                    
#>  tibble     * 1.3.4      2017-08-22 cran (@1.3.4)                     
#>  tidyr      * 0.7.2      2017-10-16 CRAN (R 3.3.2)                    
#>  tidyverse  * 1.2.1      2017-11-14 CRAN (R 3.3.2)                    
#>  withr        2.1.0.9000 2017-11-14 Github (jimhester/withr@a9ebeb3)  
#>  xml2         1.1.1      2017-01-24 CRAN (R 3.3.2)                    
#>  yaml         2.1.14     2016-11-12 CRAN (R 3.3.2)