Reputation: 45
I would like to have a multi-bar chart, horizontal bars, where the legend for the bar-color is in the same order as the bars themselves. I've looked at several articles that have influenced the example here, but I can't make it work.
In this example, it is the "Group" that is in one order in the bars, and the reverse order in the legend.
library(ggplot2)
# create data for tidy format
Category <- c("Category1","Category2","Category3","Category4","Category5","Category1","Category2","Category3","Category4","Category5","Category1","Category2","Category3","Category4","Category5")
GroupTitle <- c("GroupA","GroupA","GroupA","GroupA","GroupA","GroupB","GroupB","GroupB","GroupB","GroupB","GroupC","GroupC","GroupC","GroupC","GroupC")
Valuelst <- list()
for (i in 1:15){
Valuelst[i] <- runif(1, min=0, max=1)
}
Valuelst <- unlist(Valuelst)
# make data frame
d <- data.frame(Category,GroupTitle,Valuelst)
# set factors and orders desired
d$Category <- factor(d$Category, levels = c("Category5","Category4","Category3","Category2","Category1"))
d$GroupTitle <- factor(d$GroupTitle, levels = c("GroupA","GroupB","GroupC"))
# make graph
ggplot(d, aes(x=Category, y=Valuelst, order = -as.numeric(GroupTitle))) + # order= -as.numeric() is one solution that I read
geom_bar(aes(fill=GroupTitle), stat="identity", position="dodge") +
coord_flip() +
scale_fill_manual("Legenda", values = c("GroupC" = "#deebf7", "GroupB" = "#3182bd", "GroupA" = "#9ecae1")) # scale_fill_manual is another I read
I've commented in the code a couple of places where I tried solutions I saw elsewhere. Namely, I've made sure the Groups are a factor, set the order of that factor, used order = -as.numeric(GroupTitle)
, and tried scale_fill_manual
, all with no effect.
Upvotes: 3
Views: 1984
Reputation: 1103
I used ggpubr
library. The trick is the -0.7
for position_dodge()
, which make the bars reversed. You can also use
library(ggplot2)
library(ggpubr)
# create data for tidy format
Category <- paste0("Category", rep(1:5,3))
GroupTitle <- paste0("Group", rep(LETTERS[1:3], each=5))
Valuelst <- runif(15, min=0, max=1)
# make data frame - factor is default for strings
d <- data.frame(Category, GroupTitle, Valuelst)
# make graph
ggbarplot(d,x="Category", y="Valuelst", fill="GroupTitle", legend = "right",
orientation = "horiz", position=position_dodge(-.7),
order = c("Category5","Category4","Category3","Category2","Category1")) +
scale_fill_manual("Legenda", values = c("GroupC" = "#deebf7", "GroupB" = "#3182bd", "GroupA" = "#9ecae1"))
You can also use position=position_dodge(-.9)
in geom_bar()
to achieve the same effect.
ggplot(d, aes(x=Category, y=Valuelst, order = -as.numeric(GroupTitle))) +
geom_bar(aes(fill=GroupTitle), stat="identity", position=position_dodge(-.9)) +
coord_flip() +
scale_fill_manual("Legenda", values = c("GroupC" = "#deebf7", "GroupB" = "#3182bd", "GroupA" = "#9ecae1"))
Upvotes: 1
Reputation: 13570
Option 1 - Order: C, B and A
To reverse the legend labels you only need to add: guides(fill = guide_legend(reverse = TRUE))
. Keeping the original order of the bars: C, B and A.
ggplot(d, aes(x = Category,
y = Valuelst,
fill = GroupTitle)) +
geom_bar(stat = "identity", position = "dodge") +
coord_flip() +
scale_fill_manual("Legenda",
values = c(
"GroupC" = "#deebf7",
"GroupB" = "#3182bd",
"GroupA" = "#9ecae1"
)) +
guides(fill = guide_legend(reverse = TRUE))
Option 2 - Order: A, B and C
To reverse the order of the bars, we just reorder the levels before plotting.
d$GroupTitle <- factor(d$GroupTitle, levels = c("GroupC","GroupB","GroupA"))
# make graph
ggplot(d, aes(x = Category,
y = Valuelst,
fill = GroupTitle)) +
geom_bar(stat = "identity", position = "dodge") +
coord_flip() +
scale_fill_manual("Legenda",
values = c(
"GroupC" = "#deebf7",
"GroupB" = "#3182bd",
"GroupA" = "#9ecae1"
)) +
guides(fill=guide_legend(reverse=TRUE))
Upvotes: 3
Reputation: 675
You were almost there with scale_fill_manual
.
scale_fill_manual("Legenda",
values = c("GroupA" = "#9ecae1", "GroupB" = "#3182bd", "GroupC" = "#deebf7"),
breaks = c("GroupC", "GroupB", "GroupA"))
The breaks
argument will set the order.
Upvotes: 2