有时,R geom_path行“关闭".如何保持它们“打开"? [英] R geom_path lines "closing", sometimes. How to keep them "open"?
问题描述
我正在用ggplot的geom_path()绘制一系列财务预测.
I'm plotting a series of financial projections with ggplot's geom_path().
ggplot(df,aes(year,pot,color=iRate)) + geom_path() + theme(legend.position="none") + ylim(0,300000)
这就是我所拥有的.. [对不起,这是一个链接]
This is what I have..[sorry it's a link]
..其中两条路径碰到了右手边,即40年,但随后又回到了起点.一行没有.这不是轴显示限制的问题,也不是数据框中的流氓线的问题-如果我删除所有年份< 5发生同样的事情.
..two of the paths hit the right hand edge, year 40, but then nip back to their beginning. One line doesn't. This isn't a problem of axis-display limits, nor of a rogue line in the data frame - if I remove all years < 5 the same thing happens. It may be a just problem of overburdening the plotting device, but it is repeatable..
有些问题询问如何关闭" geom_path,但无法回答此问题.如何确保路径保持开放"状态?
There are questions asking how to 'close' a geom_path which don't answer this. How do I ensure the path stays 'open'?
inflation <- seq(1, 1.1, 0.02)
potGrowth <- seq(1.02, 1.1, 0.02)
div <- 10000
initcapital <- 100000
output <- numeric()
lifespan <- 40
delay <- 10
for(j in 1:length(inflation)){
str <- rep(0,lifespan)
for(i in delay:lifespan){ str[i] <- floor((inflation[j]^i)*div) }
for(k in 1:length(potGrowth)){
cap <- initcapital
for(i in 1:lifespan){
cap <- cap-str[i]; cap <- cap*potGrowth[k]
output <- append(output, floor(cap))
}
}
}
iLen <- length(inflation); gLen <- length(potGrowth)
simulations <- iLen*gLen
df <- data.frame(pot=output, projection=rep(1:simulations,each=lifespan), iRate=rep(inflation,each=lifespan*gLen), gRate=rep(rep(potGrowth,each=lifespan),times=iLen), year=rep(1:lifespan,times=simulations))
在这里可以通过插入group = projection
and here it is solved by inserting group=projection
ggplot(df,aes(year,pot,color = iRate,group = projection))+ geom_path()...
ggplot(df,aes(year,pot,color=iRate,group=projection)) + geom_path() ...
推荐答案
感谢@aosmith在您的评论中提及参数group
.这是一个可再现的示例,描述了空气质量数据集的路径关闭问题.假设您要绘制每个月各天的温度图表.保持所有月份在同一块图上(本着这些年度图的精神: arcticseaicenews ).
Thanks @aosmith for mentioning the argument group
in your comment. Here is a reproducible example describing the path closing issue with the airquality dataset. Let's say you want to plot a chart of temperature along the days of each month. Keeping all months on the same plot (in the spirit of these yearly plots: arcticseaicenews).
单独使用geom_path()
会导致上个月最后一天到下个月第一天之间令人讨厌的关闭"行.
Using geom_path()
alone leads to annoying "closing" lines between the last day of the previous month and the first day of the next month.
library(ggplot2)
ggplot(airquality, aes(x = Day, y = Temp)) +
geom_path()
在参数group=Month
中使用geom_path()
可以防止出现以下几行:
Using geom_path()
with the argument group=Month
prevents these lines from appearing:
ggplot(airquality, aes(x = Day, y = Temp, group=Month)) +
geom_path()
当然,您还可以根据需要使用facet_wrap
在不同的方面显示月份:
Of course you could also display months on different facets with facet_wrap
depending on what you desire:
ggplot(airquality, aes(x = Day, y = Temp)) +
geom_path() +
facet_wrap(~Month)
这篇关于有时,R geom_path行“关闭".如何保持它们“打开"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!