我正在使用此数据库:
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"))
这是我的代码,用于绘制阿根廷的贸易平衡时间序列:
install.packages("ggplot2")
library(ggplot2)
ggplot(TB_ARG_since_1990,aes(x=year,y=saldo,group=1))+
geom_line(aes(color=""),linetype = "solid",size=1)+
labs(color="Trade balance")+
labs(title="Argentina Trade Balance",x="",y="Saldo")+
theme_classic()+
scale_x_continuous(name="", labels=as.character(TB_ARG_since_1990$year), breaks = TB_ARG_since_1990$year)+
theme(text = element_text(size=10), axis.text.x = element_text(angle=90, hjust=1))+
scale_y_continuous(name="", labels=as.character(TB_ARG_since_1990$saldo),breaks= TB_ARG_since_1990$saldo)+
theme(text = element_text(size=10),axis.text.y = element_text(angle=0, hjust=1))
这是我当前的情节:
我想要这张图:
我不知道如何指出图中y轴的负值。另外,我想在y轴的每个值之间添加更多空间。
答案 0 :(得分:3)
我同意@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
在横过y轴时无法按预期工作。
要解决此问题,我们需要将y轴交叉点添加到数据集。可以结合使用R base的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或使用Google ^ _ ^)
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")
答案 1 :(得分:1)