简体   繁体   English

为什么我的多面图会创建2个geom_hlines,而传递的参数只能创建一个?

[英]Why is my faceted plot creating 2 geom_hlines with parameters passed to create only one?

I'm trying to draw one horizontal line on one panel of a facetted ggplot. 我正在尝试在多面ggplot的一个面板上绘制一条水平线。 If you look at the attached graphic, I want it in the bottom facet, and not the top. 如果您查看所附的图形,我希望它位于底部,而不是顶部。 I'm getting it in both, and I can't recreate it. 我都得到了它,我无法重新创建它。 My code is below; 我的代码如下; it would be fully reproducible for any viewer if it weren't for the loading of csv data. 如果不是用于加载csv数据,则对于任何查看器来说都是完全可复制的。 The ggplot creation is at the end of the code, and the call to geom_hline is the 2nd line in the ggplot call. ggplot创建在代码的末尾,对geom_hline的调用是ggplot调用中的第二行。 The data frame that is the source of the line parameters is jst above it, named 'mdiLevel'. 线参数来源的数据框位于其上方的第一个位置,名为“ mdiLevel”。 I'm hoping something jumps out at someone without this being fully reproducible. 我希望有人能跳出来,而这是无法完全再现的。 Thanks in advance... 提前致谢...

在此处输入图片说明

require("tseries")
require("zoo")
require("reshape2")
require("ggplot2")
require("grid")

filePath = "Fakepath/MDI.csv"
data = read.csv(filePath, header=TRUE, stringsAsFactors=FALSE)
data$Date = as.Date(data$Date, format="%m/%d/%y")
data = zoo(data$M.D.I., data$Date)

startDate = index(data[1])
endDate = index(data[length(data)])

spy = get.hist.quote(instrument='SPY', start=startDate, end=endDate, quote="AdjClose", 
          provider = "yahoo", origin="1970-01-01", compression = "d", retclass="zoo")

allData = merge(data, spy)
allData = na.locf(allData)
allData = allData[index(allData) %in% index(data),]

names(allData) = c('MDI', 'SPY')
allData = allData[complete.cases(allData),]
allData = data.frame(index(allData), allData)
rownames(allData) = NULL
names(allData) = c('Date', 'MDI', 'SPY')

resh = melt(allData, id='Date')

resh2 = resh
names(resh2) = c('Date', 'Var', 'Val')
resh2$Var = factor(resh2$Var, levels=c('SPY', 'MDI'))

mdiLevel = data.frame(variable='MDI', Z=0.5)

pl = ggplot(resh2, aes(x=Date, y=Val, color=Var)) + geom_line(size=1) + facet_grid(Var ~ ., scales='free') +

     geom_hline(data=mdiLevel,aes(yintercept=Z), linetype='dashed', color='darkgreen') +

     theme(legend.position='none', plot.title = element_text(vjust = 2, size=rel(1.8), 
     face="bold"), axis.text.x=element_text(color='orangered4',size=rel(1.2)), 
     axis.text.y=element_text(color='orangered4',size=rel(1.2)), axis.title.x = element_blank(), 
     axis.title.y = element_blank(), strip.text.x = element_text(size=rel(1.2)),
     panel.margin=unit(2,"lines"), strip.text.y = element_text(size=rel(1.2))) +
     ggtitle("MDI and SPY") + scale_color_brewer(palette="Set1")

plot(pl)

In the data mdiLevel you have to use the same variable names if you want the variable to be recognized as the faceting variable. 在数据mdiLevel中,如果要将变量识别为构面变量,则必须使用相同的变量名称。 Use Var instead of variable . 使用Var而不是variable

mdiLevel = data.frame(Var='MDI', Z=0.5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM