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.
我想改变酒吧的颜色。我已经尝试过来自http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/的不同scale_fill_manual,但仍然有灰色条。
Thank you for your help.
感谢您的帮助。
1 个解决方案
#1
24
If you want all the bars to get the same color (fill), you can easily add it inside geom_bar
.
如果您希望所有条形图获得相同的颜色(填充),您可以轻松地将其添加到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.
如果要根据分类变量手动更改颜色,请使用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(stat="identity", width=c2) +
scale_fill_manual("legend", values = c("A" = "black", "B" = "orange", "C" = "blue"))
#1
24
If you want all the bars to get the same color (fill), you can easily add it inside geom_bar
.
如果您希望所有条形图获得相同的颜色(填充),您可以轻松地将其添加到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.
如果要根据分类变量手动更改颜色,请使用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(stat="identity", width=c2) +
scale_fill_manual("legend", values = c("A" = "black", "B" = "orange", "C" = "blue"))