ggplot2 R上的连续到离散图例

时间:2017-11-16 20:35:30

标签: r plot ggplot2 legend


我试图用ggplot2绘制曲线并按期限分组。在我的数据集中,到期日以剩余时间表示 我的问题是,在我的整个数据集中,我只有两个期限(0.15和0.2),但图例显示的颜色为0.15,0.16,0.17 ... 这是一个截图:

enter image description here 这是我的代码:

    call_vol_plot = ggplot(data=df[df$type=="C",],
       aes(x=strike,
           y=impliedVol,
           group = time_to_expiry,
           colour = time_to_expiry)) + geom_line() + geom_point(size=4, shape=21, fill="white")

    call_vol_plot = call_vol_plot + labs(title="Call Implied Volatility",
                           subtitle="Options on future contracts",
                           y="Implied Volatility",
                           x="Strike") + guides(color=guide_legend("Maturity in year"))

我尝试了scale_color_discrete,但它没有用。谢谢!

2 个答案:

答案 0 :(得分:1)

在aes中,将time_to_expiry转换为系数as.factor(time_to_expiry)

+ scale_color_manual(values = c(0.15, 0.2))

答案 1 :(得分:0)

解决方案 :按{预期} <key>CFBundleDocumentTypes</key> <array> <dict> <key>CFBundleTypeIconFiles</key> <array/> <key>CFBundleTypeName</key> <string>Pin The World 2 File</string> <key>CFBundleTypeRole</key> <string>Editor</string> <key>LSHandlerRank</key> <string>Owner</string> <key>LSItemContentTypes</key> <array> <string>com.valgy.PintheWorld2</string> </array> </dict> </array> <key>UTExportedTypeDeclarations</key> <array> <dict> <key>UTTypeConformsTo</key> <array> <string>public.data</string> </array> <key>UTTypeDescription</key> <string>Pin The World 2 File</string> <key>UTTypeIdentifier</key> <string>com.valgy.PintheWorld2</string> <key>UTTypeTagSpecification</key> <dict> <key>public.filename-extension</key> <string>p2w</string> <key>public.mime-type</key> <string>application/PintheWorld2</string> </dict> </dict> </array> 替换time_to_expiry。 R不能使用字符类型的变量来创建连续值。
非常感谢@Highland几乎给出了解决方案!