MinMax заговор как ggplot - PullRequest
       12

MinMax заговор как ggplot

0 голосов
/ 20 сентября 2018

Я сделал сюжет, используя команду plot() со сложной легендой.Также у меня есть 3 других ggplots().При попытке создать несколько графиков, используя grid.arrange() из пакета gridExtra со всеми 4 графиками, я получаю эту ошибку.Это потому, что я не могу создать сетку с 3 ggplots и 1 базовым графиком.

ОШИБКА:

Ошибка в gList (list (grobs = list (list (x = 0.5, y =)0.5, ширина = 1, высота = 1,: в gList разрешены только 'grobs'

Поэтому я попытался "преобразовать" мои plot() в qplot(), но я получаю этот вывод:

enter image description here

И это мой желаемый вывод:

enter image description here

Вот вся моя функция: создание точек Min и Max и построение их в p2 ().

inflect <- function(x, threshold = 1)
{
  up   <- sapply(1:threshold, function(n) c(x[-(seq(n))], rep(NA, n)))
  down <-  sapply(-1:-threshold, function(n) c(rep(NA,abs(n)), x[-seq(length(x), length(x) - abs(n) + 1)]))
  a    <- cbind(x,up,down)
  list(minima = which(apply(a, 1, min) == a[,1]), maxima = which(apply(a, 1, max) == a[,1]))
}

# Pick a desired threshold # to plot up to
n <- 2
bottoms <- lapply(1:n, function(x) inflect(smooth$amount, threshold = x)$minima)
tops <- lapply(1:n, function(x) inflect(smooth$amount, threshold = x)$maxima)
# Color functions
cf.1 <- grDevices::colorRampPalette(c("pink","red"))
cf.2 <- grDevices::colorRampPalette(c("cyan","blue"))

p2 <- qplot(smooth$amount, type = 'l', main = "Minima & Maxima")

for(i in 1:n){
  points(bottoms[[i]], smooth$amount[bottoms[[i]]], pch = 16, col = cf.1(n)[i], cex = i/1.5)
}
for(i in 1:n){
  points(tops[[i]], smooth$amount[tops[[i]]], pch = 16, col = cf.2(n)[i], cex = i/1.5)
}
legend("topright", legend = c("Minima",1:n,"Maxima",1:n), 
       pch = rep(c(NA, rep(16,n)), 2), col = c(1, cf.1(n),1, cf.2(n)), 
       pt.cex =  c(rep(c(1, c(1:n) / 1.5), 2)), cex = .75, ncol = 2)

Мои данные:

  date         amount
2012-07-01   1.970755
2012-08-01   3.976561
2012-09-01   5.180346
2012-10-01   5.671865
2012-11-01   5.370846
2012-12-01   3.884054
2013-01-01   3.214452
2013-02-01   3.483037
2013-03-01   3.777904
2013-04-01   3.990532
2013-05-01   4.051390
2013-06-01   3.748177
2013-07-01   3.160887
2013-08-01   2.894250
2013-09-01   3.034773
2013-10-01   3.027029
2013-11-01   2.980137
2013-12-01   2.870456

Есть идеи?

1 Ответ

0 голосов
/ 20 сентября 2018

Вот хорошее начало.Я не связывался с точками разных размеров, но это общий формат.

library(tidyverse)

df <- read_table2(" date         amount
2012-07-01   1.970755
2012-08-01   3.976561
2012-09-01   5.180346
2012-10-01   5.671865
2012-11-01   5.370846
2012-12-01   3.884054
2013-01-01   3.214452
2013-02-01   3.483037
2013-03-01   3.777904
2013-04-01   3.990532
2013-05-01   4.051390
2013-06-01   3.748177
2013-07-01   3.160887
2013-08-01   2.894250
2013-09-01   3.034773
2013-10-01   3.027029
2013-11-01   2.980137
2013-12-01   2.870456")

df %>% 
  mutate(date = lubridate::ymd(date),
         max  = ifelse(lead(amount) < amount & lag(amount) < amount, amount, NA),
         min = ifelse(lead(amount) > amount & lag(amount) > amount, amount, NA)) %>%
  ggplot(aes(x = date))+
  geom_path(aes(y = amount))+
  geom_point(aes(y = max, color = "Maxima"), size = 3, show.legend = T)+
  geom_point(aes(y = min, color = "Minima"), size = 3, show.legend = T)+
  scale_color_manual(values = c("blue", "red"))+
  labs(color = "")

Создано в 2018-09-20 пакетом представ. (v0.2.0).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...