r - MinMax 图为 ggplot
问题描述
plot()
我使用带有复杂图例的命令制作了一个情节。另外,我还有 3 个ggplots()
。当尝试使用所有 4 个图grid.arrange()
的gridExtra包创建多个图时,我收到此错误。这是因为我不能用 3 个 ggplots 和 1 个基本图制作一个网格。
错误:
gList 中的错误(list(grobs = list(list(x = 0.5,y = 0.5,width = 1,height = 1,:“gList”中只允许“grobs”)
所以我试图“转换”我plot()
的,qplot()
但我得到了这个输出:
这是我想要的输出:
这是我的整个函数,创建最小和最大点并将它们绘制到 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
有任何想法吗?
解决方案
这是一个好的开始。我没有弄乱不同大小的点,但这是一般格式。
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 = "")
由reprex 包(v0.2.0)于 2018 年 9 月 20 日创建。
推荐阅读
- bash - QProcess来自concole的不同输出
- node.js - Vercel 不显示 expressjs 静态文件
- mysql - 在 Django 中用 MySQL 数据库替换 SQLite 数据库
- mongodb - 有没有人能够在 cosmos mongodb 上使用带有日期字段的 ttl?
- python - 如何使用 replit 在 pygame 中显示字体
- cuda - CUDA 异步内存复制 - 哪个硬件设备执行内存复制操作?
- node.js - Heroku /bin/sh: 1: g++: 未找到
- java - bluetoothAdapter.startDiscovery() 没有返回任何内容,或者 Android 中未调用 BroadcastReceiver 接收器
- excel - Excel Cube 函数 Power Pivot 模型
- python - urllib.request.urlopen 对某些 URL 成功,对其他 URL 超时