0

I have merged two series to plot with ggplot2 but the second series plots as an area rather than a line. My input is a tibble which suggests something about the data. I have merged to xts series into a tibble, when I plot it, the first series shows as a line and the second shows as an area. I cannot understand why. The two series are similar in magnitude. The top of the red area is about right for the second series. Here is summary of the input data frame

> summary(us_cdn_xts)
     Index               us10y2y          cdn10y2y      
 Min.   :2004-01-01   Min.   :-0.190   Min.   :-0.1400  
 1st Qu.:2007-09-13   1st Qu.: 0.770   1st Qu.: 0.0000  
 Median :2011-05-26   Median : 1.460   Median : 0.6500  
 Mean   :2011-05-26   Mean   : 1.401   Mean   : 0.6949  
 3rd Qu.:2015-02-05   3rd Qu.: 2.120   3rd Qu.: 1.2200  
 Max.   :2018-10-18   Max.   : 2.910   Max.   : 2.3200  
                      NA's   :1700     NA's   :159      
> 

Code is below

> plot_data<-tidy(us_cdn_xts)
> print(plot_data)
# A tibble: 10,810 x 3
   index      series  value
   <date>     <chr>   <dbl>
 1 2004-01-01 us10y2y NA   
 2 2004-01-02 us10y2y  2.44
 3 2004-01-03 us10y2y NA   
 4 2004-01-04 us10y2y NA   
 5 2004-01-05 us10y2y  2.46
 6 2004-01-06 us10y2y  2.45
 7 2004-01-07 us10y2y  2.43
 8 2004-01-08 us10y2y  2.42
 9 2004-01-09 us10y2y  2.43
10 2004-01-10 us10y2y NA   
# ... with 10,800 more rows
> library(ggthemes)
> term_plot<-ggplot(plot_data,aes(x=index,y=value,colour=series))+
+ geom_line()
> ggsave(term_plot,file="term_plot.png")
Saving 7 x 7 in image
Warning message:
Removed 3 rows containing missing values (geom_path). 
> 

plot

r2evans
  • 141,215
  • 6
  • 77
  • 149
jciconsult
  • 31
  • 3
  • What does being a `tibble` suggest about the data? – r2evans Oct 19 '18 at 21:51
  • Just guessing since we can't see your data: take a look at a tight band of `cdb10y2y`; if you see same-time (or very-close-times) that oscillate between 0 and non-zero, then what you may be seeing is very tightly compacted lines, effectively creating a polygon of sorts. – r2evans Oct 19 '18 at 22:00
  • Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Oct 19 '18 at 22:17

2 Answers2

0

I think you may just need to add a group aesthetic if you use a data table or frame.

Here's an example that ran for me:

index  <- c("10/17/2018","10/18/2018","10/19/2018","10/20/2018","10/17/2018","10/18/2018","10/19/2018","10/20/2018")
series <- c("a","a","a","a","b","b","b","b")
values <- c(1,2,3,4,4,3,2,1)
df <- data.frame(index, series, values)

g <- ggplot()
g <- g + geom_line(data= df, aes(x=index, y=values, colour= series, group= as.factor(series)))
g

Hope that helps!

The resulting graph - g

Community
  • 1
  • 1
0

I was trying to figure out how to post data with my question when I figured out my answer. The problem with the second series is that it has observations with absolute zero. I would appreciate a comment about what that causes the strange plots. I could find no documentation for that behaviour. But replacing the zeros with NA gives me a good plot. good plot after replacing 0's with NA

jciconsult
  • 31
  • 3