R:如何在barplot中指定条颜色?
我的数据框有2个数值和分类列。负条填充白色,但带有与相应的正杆相同的彩色边界。 现在,我需要拥有具有相同颜色和B类的酒吧,以不同的颜色
df <- data.frame(model = c("A","B","C","D","B","C"),
category = c("origin", "origin","origin","abroad","abroad","abroad"),
pos = c(40,50,45,100,105,80),
neg = c(-10,-5,-4,-16,-7,-2),
Colour = c("chocolate","deeppink4","yellow","steelblue3","deeppink4","yellow"))
Colour <- as.character(df$Colour)
Colour <- c(Colour,"white")
names(Colour) <- c(as.character(df$model),"white")
df <- df %>% pivot_longer(., cols=c('pos','neg'),
names_to = 'sign') %>%
mutate(Groups = paste(category, model),
sign = factor(sign, levels = c("neg", "pos")))
ggplot() +
# plot positive with fill and colour based on model
geom_col(aes(value, tidytext::reorder_within(model, value, category),
fill = model, color = model),
data = df[df$sign == "pos", ],
position = "stack") +
# plot negative with colour from based on model, but fill fixed as "white"
geom_col(aes(value, tidytext::reorder_within(model, value, category),
color = model),
data = df[df$sign == "neg", ],
fill = "white",
position = "stack") +
# the rest is same as OP's code
tidytext::scale_y_reordered() +
labs(fill = "model") +
facet_grid(category ~ ., switch = "y",scales = "free_y") +
theme(axis.text.x = element_text(angle = 90),
strip.background = element_rect(fill = "white"),
strip.placement = "outside",
strip.text.y.left = element_text(angle = 0),
panel.spacing = unit(0, "lines")) +
theme(legend.position="none") +
labs( title = "BarPlot",
subtitle = "changes",
y = " ")
My dataframe has 2 numerical and categorical columns. Negative Bars are filled with white but with with the same coloured borders as corresponding positive bars.
Now I need to have bars with same colour and B category in different colour
df <- data.frame(model = c("A","B","C","D","B","C"),
category = c("origin", "origin","origin","abroad","abroad","abroad"),
pos = c(40,50,45,100,105,80),
neg = c(-10,-5,-4,-16,-7,-2),
Colour = c("chocolate","deeppink4","yellow","steelblue3","deeppink4","yellow"))
Colour <- as.character(df$Colour)
Colour <- c(Colour,"white")
names(Colour) <- c(as.character(df$model),"white")
df <- df %>% pivot_longer(., cols=c('pos','neg'),
names_to = 'sign') %>%
mutate(Groups = paste(category, model),
sign = factor(sign, levels = c("neg", "pos")))
ggplot() +
# plot positive with fill and colour based on model
geom_col(aes(value, tidytext::reorder_within(model, value, category),
fill = model, color = model),
data = df[df$sign == "pos", ],
position = "stack") +
# plot negative with colour from based on model, but fill fixed as "white"
geom_col(aes(value, tidytext::reorder_within(model, value, category),
color = model),
data = df[df$sign == "neg", ],
fill = "white",
position = "stack") +
# the rest is same as OP's code
tidytext::scale_y_reordered() +
labs(fill = "model") +
facet_grid(category ~ ., switch = "y",scales = "free_y") +
theme(axis.text.x = element_text(angle = 90),
strip.background = element_rect(fill = "white"),
strip.placement = "outside",
strip.text.y.left = element_text(angle = 0),
panel.spacing = unit(0, "lines")) +
theme(legend.position="none") +
labs( title = "BarPlot",
subtitle = "changes",
y = " ")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已将
mod_col
添加到DF SO绿色和其他所有紫色。然后使用mod_col
填充
和color
在GEOM _
s中。并添加比例_*_ Identity
使用这些颜色。由 reprex软件包(v2.0.1)
I've added
mod_col
to the df so B's green and everything else purple. Then usedmod_col
for thefill
andcolour
in thegeom_
s. And addedscale_*_identity
to use those colours.Created on 2022-06-02 by the reprex package (v2.0.1)