在r中用ggplot2更改geom_bar中的条形图颜色 [英] Change bar plot colour in geom_bar with ggplot2 in r
本文介绍了在r中用ggplot2更改geom_bar中的条形图颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
c1 < - c(10,20, (c 1,c 2,...,c 1,c 1,c 1,c 2,c 4,c 4,c 4,c 5,c 7, c3)
ggplot(data = df,aes(x = c1 + c2 / 2,y = c3))+
geom_bar(stat =identity,width = c2)+
scale_fill_manual (values = c(#FF6666))
我最终只有灰色条:
如果您想手动更改颜色,请使用
scale_fill_manual()
取决于分类变量。 c4 = c(A,B,C)
df = cbind(df,c4)
ggplot(data = df,aes(x = c1 + c2 / 2,y = c3,fill = c4))+
geom_bar(stat =identity, width = c2)
ggplot(data = df,aes(x = c1 + c2 / 2,y = c3,fill = c4))+
geom_bar scale =identity,width = c2)+
scale_fill_manual(legend,values = c(A=black,B=orange ,C=blue))
I have the following in order to bar plot the data frame.
c1 <- c(10, 20, 40)
c2 <- c(3, 5, 7)
c3 <- c(1, 1, 1)
df <- data.frame(c1, c2, c3)
ggplot(data=df, aes(x=c1+c2/2, y=c3)) +
geom_bar(stat="identity", width=c2) +
scale_fill_manual(values=c("#FF6666"))
I end up having only grey bars: Grey bars for bar plot
I would like to change the color of the bar. I already tried different scale_fill_manual from http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/ but still have grey bars.
Thank you for your help.
解决方案
If you want all the bars to get the same color (fill), you can easily add it inside geom_bar
.
ggplot(data=df, aes(x=c1+c2/2, y=c3)) +
geom_bar(stat="identity", width=c2, fill = "#FF6666")
Use scale_fill_manual()
if you want to manually the change of colors depending of a categorical variable.
c4 = c("A", "B", "C")
df = cbind(df, c4)
ggplot(data=df, aes(x=c1+c2/2, y=c3, fill = c4)) +
geom_bar(stat="identity", width=c2)
ggplot(data=df, aes(x=c1+c2/2, y=c3, fill = c4)) +
geom_bar(stat="identity", width=c2) +
scale_fill_manual("legend", values = c("A" = "black", "B" = "orange", "C" = "blue"))
这篇关于在r中用ggplot2更改geom_bar中的条形图颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文