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?

user3022875
  • 8,598
  • 26
  • 103
  • 167
  • 1
    `+geom_line(data=historical,aes(x=x,y=value,fill=category))` ? – scoa Aug 12 '15 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?!? – mts Aug 12 '15 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. – user3022875 Aug 12 '15 at 17:38
  • For that latter question try google: http://stackoverflow.com/questions/17180115/manually-setting-group-colors-for-ggplot2 and for manual legend construction http://stackoverflow.com/questions/17148679/ggplot2-need-to-construct-a-manual-legend-for-complicated-plot – mts Aug 12 '15 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 – mts Aug 12 '15 at 17:47
  • @user3022875 I updated my answer with everything you asked for – mts Aug 12 '15 at 18:06

1 Answers1

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:

Community
  • 1
  • 1
mts
  • 2,160
  • 2
  • 24
  • 34
  • mts when I add geom_point() it adds to the bar chart not the line chart. Any thoughts? – user3022875 Aug 12 '15 at 17:53
  • 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))` – mts Aug 12 '15 at 17:55