我同意@yarnabrina 的观点,geom_area 是您需要的选项。但是,ggplot 需要一些帮助才能完全满足您的要求,请参见下文。
data.df <- structure(list(year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996,
1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
), saldo = c(8275867, 3702514, -2636805, -3665755, -5751042,
841426, 48908, -4019329, -4943662, -2199522, 1060542, 6223146,
16661053, 16087980, 12130455, 11699873, 12392519, 11272844, 12556386,
16885829, 11381863, 9020415, 12008167, 1521176, 2668277.78157,
-3419083.03045001, 2057032, -8308931, -3881619)), row.names = c(NA,
-29L), class = c("tbl_df", "tbl", "data.frame"))
仅使用geom_area的图表
ggplot(data.df, aes(x=year, y=saldo)) +
geom_area() +
geom_line(color= "red") +
geom_point(color= "red")
我添加了一条线和点来显示您的数据。现在很明显,geom_area 在穿过 x 轴时无法正常工作。
要解决这个问题,我们需要将 y 轴交叉点添加到数据集中。这可以通过 R 基础的 lm 和 predict 函数的组合来完成。
见下面的代码来计算新的点(来自这个post)
rx <- do.call("rbind",
sapply(1:(nrow(data.df)-1), function(i){
f <- lm(year~saldo, data.df[i:(i+1),])
if (f$qr$rank < 2) return(NULL)
r <- predict(f, newdata=data.frame(saldo=0))
if(data.df[i,]$year < r & r < data.df[i+1,]$year)
return(data.frame(year=r,saldo=0))
else return(NULL)
}))
newdata.df <- rbind(data.df, rx)
newdata.df <- newdata.df[order(newdata.df$saldo),]
并绘制图表。
ggplot(newdata.df, aes(x=year, y=saldo)) +
geom_area() +
geom_line(color= "red") +
geom_point(color= "red")
现在它看起来更像您正在寻找的东西。最后一件事是修改代码以使其更好。
最终剧情
设置一些值以便于配置(您可以找到更多颜色选项here 或使用谷歌^_^)
color.plot <- "darkorchid4"
size.line <- 1.5
选项 1
ggplot(newdata.df, aes(x=year, y=saldo, color = "Trade balance")) +
geom_area(fill = color.plot, alpha = 0.6, size = size.line) +
# geom_line(color= color.plot, size = size.line) +
geom_hline(aes(yintercept = 0), size = size.line) +
labs(color= "", title="Argentina Trade Balance",x="",y="Saldo") +
scale_y_continuous(breaks = seq(from = -10e6, to = 20e6, by = 2.5e6),
limits = c(-10e6, 20e6)) +
scale_x_continuous(breaks = seq(from = 1990, to = 2018, by = 1),
limits = c(1990, 2018)) +
scale_color_manual(values = c("Trade balance" = color.plot)) +
theme_minimal() +
theme(text = element_text(size=10),
axis.text.x = element_text(angle=90, hjust=1),
plot.title = element_text(hjust = 0.5),
legend.position = "bottom")
选项 2
ggplot(newdata.df, aes(x=year, y=saldo, fill = "Trade balance")) +
geom_area(alpha = 0.6) +
geom_line(color= color.plot, size = size.line) +
geom_hline(aes(yintercept = 0)) +
labs(fill= "", title="Argentina Trade Balance",x="",y="Saldo") +
scale_y_continuous(breaks = seq(from = -10e6, to = 20e6, by = 2.5e6),
limits = c(-10e6, 20e6)) +
scale_x_continuous(breaks = seq(from = 1990, to = 2018, by = 1),
limits = c(1990, 2018)) +
scale_fill_manual(values = c("Trade balance" = color.plot)) +
theme_minimal() +
theme(text = element_text(size=10),
axis.text.x = element_text(angle=90, hjust=1),
plot.title = element_text(hjust = 0.5),
legend.position = "bottom")
回答问题编辑
我不确定您的问题出在哪里。在我的电脑中,它运行良好,并产生了与我帖子中的最后一个类似的情节。
我在这里添加了完整的代码来生成绘图,以防万一它可能有用,实际上除了创建初始数据框之外的所有内容都是直接复制/粘贴您问题中的代码。
TB_since_1990 <- structure(list(year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996,
1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
), saldo = c(8275867, 3702514, -2636805, -3665755, -5751042,
841426, 48908, -4019329, -4943662, -2199522, 1060542, 6223146,
16661053, 16087980, 12130455, 11699873, 12392519, 11272844, 12556386,
16885829, 11381863, 9020415, 12008167, 1521176, 2668277.78157,
-3419083.03045001, 2057032, -8308931, -3881619)), row.names = c(NA,
-29L), class = c("tbl_df", "tbl", "data.frame"))
rx <- do.call("rbind",
sapply(1:(nrow(TB_since_1990)-1), function(i){
f <- lm(year~saldo, TB_since_1990[i:(i+1),])
if (f$qr$rank < 2) return(NULL)
r <- predict(f, newdata=data.frame(saldo=0))
if(TB_since_1990[i,]$year < r & r < TB_since_1990[i+1,]$year)
return(data.frame(year=r,saldo=0))
else return(NULL)
}))
newdata.df <- rbind(TB_since_1990, rx)
newdata.df <- newdata.df[order(newdata.df$saldo),]
color.plot <- "darkorchid4"
size.line <- 1.5
ggplot(newdata.df, aes(x=year, y=saldo, fill = "Trade balance")) +
geom_area(alpha = 0.6) +
geom_line(color= color.plot, size = size.line) +
geom_hline(aes(yintercept = 0)) +
labs(fill= "", title="Argentina Trade Balance",x="",y="Saldo") +
scale_y_continuous(breaks = seq(from = -10e6, to = 20e6, by = 2.5e6),
limits = c(-10e6, 20e6)) +
scale_x_continuous(breaks = seq(from = 1990, to = 2018, by = 1),
limits = c(1990, 2018)) +
scale_fill_manual(values = c("Trade balance" = color.plot)) +
theme_minimal() +
theme(text = element_text(size=10),
axis.text.x = element_text(angle=90, hjust=1),
plot.title = element_text(hjust = 0.5),
legend.position = "bottom")
根据您的图像,ggplot 似乎没有正确解释颜色,更重要的是,geom_area 没有按预期工作。您可能想再次安装 ggplot 或在另一台计算机上尝试该代码。祝你好运!