ggplot with three bars and one line using geom_col and geom_line

Hello,
I need to plot 3 bars and one line in one graph. I thought It was simple, but I can't progress.
This is the code I've made as an example:

d1=c(rep("in1",13),rep("in2",13),rep("in3",13),rep("in4",13))
value1=data.frame(seq(1:13),runif(n = 13,10,70))
value2=data.frame(seq(1:13),runif(n = 13,0,10))
value3=data.frame(seq(1:13),runif(n = 13,5,75))
value4=data.frame(seq(1:13),runif(n = 13,7,65))

colnames(value1)=c("date","income")
colnames(value2)=c("date","income")
colnames(value3)=c("date","income")
colnames(value4)=c("date","income")

dataF=rbind(value1,value2,value3,value4)
dataT=data.frame(dataF,d1)
library(ggplot2)
p=ggplot(dataT[1:39,],aes(x=date,y=income,fill=d1))
p+geom_col()
p+geom_line(dataT[40:52,],group=1,mapping=aes(x=date,y=income) )

My idea is using geom_col with geom_line on the same graph.
I can plot

p=ggplot(dataT[1:39,],aes(x=date,y=income,fill=d1))
p+geom_col()

or

p=ggplot(dataT[1:39,],aes(x=date,y=income,fill=d1))
p+geom_line(dataT[40:52,],group=1,mapping=aes(x=date,y=income) )

But if my desire is to overlapping them...I fail.
Can you guide me, please?
Thanks for your time and interest.

Is this the plot you want?

d1=c(rep("in1",13),rep("in2",13),rep("in3",13),rep("in4",13))
value1=data.frame(seq(1:13),runif(n = 13,10,70))
value2=data.frame(seq(1:13),runif(n = 13,0,10))
value3=data.frame(seq(1:13),runif(n = 13,5,75))
value4=data.frame(seq(1:13),runif(n = 13,7,65))

colnames(value1)=c("date","income")
colnames(value2)=c("date","income")
colnames(value3)=c("date","income")
colnames(value4)=c("date","income")

dataF=rbind(value1,value2,value3,value4)
dataT=data.frame(dataF,d1)
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3
# p=ggplot(dataT[1:39,],aes(x=date,y=income,fill=d1))
# p+geom_col()
# 
# p=ggplot(dataT[1:39,],aes(x=date,y=income,fill=d1))
# p+geom_line(dataT[40:52,],group=1,mapping=aes(x=date,y=income) )

ggplot() + geom_col(data= dataT[1:39,],mapping = aes(x=date,y=income,fill=d1)) +
  geom_line(data= dataT[40:52,],group=1,mapping=aes(x=date,y=income))

Created on 2019-11-26 by the reprex package (v0.3.0.9000)

2 Likes

Yes. That's the plot I need it.
One more question about it. What can I do in order to plot the bars using the secondary y axis, and the line the primary axis?
Thanks for your guidance, FJCC.
I really appreciate your effort.

I have almost never used secondary axes so there may be a better way. I used the documentation at

d1=c(rep("in1",13),rep("in2",13),rep("in3",13),rep("in4",13))
value1=data.frame(seq(1:13),runif(n = 13,10,70))
value2=data.frame(seq(1:13),runif(n = 13,0,10))
value3=data.frame(seq(1:13),runif(n = 13,5,75))
value4=data.frame(seq(1:13),runif(n = 13,7,65))

colnames(value1)=c("date","income")
colnames(value2)=c("date","income")
colnames(value3)=c("date","income")
colnames(value4)=c("date","income")

dataF=rbind(value1,value2,value3,value4)
dataT=data.frame(dataF,d1)
library(ggplot2)

ggplot()  + 
  geom_col(data= dataT[1:39,],mapping = aes(x=date,y=income/2 ,fill=d1)) +
  geom_line(data= dataT[40:52,],group=1,mapping=aes(x=date,y=income)) + 
  scale_y_continuous(sec.axis = sec_axis(trans = ~ . * 2)) +
  labs(y = "in4")

Created on 2019-11-27 by the reprex package (v0.2.1)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.