为两个几何点图添加图例

时间:2018-03-07 15:25:49

标签: r ggplot2

我有这张图。enter image description here

然后我需要为每个彩色点添加标签。红色的应该被标记为历史性的,而绿色的应该被标记为预测的。我还需要添加标题。

这是数据框的一部分:

> dput(df[1:100,])
structure(list(x = 1:100, historic = c(8314, 8977, 7610, 8864, 
8107, 3878, 5080, 5702, 5414, 5600, 5798, 4258, 6679, 7909, 8663, 
8625, 9462, 4011, 6316, 6613, 6347, 6180, 6736, 4482, 8149, 7450, 
8311, 13261, 10243, 5156, 6222, 5763, 5448, 6757, 5840, 3891, 
8074, 7537, 8524, 9780, 10818, 4687, 6181, 5656, 6440, 7127, 
6567, 4622, 9685, 9590, 10455, 11594, 4542, 7421, 6475, 5589, 
6443, 4613, 7349, 7682, 8226, 8728, 10040, 4149, 7515, 7003, 
6043, 5797, 6024, 9083, 7705, 9046, 8719, 9488, 6564, 9971, 9484, 
8758, 9447, 4373, 5376, 5570, 5875, 5756, 5238, 3772, 7735, 8336, 
8651, 9274, 9563, 4008, 6343, 6185, 6253, 6830, 7123, 8716, 8546, 
11299), predicted = c(8782.97717620368, 9978.79518175856, 8998.8447166933, 
9056.09292286074, 8986.24861655443, 3472.24622783409, 4228.01539931519, 
4847.81488052988, 5227.44826313378, 5342.60987384682, 5761.73678511575, 
4112.81406962844, 8157.7621663205, 8647.77978551097, 8865.45833183255, 
8830.68780816475, 9201.56353843651, 4074.39005207109, 4842.30468739143, 
5111.00499566978, 5299.26018923085, 5885.29804163049, 6148.14940260017, 
4675.33519126484, 8227.13337281752, 8817.62344329373, 8779.33489500296, 
11770.3576486729, 10726.7899160657, 5040.78543580398, 5401.15275473926, 
5326.63019069626, 5576.60733248636, 6677.55752971976, 6780.93167992754, 
5058.89649127275, 8413.22186694323, 9446.79389070227, 9393.39748620683, 
9885.31660360217, 10339.1814961402, 5356.9665066317, 5637.09805518238, 
5899.04908484247, 6205.73692293216, 6237.47139799524, 6630.52336590047, 
5542.68315773091, 8549.12513770569, 9949.83268235816, 10122.6153647164, 
10057.7814991978, 6570.7377706275, 5569.78521368518, 5469.12013581734, 
5650.09666465588, 6782.0118259475, 5017.65387595842, 8308.74867168439, 
8428.0555435033, 8749.75036471726, 9131.1787228978, 9680.42313011342, 
5251.51538538564, 5439.52623864061, 5776.76172331742, 6190.4309058836, 
6471.22815923168, 4668.44196183706, 8017.24595476556, 8845.96504605688, 
8920.49226341494, 9584.33328549851, 10024.7530182703, 5348.27419073585, 
9642.46137343624, 8995.80779352131, 9230.73151783352, 9161.37900329832, 
4331.7593449963, 4594.80434801074, 5459.45979347046, 5270.88707851976, 
5526.33825400275, 6132.27830540825, 4958.25238875257, 8202.35653156834, 
8873.47138770739, 9102.81421397787, 9125.95108024552, 9499.41782101306, 
4842.29670202133, 5509.66417660203, 5541.57725277566, 6126.81095205261, 
6760.08508879361, 4969.93110704212, 9555.42783499693, 9678.61451504185, 
9919.41063384993)), .Names = c("x", "historic", "predicted"), row.names = c(NA, 
100L), class = "data.frame")

我决定这样,但它不起作用:

g <- ggplot(df, aes(x))
g <- g + geom_point(aes(y=historic), colour="red",label="Historic")
g <- g + geom_point(aes(y=predicted), colour="green",label="Forcasted")
g<- g + scale_color_manual(values = c(historic = '#ff00ff',predicted = '#3399ff'))
g

提前谢谢!

1 个答案:

答案 0 :(得分:1)

你需要将数据轮转换为长格式,如@Jack Brookes所说 - 可以使用tidyr库中的收集功能完成。结果简化了情节:

library(tidyr)

df1 = gather(df, key = Type, value = y, historic, predicted)
g <- ggplot(df1, aes(x, y))
g <- g + geom_point(aes(colour = Type))
g <- g + scale_color_manual(values = c(historic = 'red',predicted = 'green'))
g

enter image description here