首页 > 解决方案 > 从绘图更改为 ggplot

问题描述

我目前使用下面的代码在 Base R 中创建了一组图

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))

我想将这些转换为使用 ggplot2 创建的图,但是当我尝试让它们工作时,输出看起来更糟。

这就是我得到的地方

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

我认为这与每天的多个结果有关

如果我显示一些数据可能会有所帮助,

基础图形 R

以下是一些数据供您了解

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")

标签: rggplot2

解决方案


这是一个可能的解决方案

# 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=""))

在此处输入图像描述


推荐阅读