Mix line and scatter plot in ggplot

I looked a little good, but I was at a dead end. I can't seem to find a way to build a line that is not related to a scatterplot. Here are some of my details and code to clarify the problem. I have data from the following form

> head(allData) AnnounceDate MarketProbability DealStatus binary BrierScore 1 2000-04-10 0.3333333 Complete 1 0.2340565 2 2000-06-14 0.2142857 Complete 1 0.3618200 3 2000-06-26 0.6846154 Complete 1 0.3690167 4 2000-06-16 0.1875000 Complete 1 0.4364041 5 2000-10-05 0.9555556 Complete 1 0.3078432 6 2000-10-19 0.8500000 Complete 1 0.2670799 

I would like to plot MarketProbabilities scatter versus AnnounceDate and determine if the transaction completed successfully using color, i.e.

 ggplot(data = allData, aes(x=AnnounceDate, y=MarketProbability, colour=DealStatus)) + geom_point() + scale_colour_hue(h = c(180,0)) 

enter image description here

but I would also like to impose a sparkling Brier score, I tried

 ggplot(data = allData, aes(x=AnnounceDate, y=MarketProbability, colour=DealStatus)) + geom_point() + scale_colour_hue(h = c(180,0)) + geom_line(aes(x=AnnounceDate, y=BrierScore)) 

enter image description here

I'm confused, why does he draw two lines and add color? How can I associate a string with the previous constructed information?

+7
source share
2 answers

As @MattBagg noted, this problem was fixed by moving aes() for the color from the main ggplot() call and into the geom_point() call.

 library(ggplot2) allData = read.table(header=TRUE, colClasses=c("Date", "numeric", "character", "numeric", "numeric"), text="AnnounceDate MarketProbability DealStatus binary BrierScore 2000-04-10 0.3333333 Complete 1 0.2340565 2000-06-14 0.2142857 Complete 1 0.3618200 2000-06-26 0.6846154 Complete 1 0.3690167 2000-06-16 0.1875000 Complete 1 0.4364041 2000-10-05 0.9555556 Complete 1 0.3078432 2000-10-19 0.8500000 Complete 1 0.2670799") p1 = ggplot(data=allData, aes(x=AnnounceDate)) + geom_point(aes(y=MarketProbability, colour=DealStatus)) + scale_colour_hue(h = c(180,0)) + geom_line(aes(y=BrierScore)) ggsave(filename="plot_1.png", plot=p1, height=2.5, width=5) 

enter image description here

+5
source

Aes () in ggplot () are inherited by subsequent geomes. You assigned color = DealStatus, which is inherited by geom_line (), and it needs to do two lines to make two colors. I cannot verify this because I am on my phone, but try moving the color assignment from the main ggplot and to geom_point ():

 ggplot(data = allData, aes(x=AnnounceDate)) + geom_point(aes(colour=DealStatus, y=MarketProbability)) + scale_colour_hue(h = c(180,0)) + geom_line(aes(y=BrierScore, group=1)) 

I add a constant as a group argument to tell her to connect all the points. Otherwise, ggplot sometimes makes the wrong guess.

+2
source

All Articles