1

I have 2 data frames and I would like to create one plot where the data from the "now" data frame is a bar chart and the data from the "historical" data frame is a line chart.

I have created the bar chart but am not sure how to overlay the data from the "historical" data frame as a lines and have there only be one legend. The Legend should contain 4 elements: a_today, b_today, a_hist, b_hist

historical= data.frame(x = c("10:00","10:30","11:00","10:00","10:30","11:00"), value= c(1,2,3,4,5,6), category = c("a_hist","a_hist","a_hist","b_hist","b_hist","b_hist"))
historical

now= data.frame(x = c("10:00","10:30","10:00","10:30"), value= c(8,6,10,10), category = c("a_today","a_today","b_today","b_today"))
now

ggplot(now, aes(x=x, y=value, fill = category )) + 
  geom_bar(stat= "identity",position=position_dodge()) + ggtitle("this is my plot")




ggplot(now, aes(x=x, y=value, fill = category )) + geom_bar(stat= "identity",position=position_dodge()) + ggtitle("this is my plot") + 
  geom_line(data = historical, aes(x=x, y=value, group = category, col=category)) + scale_color_discrete(guide = F) + 
  scale_fill_manual(values = c("a_hist"= "green","b_hist" ="salmon","a_today" = "yellow", "b_today" = "red") ) + geom_point()

enter image description here

Any idea how to get points to show up on the line properly and not on the bar chart?

6
  • 1
    +geom_line(data=historical,aes(x=x,y=value,fill=category)) ? Commented Aug 12, 2015 at 17:29
  • ggplot(now, aes(x=x, y=value, fill = category )) + geom_bar(stat= "identity",position=position_dodge()) + ggtitle("this is my plot") + geom_line(data = historical, aes(x=x, y=value, group = category, col=category)) + scale_color_discrete(guide = F) is almost what you want but in the legend I assume you want lines for historical data?!? Commented Aug 12, 2015 at 17:36
  • That is almost it. If the legend could show lines that'd be great. How can I set the colors for the 4 time series and add points to the lines? Thank you. Commented Aug 12, 2015 at 17:38
  • For that latter question try google: stackoverflow.com/questions/17180115/… and for manual legend construction stackoverflow.com/questions/17148679/… Commented Aug 12, 2015 at 17:40
  • Oh if you want to add points + geom_point(data = historical, aes(x=x, y=value, group = category, col=category)) will do, I'll update my answer Commented Aug 12, 2015 at 17:47

1 Answer 1

3

Now this should be all you asked for (but with really ugly colors):

#data
historical= data.frame(x = c("10:00","10:30","11:00","10:00","10:30","11:00"), value= c(1,2,3,4,5,6), category = c("a_hist","a_hist","a_hist","b_hist","b_hist","b_hist"))
now= data.frame(x = c("10:00","10:30","10:00","10:30"), value= c(8,6,10,10), category = c("a_today","a_today","b_today","b_today"))
#plot
ggplot() + 


geom_bar(data = now, aes(x=x, y=value, fill = category ), stat= "identity",position=position_dodge()) + 
  ggtitle("this is my plot") +
  geom_line(data = historical, aes(x=x, y=value, group = category, col=category)) + 
  geom_point(data = historical, aes(x=x, y=value, group = category, col=category)) +
  scale_fill_manual(name = "today", 
                    values = c("a_today"="green", "b_today" = "purple"), 
                    labels = c("a_today", "b_today")) + 
  scale_color_manual(name = "historical", 
                     values = c("a_hist"="red", "b_hist"="blue"),
                     labels = c("a_hist", "b_hist"))

gives

enter image description here

For the additional gimmicks you asked for there are plenty of references on SO, e.g. I used:

Sign up to request clarification or add additional context in comments.

2 Comments

mts when I add geom_point() it adds to the bar chart not the line chart. Any thoughts?
see my comment to your question above, you need to re-specify mapping and data, thus add + geom_point(data = historical, aes(x=x, y=value, group = category, col=category))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.