0

I keep getting an error becasue the bargraphs are used for quaterly data and the line is going to be data from the entire year so it will have many points.

The only issue is with the geom_line function which I am new to using. The error is -->

Error in scale_fill_manual(values = c("green", "yellow")) + geom_line(aes(x = dts2, : non-numeric argument to binary operator

t="DG"
fin=getFinancials(t, auto.assign = F)
dts = labels(fin$BS$A)[[2]]
dts2 = paste(substr(dts,1,7),"::",dts, sep="")
stockprices = getSymbols(t, auto.assign = F)
price = rep(0,NROW(dts))
for(i in 1:NROW(price))
{
  price[i]=as.vector(last(stockprices[dts2[i],6]))
}

yr= as.numeric(substr(dts,1,4))
pastyr = yr -2
betayr = paste(pastyr,"::",yr,sep="")

os = fin$BS$A["Total Common Shares Outstanding", ]
gw= fin$BS$A["Goodwill, Net", ]
ta= fin$BS$A["Total Assets", ]
td= fin$BS$A["Total Debt", ]
ni= fin$IS$A["Net Income", ]
btax = fin$IS$A["Income Before Tax", ]
atax = fin$IS$A["Income After Tax",]

intpaid = fin$CF$A["Cash Interest Paid, Supplemental",]
gw[is.na(gw)]=0
intpaid[is.na(intpaid)]=0
taa = (ta - gw)/os 

Rd = rep(0,NROW(dts))

for(i in 1:NROW(dts))
{
  if(td[i]!=0)
  {
    Rd[i] = intpaid[i]/td[i]
  }
}


gspc = getSymbols("^GSPC", auto.assign = F)
gs5 = getSymbols("GS5", src = "FRED", auto.assign = F)

marketRisk = rep(0,NROW(dts))
riskFree = rep(0,NROW(dts))
beta = rep(0,NROW(dts))

for(i in 1:NROW(dts))
{
marketRisk[i]= mean(yearlyReturn(gspc[betayr[i]]))
riskFree[i] = mean(gs5[betayr[i]])
gspc.weekly = weeklyReturn(gspc[betayr[i]])

stockprices.weekly = weeklyReturn(stockprices[betayr[i]])
beta[i] = CAPM.beta(stockprices.weekly,gspc.weekly)
}

Re = (riskFree/100) + beta * (marketRisk-(riskFree/100))

E = os*price
V=E+td
Tc = (btax - atax)/btax
wacc = (E/V)*Re + (td/V)*Rd*(1-Tc)
margin = (ni/wacc)/os  - taa

taadf = data.frame(dts,val = taa,cat="ta")
margindf = data.frame(dts,val = margin ,cat="margin")

mdf=rbind(margindf,taadf)

#linrng = paste(dts[NROW(dts)],"::",dts[1],sep="")

#dfdt = data.frame(stockprices[linrng,6])
#dfdt2 = data.frame(dt = labels(dfdt)[[1]],dfdt$AAPL.Adjusted,cat="taa")
#names(dfdt2)=c("dt,price,cat")
pricedf = data.frame(as.vector((stockprices[dts2[i],6])))
ggplot(mdf, aes(x=dts,y=val,fill=cat)) + geom_bar(stat="identity",color="black") 
  scale_fill_manual(values = c("green","yellow")) + 
  geom_line(aes(x=dts2, y=stockprices), stat = "identity", 
  position = "identity", na.rm = FALSE, show.legend = NA,
  inherit.aes = TRUE)
  • The non-numeric to binary suggests that when you pasted the `dts2` it became a character variable and the line plot seems to be expecting a numeric value to match the x-axis for `dts` in the bar graph. Try converting it to a number if possible (I cannot see real data or a `str()` to know for sure) – sconfluentus Apr 27 '17 at 02:24

1 Answers1

0

Note, the object stockprices is An ‘xts’ object. So, you can't use inside ggplot scale. I picked the fist variable of stockprices object to show the code, but you probabli want another one.

library(dplyr)
library(quantmod)
library(PerformanceAnalytics)
library(ggplot2)

stockprices_df <- as.data.frame(stockprices) %>% 
  mutate(date = rownames(.)) %>% 
  filter(date %in% dts)

ggplot() +
  geom_col(
    data = mdf,
    aes(x = dts,y = val,fill = cat)
  ) +
  geom_line(
    data = stockprices_df,
    aes(x = date, y = DG.Open, group = 1 )
  ) +
  scale_fill_manual(values = c("green","yellow")) 

[1]

Italo Cegatta
  • 410
  • 4
  • 8