[英]Changing default ggplot colours of a dodged bar chart
I have the following data frame:我有以下数据框:
from variable value
1 ASM cent.degree 0.208333333
2 AUS cent.degree 0.958333333
3 COK cent.degree 0.166666667
4 FJI cent.degree 0.916666667
5 FSM cent.degree 0.208333333
6 GUM cent.degree 0.208333333
26 ASM cent.betweenness 0.000000000
27 AUS cent.betweenness 0.588500000
28 COK cent.betweenness 0.000000000
29 FJI cent.betweenness 0.509333333
30 FSM cent.betweenness 0.001666667
31 GUM cent.betweenness 0.001666667
And the following code:以及以下代码:
geom_bar(stat='identity', position='dodge') +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
theme(legend.position="bottom") +
ggtitle("Központiság mutatók") +
#scale_y_continuous(trans = "log2") +
scale_x_discrete(name = "Származás") +
scale_fill_discrete(name = NULL, labels = c("Degree centrality (Népszerűség)",
"Closeness centrality (Befolyás)",
"Betweenness centrality (Alkuerő)",
"Sajátvektor központiság (Státusz)"))
It provides a side-by-side bar chart of four variables in total, differentiating the colour based on which variable it is.它提供了一个总共四个变量的并排条形图,根据它是哪个变量来区分颜色。 The thing I'm having trouble with is changing the colours (to four new colors).
我遇到的问题是更改颜色(更改为四种新颜色)。 I tried adding
scale_fill_manual(values = oc.color[1:4]) +
(where oc.color
is a vector of hexacodes), but it says "Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale."我尝试添加
scale_fill_manual(values = oc.color[1:4]) +
(其中oc.color
是十六进制向量),但它说“'填充'的比例已经存在。为'填充'添加另一个比例,这将取代现有的规模。” , and continues to use the default ggplot colors. ,并继续使用默认的ggplot colors。
Thanks for any help.谢谢你的帮助。
You probably added the scale_fill_manual
before scale_fill_discrete
.您可能在
scale_fill_manual
之前添加了scale_fill_discrete
。 This way scale_fill_discrete
will replace your colors with default colors.这样
scale_fill_discrete
将用默认的 colors 替换您的 colors。 Best and the recommended way is to use only one scale per aesthetic.最好和推荐的方法是每种美学只使用一个比例。 Try this:
尝试这个:
library(ggplot2)
oc.color <- c("red", "blue", "green", "pink")
ggplot(df, aes(from, value, fill = variable)) +
geom_bar(stat='identity', position='dodge') +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
theme(legend.position="bottom") +
ggtitle("Központiság mutatók") +
scale_x_discrete(name = "Származás") +
scale_fill_manual(name = NULL, labels = c("Degree centrality (Nepszerűség)",
"Closeness centrality (Befolyás)",
"Betweenness centrality (Alkuerő)",
"Sajátvektor központiság (Státusz)"), values = oc.color[1:4])
This is working in my system:
这在我的系统中工作:
oc.color <- c("#999999", "#E69F00", "#56B4E9","#56B0E9");
df %>% ggplot(aes(from, value)) +
geom_bar(stat='identity', position='dodge', aes(fill = variable)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
theme(legend.position="bottom") +
ggtitle("Központiság mutatók") +
#scale_y_continuous(trans = "log2") +
scale_x_discrete(name = "Származás") +
scale_fill_discrete(name = NULL, labels = c("Degree centrality (Népszerűség)",
"Closeness centrality (Befolyás)",
"Betweenness centrality (Alkuerő)",
"Sajátvektor központiság (Státusz)")) +
scale_fill_manual(values = oc.color[1:4]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.