R 无法在ggplot中自定义图例

R 无法在ggplot中自定义图例,r,ggplot2,legend,R,Ggplot2,Legend,我正在使用ggplot(下面是我的代码)绘制年度需求,但我无法为绘图添加颜色图例。我的data.frame有“Zone”和“TotalDemand”(只有两列),我有三个data.frame,为期三年(“sales12”、“sales13”和“sales14”) 请帮我找出我的错误。通过一个非常小的数据帧示例df,我将其融化,以格式化为ggplot dput(df) structure(list(Zone = structure(1:4, .Label = c("Alpha", "Baker",

我正在使用
ggplot
(下面是我的代码)绘制年度需求,但我无法为绘图添加颜色图例。我的data.frame有“Zone”和“TotalDemand”(只有两列),我有三个data.frame,为期三年(“sales12”、“sales13”和“sales14”)


请帮我找出我的错误。

通过一个非常小的数据帧示例df,我将其融化,以格式化为
ggplot

dput(df)
structure(list(Zone = structure(1:4, .Label = c("Alpha", "Baker", 
"Charlie", "Delta"), class = "factor"), TotalDemand = c(90L, 
180L, 57L, 159L), sales12 = c(25L, 40L, 13L, 50L), sales13 = c(30L, 
60L, 16L, 55L), sales14 = c(35L, 80L, 28L, 54L)), .Names = c("Zone", 
"TotalDemand", "sales12", "sales13", "sales14"), class = "data.frame", row.names = c(NA, 
-4L))

df.m <- melt(df, id.vars = "Zone", measure.vars = c("sales12", "sales13", "sales14"))

ggplot(df.m, aes(x=factor(Zone), y=value, color = variable )) + 
  geom_point(size=6, shape=17) +
  labs(y='Demand (in 1000s)',x='Zones') +
  scale_colour_manual(values = c('green', 'black', 'red'))
dput(df)
结构(列表)区域=结构(1:4,.Label=c(“Alpha”,“Baker”,
“Charlie”,“Delta”,class=“factor”),总需求=c(90L,
180L,57L,159L),sales12=c(25L,40L,13L,50L),sales13=c(30L,
60L,16L,55L),sales14=c(35L,80L,28L,54L)),.Names=c(“区域”,
“TotalDemand”、“sales12”、“sales13”、“sales14”),class=“data.frame”,row.names=c(NA,
-(4升)

我的问题可能不清楚,所以它得到了否决票。我已经用了三种颜色三年了,我不能把它们作为传奇。在将我的问题发布到这里之前,我尝试查看了相关帖子上提供的帮助你不必像那样三次灌水
geom_point
。为年份创建一个变量,在aes中,您可以使用该变量来计算分数。这是非常基本的情节。这表明你在发布这个问题之前没有读过任何东西。当然,如果你能提供一个可复制的例子,你会得到一个答案。如果你想要一个图例,你必须在
aes
中将某些东西映射到颜色。
dput(df)
structure(list(Zone = structure(1:4, .Label = c("Alpha", "Baker", 
"Charlie", "Delta"), class = "factor"), TotalDemand = c(90L, 
180L, 57L, 159L), sales12 = c(25L, 40L, 13L, 50L), sales13 = c(30L, 
60L, 16L, 55L), sales14 = c(35L, 80L, 28L, 54L)), .Names = c("Zone", 
"TotalDemand", "sales12", "sales13", "sales14"), class = "data.frame", row.names = c(NA, 
-4L))

df.m <- melt(df, id.vars = "Zone", measure.vars = c("sales12", "sales13", "sales14"))

ggplot(df.m, aes(x=factor(Zone), y=value, color = variable )) + 
  geom_point(size=6, shape=17) +
  labs(y='Demand (in 1000s)',x='Zones') +
  scale_colour_manual(values = c('green', 'black', 'red'))