这是一个挑剔的问题,但我在图表的图例中显示一条线的颜色时遇到了问题。在“Sabry Lee”旁边的框中,颜色应该是蓝色,但由于某种原因,它是空白的。
以下是数据:
df2 <- structure(list(STRING = c("1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC"), time = structure(c(13239, 13422,
13483, 13545, 13573, 13604, 13634, 13665, 13695, 13726, 13757,
13787, 13818, 13848, 13879, 13910, 13939, 13970, 14000, 14031,
14061, 14092, 14123, 14153, 14184, 14214, 14245, 14276, 14304,
14335), class = "Date"), ee_qty = c(NA, 2302, 3434, 1800, 2766,
4408, 6314, 2860, 2476, 1572, 4950, 1812, 14100, 7591, NA, NA,
11823, NA, NA, 11829, 3836, 5378, 4804, 4356, 3655, 5955, 1345,
4717, 8854, 11494), ee_amt = c(NA, 33815.88, 60107.4, 66279.2,
66814.46, 129042.18, 151649.49, 58576.2, 61984.58, 61782, 102648.16,
67844.4, 362371.82, 159274.66, NA, NA, 285847.62, NA, NA, 258045.26,
82306.74, 119702.9, 113514.6, 118765.82, 86298.66, 155659.83,
40246.14, 134675.59, 236084.63, 256467.56), elite_qty = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1281, 3037, 2141, 1764, 1661,
1838, 2780, 2627, 2498, 3137, 2238, 2470, 1107, 1886, 844, 798,
NA, 1588, 1919, 2603), elite_amt = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 34956.08, 48618.37, 58880.59, 49141.23, 49067.42,
40105.91, 89717.69, 78612.5, 40608.3599999999, 65893.38, 33706.66,
77191.76, 45096.9, 55742.83, 29993.7600000000, 27171.22, NA,
1427.23, 2466.64, 54500.19), Sabry_Qty = c(596, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), Sabry_amt = c(17761.06,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), depo_qty = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
902, 319, 883, 597, 854, 710, 911, 573, 447, 857, 795, 997, 822
), depo_amt = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 31011.68, 13556.65, 32134.72, 23106.94, 36681.95,
30220.74, 38051.93, 24941.85, 19337.46, 31360.4, 30182.18, 41363.71,
34419.34), tyc_qty = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 58, 116, 136), tyc_amt = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 3103.73, 7538.71, 7497.34), string2 = c("1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC")), .Names = c("STRING",
"time", "ee_qty", "ee_amt", "elite_qty", "elite_amt", "Sabry_Qty",
"Sabry_amt", "depo_qty", "depo_amt", "tyc_qty", "tyc_amt", "string2"
), row.names = c(NA, 30L), class = "data.frame")
制作情节的代码:
p = ggplot(df2)
p = p +
geom_line(aes(time, ee_amt, colour="ee_amt"), size = 2) +
geom_point(aes(time, ee_amt, colour="ee_amt"), size = 2) +
geom_line(aes(time, elite_amt, colour="elite_amt"), size = 2) +
geom_point(aes(time, elite_amt, colour="elite_amt"), size = 2) +
geom_line(aes(time, Sabry_amt, colour="Sabry_amt"), size = 2) +
geom_point(aes(time, Sabry_amt, colour="Sabry_amt"), size = 2) +
geom_line(aes(time, depo_amt, colour="depo_amt"), size = 2) +
geom_point(aes(time, depo_amt, colour="depo_amt"), size = 2) +
geom_line(aes(time, tyc_amt, colour="tyc_amt"), size = 2) +
geom_point(aes(time, tyc_amt, colour="tyc_amt"), size = 2) +
ylab("Revenue (Dollars)") +
xlab("Time") +
labs(colour = "") +
opts(legend.position="right") +
opts(title = "Stuff") +
scale_color_manual(name ="",
values = c("red", "green", "blue", "purple", "yellow"),
breaks=c("ee_amt", "elite_amt", "Sabry_Amt", "depo_amt", "tyc_amt"),
labels=c("Eagle Eyes", "E-lite", "Sabry Lee", "Depo", "TYC"))
答案 0 :(得分:4)
你这样做是错误的。首先melt
您的数据,然后使用它让ggplot
为您完成工作,这样您就不必对geom_line
和geom_point
进行5次调用。从级别获得休息通常更安全,更少打字,更不容易出错。
library(reshape2)
df2M <- melt(df2, measure.vars = c("ee_amt", "elite_amt", "Sabry_amt", "depo_amt", "tyc_amt"))
ggplot(df2M, aes(time, value, colour = variable, group = variable)) +
geom_line(size = 2) +
geom_point(size = 2) +
labs(colour = "", x = "Time", y = "Revenue (Dollars)") +
opts(legend.position="right", title = "Stuff") +
scale_color_manual(name ="", values = c("red", "green", "blue", "purple", "yellow"),
breaks= levels(df2M$variable),
labels=c("Eagle Eyes", "E-lite", "Sabry Lee", "Depo", "TYC"))