1

excel plot

I'm trying to recreate a graph I made in excel, using ggplot2 in R and I'm having some trouble.

My variable 1 is a continuous variable (prices) and my variable two is a discrete one (cashflows)- both plotted against the same time steps.

As you noticed one variable is plotted using bars and the other using a line.

Is there any way someone could give me some help using random values? I was only able to plot them as lines.

In the sample code below v1 is the prices, v2 is the cashflows, and time is a seq(1:270)

 gdata = data.frame(num = time,  prices=v2, cashflows = v3)
test_data <- melt(gdata, id="num")  

ggplot(data=test_data, aes(x=num, y=value, colour=variable)) +
  geom_line() +
  ggtitle("Prices") +
  labs(x="Time",y="Prices") + theme_grey(base_size = 14) + theme(legend.title=element_blank()) 
George T.
  • 11
  • 3
  • 2
    Your picture shows two Y axes? See this question http://stackoverflow.com/questions/3099219/plot-with-2-y-axes-one-y-axis-on-the-left-and-another-y-axis-on-the-right – Liesel Feb 10 '16 at 01:45
  • The answer points to https://github.com/hadley/ggplot2/wiki/Align-two-plots-on-a-page – IRTFM Feb 10 '16 at 02:20
  • 1
    Or make two separate plots and align them with the `cowplot` package. – Axeman Feb 10 '16 at 12:59

0 Answers0