我正在为一条线之间的区域着色,如图所示。
但是,阴影区域在某些地方超过了线条和x轴。
仅将geom_area
定义为该行下方/上方的特定区域的最佳方法是什么?
以下是生成图表的代码:
library(dplyr)
library(ggplot2)
library(quantmod)
data.start.date <- "1990-01-01"
#temporary fix for FRED address change [only needed on MAC computers]
# options(download.file.method="libcurl")
# get symbols from FRED and convert to data frame
var.list <- c("IR", "IQ")
getSymbols(var.list, src="FRED")
ir.df <- data.frame(date= index(IR),IR$IR)
iq.df <- data.frame(date= index(IQ),IQ$IQ)
df <- dplyr::left_join(ir.df, iq.df, by = "date")
# Shorten dataframe, starting as defined above
df = dplyr::filter(df, date >= data.start.date)
# calculate trade balance (exports-imports)
df$tradebal <- df$IQ-df$IR
ggplot(data = df)+
geom_line(aes(x=date, y=tradebal, color="Trade Balance"),size=1.2) +
geom_area(aes(x=date, y=tradebal, fill=tradebal>0), alpha=.4)