ggplot2功能区图不显示

时间:2018-11-16 03:54:25

标签: r ggplot2

我是R semi-proficeint,但是在此问题上停留了2天;我要做的只是生成一个简单的项目预期和实际成本的功能区图。数据如下:

                InfoSource TimePoint PredictedCost  CPI ProjectedCost
1        XXX Business Case   2012-06       1959348  1.0       1959348
2    Second  Business Case   2014-11       2307669  6.2       2080827
3          Report to Board   2015-07       3359142  7.1       2098461
4 Report to Corporate Exec   2017-06       4153769 10.3       2161160

glimpse()输出:

## Observations: 4
## Variables: 5
## $ InfoSource    <chr> "XXX Business Case", "Second  Business Case",     "Report to Board", "Report to Corporate Exec"
## $ TimePoint     <chr> "2012-06", "2014-11", "2015-07", "2017-06"
## $ PredictedCost <int> 1959348, 2307669, 3359142, 4153769
## $ CPI           <dbl> 1.0, 6.2, 7.1, 10.3
## $ ProjectedCost <int> 1959348, 2080827, 2098461, 2161160

我尝试了各种调用ggplot2的方法,以使其正常工作,并始终生成我想要的一切,除了功能区本身。这里的例子...

Plot Example

这是我给ggplot的电话-

ggplot(Projection) +
  geom_ribbon(aes(
    x = as.factor(TimePoint), ymin = ProjectedCost, ymax = PredictedCost,
    fill = "Cost over-run"
  ), alpha = 1.0) +
  scale_fill_manual("", values = "darkred") +
  labs(
    y = "Project cost ($)", x = "Milestone", colours = "Parameter",
    title = "XXXX Project cost analysis Oct 2018"
  )

关于我在这里缺少什么的任何想法?

1 个答案:

答案 0 :(得分:1)

尝试仅使用日期:

read.csv(text='InfoSource,TimePoint,PredictedCost,CPI,ProjectedCost
"XXX Business Case","2012-06",1959348,1.0,1959348
"Second Business Case","2014-11",2307669,6.2,2080827
"Report to Board","2015-07",3359142,7.1,2098461
"Report to Corporate Exec","2017-06",4153769,10.3,2161160',
         stringsAsFactors = FALSE) -> Projection

Projection$TimePoint <- as.Date(sprintf("%s-01", Projection$TimePoint))

ggplot(Projection) +
  geom_ribbon(aes(
    x = TimePoint, ymin = ProjectedCost, ymax = PredictedCost, 
    fill = "Cost over-run"
  ), alpha = 1.0) +
  scale_fill_manual("Parameter", values = "darkred") +
  labs(
    x = "Milestone", y = "Project cost ($)",
    title = "XXXX Project cost analysis Oct 2018"
  )

enter image description here