R 如何在ggplot2条形图中添加图例

R 如何在ggplot2条形图中添加图例,r,ggplot2,R,Ggplot2,我试图在ggplot2中设计一个漂亮的图形,通过使用条形图来显示某些时段的不同值。我得到了图形,但我不能在图形的右侧添加图例。我的数据帧DF有3个变量Month、variable、value。这些变量是另一个数据中melt()函数的结果(我在最后一部分添加了dput()版本的DF)。因此,我的数据帧DF如下所示: Month variable value 1 m2 Power 1258978.9 2 m3 Power 1608317.4 3 m4

我试图在ggplot2中设计一个漂亮的图形,通过使用条形图来显示某些时段的不同值。我得到了图形,但我不能在图形的右侧添加图例。我的数据帧
DF
有3个变量
Month、variable、value
。这些变量是另一个数据中
melt()
函数的结果(我在最后一部分添加了
dput()
版本的
DF
)。因此,我的数据帧
DF
如下所示:

   Month variable     value
1     m2    Power 1258978.9
2     m3    Power 1608317.4
3     m4    Power 1293821.1
4     m5    Power 1819283.8
5     m6    Power 1436552.9
6     m7    Power  875170.3
7     m8    Power 1315856.2
8     m9    Power  710004.3
9    m10    Power  889398.1
10   m11    Power 1114883.1
11   m12    Power 1419242.1
12   m13    Power 1585857.2
13   m14    Power 1010455.6
14   m15    Power 1292333.4
为了显示
value
变量按
month
的变化,我使用了

库(ggplot2)
图书馆(比例尺)
ggplot(DF,aes(x=月,y=值))
+几何图形条(position=“identity”,fill=“#FF6C91”,color=“black”,size=1)
+连续缩放(
标签=逗号,
休息=漂亮的休息(n=7),
限制=c(0,最大值(DF$值,na.rm=T))
)
+主题(
axis.text.x=元素\文本(角度=90,颜色=grey20,面=bold,尺寸=12),
axis.text.y=元素\文本(color=“grey20”,face=“bold”,hjust=1,vjust=0.8,size=15),
axis.title.x=元素\文本(color=“grey20”,face=“bold”,size=16),
axis.title.y=元素\文本(color=“grey20”,face=“bold”,size=16)
)
+xlab(“月”)
+ylab(“”)
+ggtitle(“我的图表”)
+主题(plot.title=element\u text(线宽=3,面=bold,色=black,尺寸=24))
+主题(legend.text=元素\文本(大小=14),legend.title=元素\文本(大小=14))
通过这段代码,我得到了下一个图形:

结果几乎完美,但我不知道如何在图的右侧添加具有相同颜色的条的图例,以在该图像中提供更多信息。我试图在
geom_bar
中添加
fill
参数,但无法得到我想要的结果。下一个版本是
DF
dput()

DF=structure(list(Month = c("m2", "m3", "m4", "m5", "m6", "m7", 
"m8", "m9", "m10", "m11", "m12", "m13", "m14", "m15"), variable = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Power", class = "factor"), 
    value = c(1258978.86, 1608317.42, 1293821.14, 1819283.78, 
    1436552.93, 875170.34, 1315856.24, 710004.35, 889398.08, 
    1114883.11, 1419242.11, 1585857.22, 1010455.56, 1292333.35
    )), .Names = c("Month", "variable", "value"), row.names = c(NA, 
-14L), class = "data.frame")

非常感谢您的帮助。

您只需制作
aes
thetic映射的填充部分:

gg <- ggplot(DF, aes(x=Month, y=value))
gg <- gg + geom_bar(stat="identity", aes(fill=variable), colour="black", size=1)
gg <- gg + scale_y_continuous(labels=comma, breaks=pretty_breaks(n=7),
                              limits=c(0, max(DF$value,na.rm=T)))
gg <- gg + scale_fill_manual(values="#FF6C91", name="Legend name")
gg <- gg + labs(x="Month", y="", title="My graph")
gg <- gg + theme(plot.title=element_text(lineheight=3, face="bold", color="black", size=24))
gg <- gg + theme(legend.text=element_text(size=14), legend.title=element_text(size=14))
gg <- gg + theme(axis.text.x=element_text(angle=90, colour="grey20", face="bold", size=12), 
                 axis.text.y=element_text(colour="grey20", face="bold", hjust=1, vjust=0.8, size=15),
                 axis.title.x=element_text(colour="grey20", face="bold", size=16),
                 axis.title.y=element_text(colour="grey20", face="bold", size=16))
    gg

gg您需要将
fill
映射到一种美学,以强制创建一个传奇。从那里,您可以使用
scale\u fill\u manual

下面是代码(注意,我做了一个月的系数,所以x轴是有序的,或者至少是我认为您想要的顺序):


DF$Month2正如其他人所说,您需要
aes
中指定的
fill
。这里是另一个版本,有几个进一步的选项供您考虑(图例名称、标签和位置等):

如图所示:

您只有一种颜色的条形图为什么需要图例?真正的df在
变量中会有更多的级别吗?@user1317221_G-我将这个问题解释为OP想要“使用”该空间提供的辅助信息可能与数据本身没有直接关系…但主要是我的猜测。我很好奇为什么这会产生两次否决票…用户提供的代码,一个可复制的示例,并明确说明了预期的结果…可能是因为OP显然从未谷歌搜索过“ggplot2图例”
DF$Month2 <- factor(DF$Month, levels = paste0("m", 2:15))

ggplot(DF, aes(x = Month2, y = value, fill = variable)) +
  geom_bar(stat="identity",colour="black",size=1) + #NOTE - you need stat = identity, not position
  scale_y_continuous(labels = comma,breaks=pretty_breaks(n=7),
                     limits=c(0,max(DF$value,na.rm=T))) +
  theme(axis.text.x=element_text(angle=90,colour="grey20",face="bold",size=12),
        axis.text.y=element_text(colour="grey20",face="bold",hjust=1,vjust=0.8,size=15),
        axis.title.x=element_text(colour="grey20",face="bold",size=16),
        axis.title.y=element_text(colour="grey20",face="bold",size=16)) +
  xlab('Month')+ylab('')+ ggtitle("My graph") +
  theme(plot.title = element_text(lineheight=3, face="bold", color="black",size=24)) +
  theme(legend.text=element_text(size=14),
        legend.title=element_text(size=14)) +
  scale_fill_manual(name = "This is stuff I made up", 
                    label = "This is stuff I made up II", 
                    values = "#FF6C91")
ForLeg=c(Power="#FF6C91");
    ggplot(DF, aes(x = Month, y=value, fill=as.factor(variable)))+
      geom_bar(stat="Identity",colour="black",size=1)+ 
      scale_fill_manual(values=ForLeg, labels="The value")+
      scale_y_continuous(labels = comma,breaks=pretty_breaks(n=7),limits=c(0,max(DF$value,na.rm=T)))+
      theme(axis.text.x=element_text(angle=90,colour="grey20",face="bold",size=12),axis.text.y=element_text(colour="grey20",face="bold",hjust=1,vjust=0.8,size=15),axis.title.x=element_text(colour="grey20",face="bold",size=16),axis.title.y=element_text(colour="grey20",face="bold",size=16))+
      xlab('Month')+
      ylab('')+ 
      ggtitle("My graph")+
      theme(plot.title = element_text(lineheight=3, face="bold", color="black",size=24))+
      theme(legend.position="top",legend.text=element_text(size=14),legend.title=element_text(size=14))+
      guides(fill=guide_legend(title="",keywidth = 2, keyheight = 1,title.position = "left",title.hjust = -0.28))