Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

r - Include NA values as empty values in a facet plot

In order to show the development of a time series and its respective growth rate, I opted for a facet plot, since plots with two y-axis aren't an option. The issue now is that although the y-axis of the two plots are perfectly aligned, the tick marks on the x-axis are not, since the automatically occuring first NA value of the growth rate, shifts these values to the left and thus the plots are off by a month. I would like to know how it would be possible to include the first NA value in the plot of the growth rate as an empty column, to serve as a placeholder.

library(data.table)
library(ggplot2)
library(cowplot)
library(lubridate)
library(tidyverse)
dt <- structure(list(DATUM = structure(c(18321, 18351, 18382, 18412, 
                                         18443, 18473, 18504, 18535, 18565), tzone = "UTC", tclass = "Date", class = "Date"), 
                     value = c(70.696, 75.713, 75.713, 75.713, 75.213, 75.213, 
                               75.213, 46.025, 46.025)), row.names = c(NA, -9L), class = c("data.table", 
                                                                                           "data.frame"))

p.timeseries.plot <- dt %>% 
  melt(data = ., id.vars = c("DATUM"), measure.vars = c( "value")) %>%
  ggplot(data = .) +
  geom_line(aes(x = DATUM, y = value), color="steelblue", size =1.2) + 
  geom_point(aes(x = DATUM, y = value), size = 2) +
  theme_light() +
  scale_x_date(date_labels = "%Y-%m", date_breaks = "2 months") +
  xlab("") +
  ylab("")


p.growth.rate <- dt[
                                     , .(DATUM
                                         , growth.rate = round(log(value) - log(shift(value, n =1L, type="lag",fill = NA)),4)
                                     )] %>%
  melt(data = ., id.vars = c("DATUM"), measure.vars = c( "growth.rate")) %>%
  ggplot(data = .) +
  geom_col(aes(x = DATUM, y = value, fill = value >= 0)) + 
  scale_fill_manual(guide = FALSE, breaks = c(TRUE, FALSE), values=c("green", "red")) +
  theme_light() +
  scale_x_date(date_labels = "%Y-%m", date_breaks = "2 months") +
  xlab("") +
  ylab("")

cowplot::plot_grid(p.timeseries.plot, p.growth.rate, align = "v", ncol = 1, rel_heights = c(0.65, 0.35))

The output of the plot looks like this:

Current plot output

question from:https://stackoverflow.com/questions/65560293/include-na-values-as-empty-values-in-a-facet-plot

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One option is to use factors and prevent level dropping.

Another option (which I am showing) is to simply replace your NA with 0.

Disadvantage of both is that your x labelling is not so convenient anymore. (I offer one solution for this labelling). But clearly, user teunbrand had the better idea with setting the limits within scale_x_date.

I also modified your code a bit, and also I am using patchwork

library(patchwork)
library(lubridate)
library(tidyverse)

foodf <- structure(list(DATUM = structure(c(18321, 18351, 18382, 18412, 18443, 18473, 18504, 18535, 18565), tzone = "UTC", tclass = "Date", class = "Date"), value = c(70.696, 75.713, 75.713, 75.713, 75.213, 75.213, 75.213, 46.025, 46.025)), row.names = c(NA, -9L), class = c("data.frame"))

foodf <- 
  foodf %>%
  mutate(date = format(DATUM, "%Y-%m"),
         diffval = c(0, diff(value)))

labeldate <- foodf$date
labeldate[c(T, F)] <- ""

p1 <-
  ggplot(foodf, aes(x = date, y = value)) +
  geom_line(aes(group= 1), color = "steelblue", size = 1.2) +
  geom_point(size = 2) +
  scale_x_discrete() +
  theme(axis.text.x = element_blank(), 
        axis.ticks.x = element_blank()) +
  labs(x = NULL) 

p2 <-
  foodf %>%
  ggplot() +
    geom_col(aes(x = date, y = diffval, fill = diffval >= 0)) +
    scale_fill_manual(guide = FALSE, values = c(`TRUE` = "green", `FALSE` = "red")) + 
    scale_x_discrete(labels = labeldate) +
    labs(x = NULL) 

p1 / p2


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...