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")
6
  • 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. Commented Mar 28, 2020 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 Commented Mar 28, 2020 at 15:43
  • Please make your data example reproducible by following guidelines of this link: stackoverflow.com/questions/5963269/…. 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. Commented Mar 28, 2020 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 ? Commented Mar 28, 2020 at 15:55
  • @PaulPerton1: you will have to convert your data from wide to long format to make ggplot happly. See these examples: stackoverflow.com/a/52789737/786542 & stackoverflow.com/a/51752828/786542 Commented Mar 28, 2020 at 17:36

1 Answer 1

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

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

Comments

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.