如何在ggplot2条形图中添加图例 [英] How to add legend in a ggplot2 bar chart
问题描述
我试图在ggplot2中设计一个不错的图表,通过使用小节来显示某些时段的不同值。我得到了图形,但是我无法在图表的正面添加图例。我的数据框 DF
有3个变量月,变量,值
。这些变量是另一个数据中 melt()
函数的结果(我添加了 dput()
版本的 DF
在最后一部分)。所以,我的数据框 DF
看起来像这样:
月变量值
1平方米功率1258978.9
2 m3功率1608317.4
3 m4功率1293821.1
4 m5功率1819283.8
5 m6功率1436552.9
6 m7功率875170.3
7 m8功率1315856.2
8 m9功率710004.3
9 m10功率889398.1
10 m11功率1114883.1
11 m12功率1419242.1
12 m13功率1585857.2
13 m14 Power 1010455.6
14 m15 Power 1292333.4
为了显示值
变量由 month
我使用这段代码:
library(ggplot2)
library(scales)
ggplot(DF,aes(x = Month,y = value))+ geom_bar(position =identity,fill =#FF6C91 ,color =black,size = 1)+ scale_y_continuous(labels =逗号,break = pretty_breaks(n = 7),limits = c(0,max(DF $ value,na.rm = T))+
主题(axis.text.x = element_t分机(角度= 90,颜色= grey20,面部= 黑体,大小= 12),axis.text.y = element_text(颜色= grey20,面部= 黑体,hjust = 1,vjust = 0.8 ,大小= 15),axis.title.x = element_text(颜色= grey20,面部= 黑体,大小= 16),axis.title.y = element_text(颜色= grey20,面部= 黑体 ,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))
通过此代码,我得到了下图:
结果几乎完美,但我不知道如何在图的右侧添加图例的相同颜色的图例此图片中的更多信息。我试图在 geom_bar
内添加 fill
参数,但是我无法得到我想要的结果。 dput()
版本的 DF
是下一个:
DF = structure(list(Month = c(m2,m3,m4,m5,m6,m7,
m1,m9,m10,m11,m12,m13,m14,m15),variable = structure(c(1L,
1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L),。标签=功率,等级=因子),
值= 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)
非常感谢您的帮助。
您只需填写<$ c
$ b
gg < - ggplot(DF,aes(x =月,y =值))
gg< - gg + geom_bar(stat =identity,aes(fill = variable),color =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图表)
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, color =grey20,face =bold,size = 12),
axis.text.y = element_text(color =grey20,face =bold,hjust = 1,vjust = 0.8,size = 15),
axis.title.x = element_text(color =grey20,face =bold,size = 16),
axis.title.y = element_text(color =grey20 ,face =bold,size = 16))
gg
注意:我将 position =identity
更改为 stat =identity
(我认为你真的只是错误的粘贴,因为你确实得到它的工作),重新构造ggplot,使其更容易修改(程序员的偏好,不必使其工作),然后在填充美学映射后,手动缩放变量(并且您也可以在那里重新命名图例标题)。这是自动启用图例的映射。我还投入了一个 theme_bw()
调用好的度量(并且核实灰色背景)。
我会将x轴标签旋转回水平,但并不想强加这一点。通常不建议让人们倾斜头部阅读标签,但由于它们只是 m
的数字和数字,所以它可能不会那么难。
除非需要按照它们在x轴上的方式排序(即<$ c $),否则可能需要对条进行排序(从低到高或反之亦然) c> m10 - m9
顺便说一句,现在是重要的)。
I am trying to design a nice graph in ggplot2 by using bars to show different values for some periods. I got the graphic but I can't add the legend in the rigth side of the graph. My dataframe DF
has 3 variables Month,variable,value
. These variables are the result of melt()
function in another data (I add the dput()
version of DF
in the final part). So, my dataframe DF
looks like this:
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
To show the evolution of value
variable by month
I used this code:
library(ggplot2)
library(scales)
ggplot(DF, aes(x = Month, y = value))+geom_bar(position="identity",fill="#FF6C91",colour="black",size=1)+ 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))
With this code I got the next graphic:
The result is almost perfect but I don't know how to add the legend in the right side of the graph with the same color of bars to give more information in this image. I have tried to add fill
argument inside geom_bar
but I can't get my wished result. The dput()
version of DF
is the next:
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")
Many thanks for your help.
You just need to make the fill part of the aes
thetic mapping:
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
NOTE: I changed the position="identity"
to stat="identity"
(I think you prbly just mis-pasted since you did get it working), re-structured the ggplot to make it easier to modify (programmer's preference, not necessary to make it work) and then did a manual scale after mapping the fill aesthetic to the variable (and you can rename the legend title there, too, as demonstrated). It's that mapping which automagically enable the legend. I also threw in a theme_bw()
call for good measure (and to nuke the grey background).
I would have rotate the x-axis labels back to horizontal, but didn't want to impose that as well. Making folks tilt their heads to read labels is usually not recommended, but since they are just m
's and digits, it probably isn't going to be that hard.
It might be worth sorting the bars (lowest to highest or vice versa) unless you need them ordered the way they are on the x-axis (i.e. of the m10
- m9
order the way it is now is important).
这篇关于如何在ggplot2条形图中添加图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!