Reputation:
My df has two numerical variables and categorical columns. Negative Bars are in white white with the same coloured shape/boundaries as corresponding positive bars. Now I want to set one colour for every bars (e.g blue) and have only B categories in different color (e.g Red) and do all these in ggplotly()
in order to get interactive chart
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 = " ")
Upvotes: 1
Views: 55
Reputation: 18714
You said you wanted to color them the same, which is just the border. Did you mean color, fill, or both?
I changed the fill and color in one layer (the positive layer). This shows you one of many possible methods that you could use to accomplish the uniform color or fill in all but one category.
This way if you wanted only color, only fill, or both you will have enough information to modify it as you see fit. The important part is that when you pick a color, you do this outside of aes
.
ggplot() +
# plot positive with fill and colour based on model
geom_col(aes(value, tidytext::reorder_within(model, value, category),
fill = model, color = model),
color = ifelse(df[df$sign == "pos", ]$model == "B",
"red", "blue"),
fill = ifelse(df[df$sign == "pos", ]$model == "B",
"red", "blue"),
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 = " ") -> plt
ggplotly(plt)
Upvotes: 1