R ggplot某些分位数的带条形的线图
我有一个GG曲线图(实际上有两个y轴): 对于指数曲线(红线),我想在指数超过0.9%的时间段添加灰色水平条(因此当指数较高时)。因此,只要有一个猜测,这可能意味着三个酒吧:可能在2002-2003年,2008-2010年和2011-2012年之间 我想象这些灰色条是以这里产生的方式出现的:(从y=0一直上升到y=max) 我也试着从那里的做法中学习,但我无法将其转化为具体的问题。这在ggplot中可能吗 以下是我迄今为止创建的情节:R ggplot某些分位数的带条形的线图,r,ggplot2,quantile,R,Ggplot2,Quantile,我有一个GG曲线图(实际上有两个y轴): 对于指数曲线(红线),我想在指数超过0.9%的时间段添加灰色水平条(因此当指数较高时)。因此,只要有一个猜测,这可能意味着三个酒吧:可能在2002-2003年,2008-2010年和2011-2012年之间 我想象这些灰色条是以这里产生的方式出现的:(从y=0一直上升到y=max) 我也试着从那里的做法中学习,但我无法将其转化为具体的问题。这在ggplot中可能吗 以下是我迄今为止创建的情节: library(ggplot2) p2 <- ggp
library(ggplot2)
p2 <- ggplot(data, aes(x = Date))
p2 <- p2 + geom_line(aes(y = Count, colour = "Count"),size=1)
p2 <- p2 + geom_line(aes(y = Index, colour = "Index"), size=1)
p2 <- p2 + scale_y_continuous(sec.axis = sec_axis(~./1, name = "Index"))
p2 <- p2 + scale_colour_manual(values = c("#56B4E9", "#CC6666"))
p2 <- p2 + labs(y = "Count", x = "", colour = "") +
theme(axis.title.x = element_text(face="bold", size=12)) +
theme(axis.title.y = element_text(face="bold", size=12)) +
theme(plot.title = element_text(face="bold", size=20, hjust=0.5))
p2
库(ggplot2)
p2这里是一种可能的方法,首先生成水平条应出现的日期:
df3 <- df
df3$quant <- ifelse(df$Index > quantile(df$Index, 0.9), 1, 0)
在geom_ribbon
中,只需对数据进行子集处理,以便只绘制quant==1
的日期,并使用rle
调用获得的分组来限制功能区的范围
ggplot(df2, aes(x = Date))+
geom_ribbon(data = subset(df2, quant == 1),
aes(x = Date, ymin = 0, ymax = Inf, group = group), alpha = 0.2) +
geom_line(aes(y = Count, colour = "Count"), size=1)+
geom_line(aes(y = Index, colour = "Index"), size=1) +
scale_y_continuous(sec.axis = sec_axis(~./1, name = "Index")) +
scale_colour_manual(values = c("#56B4E9", "#CC6666")) +
labs(y = "Count", x = "", colour = "") +
theme(axis.title.x = element_text(face = "bold", size = 12)) +
theme(axis.title.y = element_text(face = "bold", size = 12)) +
theme(plot.title = element_text(face = "bold", size = 20, hjust = 0.5))
这正是我要找的酒吧类型!非常感谢你!我在试图反驳你的方法时遇到了一些问题:1)在第一行(如果else(Index>quantile(.$Index,0.9),1,0)),你使用了一个点。您是否在(.3)之前将数据集保存在一个点下?我读到%>%用于表示不同的函数“彼此相邻”,而不是以嵌套方式写入它们。但是在这种情况下,嵌套版本是什么呢?当我尝试运行这部分代码时,我得到消息“mutate_impl(.data,dots)中出错:错误的结果大小(205),预期为512或1”。“我不知道上面写的是什么。”SCW16编辑了这篇文章。我已经将第一部分(不可生成的代码)更改为基本的R方法。然后是tidyverse
方法。您将发现df2==df3
。管道%>%
用于在下一次调用中将数据作为函数的第一个参数进行管道传输,如果数据不应位于第一个位置,则可以使用
引用它,因此管道后的$Index
相当于df$Index
。无论如何,在这种特殊情况下根本不需要它,因为函数mutate
假定所有命名变量都来自数据。我把它拿走了。请检查它是否可复制。它确实有效!谢谢你的建设性帮助!
df3$group <- rep(1:length(rle(df3$quant)$lengths), times = rle(df3$quant)$lengths)
library(tidyverse)
df %>%
mutate(quant = ifelse(Index > quantile(Index, 0.9), 1, 0),
group = rep(1:length(rle(quant)$lengths), times = rle(quant)$lengths)) -> df2
ggplot(df2, aes(x = Date))+
geom_ribbon(data = subset(df2, quant == 1),
aes(x = Date, ymin = 0, ymax = Inf, group = group), alpha = 0.2) +
geom_line(aes(y = Count, colour = "Count"), size=1)+
geom_line(aes(y = Index, colour = "Index"), size=1) +
scale_y_continuous(sec.axis = sec_axis(~./1, name = "Index")) +
scale_colour_manual(values = c("#56B4E9", "#CC6666")) +
labs(y = "Count", x = "", colour = "") +
theme(axis.title.x = element_text(face = "bold", size = 12)) +
theme(axis.title.y = element_text(face = "bold", size = 12)) +
theme(plot.title = element_text(face = "bold", size = 20, hjust = 0.5))