2

I have a group of plots currently created in Base R using the code below

plot(PWR_Sports_Racing$`Running P&L`, type="l",col=4, xlab="Bets",ylab="Profit", main = "Racing YTD P&L")
par(new=T)
plot(PWR_Sports_Racing$`BFSP Running P&L`,type = "l",col=2, xlab = "",ylab = "",axes = F)
legend("topright",legend = c("P&L","BFSP P&L"),lty=c(1,1),col=c(4,2))

I would like to convert these to plots created with ggplot2 however when I try to get them to work the output looks worse.

This is where I have got

 ggplot(data = PWR_Sports_Racing) +
  geom_line(mapping = aes(,y=PWR_Sports_Racing$`Running P&L`))+
  ylab("Profit") +
  xlab("Date")

I presume it is something to do with multiple results on each day

Might help if I show some data,

Base Graphics R

Here is some of the data for you to get an idea

PWR_Sports_Racing <- structure(list(dmydate = c(43836, 43837, 43837, 43838, 43838, 
43838, 43839, 43839, 43839, 43839, 43839, 43840, 43840), Selection = c("Fantastic Flyer", 
"Cold Harbour", "Directory", "Corinthia Knight", "Sound Mixer", 
"Prince Ofd Rome", "Ekhtiyaar", "North America", "Melgate Majeure", 
"Marhaban", "Social City", "Reeves", "Flowery"), Country = c("UK", 
"UK", "UK", "UK", "UK", "UK", "UAE", "UAE", "UK", "UAE", "UK", 
"UK", "UK"), Time = c("7.45pm", "4.15pm", "5.45pm", "2.30pm", 
"5.15pm", "7.45pm", "3.05pm", "5.25pm", "3.30pm", "4.50pm", "6.00pm", 
"1.10pm", "2.00pm"), Course = c("Wolverhampton", "Southwell", 
"Southwell", "Newcastle", "Kempton", "Kempton", "Meydan", "Meydan", 
"Newcastle", "Meydan", "Chelmsford", "Lingfield", "Sedgefield"
), Race.Type = c("AWFT", "AWFT", "AWFT", "AWFT", "AWFT", "AWFT", 
"AWFT", "AWFT", "AWFT", "AWFT", "AWFT", "AWFT", "CHS"), Race.Type.2 = c("Hcap", 
"Hcap", "Hcap", "Cond", "Hcap", "Am Hcap", "Hcap", "Group 2", 
"Hcap", "Hcap", "Hcap", "Hcap", "Hcap"), Stake = c(20, 20, 20, 
20, 20, 20, 20, 40, 20, 20, 20, 20, 20), Stake.Points = c(1, 
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1), `E/W` = c("N", "N", "N", 
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N"), Total.Points = c(1, 
1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1), Odds = c(4.3, 5.2, 2.72, 
9.8, 2.8, 4.2, 2.6, 2.3, 8.2, 8.2, 10, 4.6, 5.5), BFSP = c(4.63, 
4.6, 2.7, 11.5, 3.5, 9.33, 2.16, 2.5, 5.45, 8.8, 11.2, 5.58, 
7.6), Result = c("L", "W", "L", "L", "L", "L", "W", "L", "L", 
"L", "W", "W", "L"), Commision = c(0.05, 0.05, 0.05, 0.05, 0.05, 
0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05, 0.05), Payout.1 = c(0, 
104, 0, 0, 0, 0, 50.4, 0, 0, 0, 191, 88.4, 0), `Loss/Profit` = c(-20, 
84, -20, -20, -20, -20, 30.4, -20, -20, -20, 171, 68.4, -20), 
    Payout.2 = c(0, 92, 0, 0, 0, 0, 42.04, 0, 0, 0, 213.8, 107.02, 
    0), `Loss/Profit.BFSP` = c(-20, 72, -20, -20, -20, -20, 22.04, 
    -20, -20, -20, 193.8, 87.02, -20), `Running.P&L` = c(-20, 
    64, 44, 24, 4, -16, 14.4, -5.6, -25.6, -45.6, 125.4, 193.8, 
    173.8), `BFSP.Running.P&L` = c(-20, 52, 32, 12, -8, -28, 
    -5.96, -25.96, -45.96, -65.96, 127.84, 214.86, 194.86)), row.names = c(NA, 
13L), class = "data.frame")
Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • 3
    Can you provide example data? It's easier to help you if the code/output is 100% reproducible and we don't have to guess at the data you're running this code with. – Tyler Burleigh Mar 28 '20 at 15:26
  • I have popped some data in a dropbox link and added an image on the original post. I suppose what I am asking is can this be replicated in ggplot2 – PaulPerton1 Mar 28 '20 at 15:43
  • Please make your data example reproducible by following guidelines of this link: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. Right now, your data are avalaible only if we have a dropbox account. It will be preferrable to copy/paste the output of `dput(PWR_Sports_Racing)` in your question. – dc37 Mar 28 '20 at 15:51
  • In addition, it's not clear in your question and in your code what is the x axis or the y axis on your plot. What is "Bets" ? Can you clarify it ? – dc37 Mar 28 '20 at 15:55
  • @PaulPerton1: you will have to convert your data from wide to long format to make `ggplot` happly. See these examples: https://stackoverflow.com/a/52789737/786542 & https://stackoverflow.com/a/51752828/786542 – Tung Mar 28 '20 at 17:36
  • PaulPerton1, when that dropbox link goes stale this question becomes non-reproducible. It would really help if you provided a representative sample (in your question) using `dput(head(x))` (limiting to the columns required and a reasonable number of rows). After that, please update your sample non-`ggplot2` plot to use just that data; you might find you need a different selection of rows to show sufficient variability, perhaps `sample` is your friend here :-) – r2evans Mar 28 '20 at 17:44

1 Answers1

1

Here is a possible solution

# Data set in long format
nr <- nrow(PWR_Sports_Racing)
df <- data.frame(y=c(PWR_Sports_Racing$"Running.P&L",  
                     PWR_Sports_Racing$"BFSP.Running.P&L"),
                 type=rep(c("P&L","BFSP P&L"), each=nr),
                 x=rep(1:nr, 2))

ggplot(data=df, aes(x=x, y=y, color=type)) +
 geom_line(size=1)+
 ylab("Profit") + xlab("Date") + 
 theme_bw() %+replace% theme(legend.position=c(0,1),
                             legend.justification=c("left","top"),
                             legend.background=element_rect(fill=NA,color=NA)) +
 guides(color=guide_legend(title=""))

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58