我有这些数据,我试图使用自定义颜色
School <- c('School1','School1','School1','School1','School1','School1','School1','School1','School2','School2','School2','School2','School3','School3','School3','School3')
Condition <- c('2','1','4','3','2','1','4','3','2','4','2','4','2','1','2','1')
Count <- c(136,465,2,45,652,123,205,9,392,157,201,111,298,888,254,709)
Year <- c('2009','2009','2009','2009','2010','2010','2010','2010','2009','2009','2010','2010','2009','2009','2010','2010')
acronym <- c('S1','S1','S1','S1','S1','S1','S1','S1','S2','S2','S2','S2','S3','S3','S3','S3')
df <- data.frame(School, Condition, Count, Year, acronym)
我使用ggplot来获得完美的图表,但我想知道如何编辑它以使用我公司的标准颜色
test <- ggplot(df, aes(x=Year, y=Count, fill=Condition)) +
geom_bar(stat="identity", position = "fill") + scale_y_continuous(labels = percent_format()) + facet_grid(~acronym) +
theme_bw() + labs(title="Chart Title")
test
我有一个名为colors的矢量:
colors
[1] "#101820" "#AF272F" "#EAAA00" "#D0D0CE"
我尝试用它绘制图形:
test <- ggplot(df, aes(x=Year, y=Count, fill=Condition, color = colors
)) +
geom_bar(stat="identity", position = "fill") + scale_y_continuous(labels = percent_format()) + facet_grid(~acronym) +
theme_bw() + labs(title="Chart Title")
并出现意外错误。有人可以帮忙吗?
答案 0 :(得分:2)
至少有两种可能性:
只需添加function price (city, item) {
let cities = {
city1 : {coffee:0.5, water:0.8, beer:1.2, sweets:1.45, peanuts:1.6},
city2 : {coffee:0.4, water:0.9, beer:1.4, sweets:1.25, peanuts:1.8}
}
let res = cities[city][item]
console.log(res)
return res;
}
price ("city1", "water")
:
+ scale_fill_manual(values = colors)
或者,您可以向数据框添加具有已定义颜色的特殊列:
ggplot(df, aes(x=Year, y=Count, fill=Condition)) +
geom_bar(stat="identity", position = "fill") +
scale_y_continuous(labels = percent_format()) +
facet_grid(~acronym) +
theme_bw() +
labs(title="Chart Title") +
scale_fill_manual(values = colors) # added