Ilias Moysidis
Ilias Moysidis

Reputation: 23

How to set the order for the fill variable in ggplot2 for R

I want to know if there is a way for defining the order of appearance per group for the side by side barplot in ggplot. My dataset is

> df
        V1         V2   V3
1     True      Joint 0.18
2     True Individual 0.82
3 Estimate      Joint 0.37
4 Estimate Individual 0.63

The following line of code

library(ggplot2)

ggplot(df, aes(x = V2, y = V3, fill = V1)) +
  geom_col(position = position_dodge2()) +
  scale_fill_manual(values=c("steelblue","coral2")) +
  guides(fill = guide_legend(reverse = T)) +
  scale_x_discrete(limits = unique(df$V2)) +
  theme(legend.title = element_blank()) +
  xlab("Structure") + ylab("Variance") +
  ggtitle("Variance Explained by Joint and Individual structure")

gives me this graph side-by-side-barplot.

I want the coral-colored bar to be on the left and the blue bar on the right for each group. I have tried reordering the dataset with lines of code such as

df = arrange(df, df$V1)
df = df[order(df$V1, decreasing = F),]
df = df[c(1,3,2,4),]
df = df[c(1,4,3,2),]

but they did not work.

Upvotes: 1

Views: 1945

Answers (2)

TarJae
TarJae

Reputation: 79286

You could simply add the reverse = TRUE argument to position_dodge2:

ggplot(df, aes(x = V2, y = V3, fill = V1)) +
  geom_col(position = position_dodge2(reverse = TRUE)) +
  scale_fill_manual(values=c("steelblue","coral2")) +
  guides(fill = guide_legend(reverse = T)) +
  scale_x_discrete(limits = unique(df$V2)) +
  theme(legend.title = element_blank()) +
  xlab("Structure") + ylab("Variance") +
  ggtitle("Variance Explained by Joint and Individual structure")

enter image description here

Upvotes: 1

stefan
stefan

Reputation: 125797

You could achieve your desired result by converting to a factor and setting the levels in your desired order:

Additionally I made use of a named vector to assign the colors to the categories. But that is just a matter of personal preference.

library(ggplot2)

ggplot(df, aes(x = V2, y = V3, fill = factor(V1, levels = c("True", "Estimate")))) +
  geom_col(position = position_dodge2()) +
  scale_fill_manual(values = c(Estimate = "steelblue", True = "coral2")) +
  guides(fill = guide_legend(reverse = T)) +
  scale_x_discrete(limits = unique(df$V2)) +
  theme(legend.title = element_blank()) +
  xlab("Structure") +
  ylab("Variance") +
  ggtitle("Variance Explained by Joint and Individual structure")

DATA

df <- structure(list(
  V1 = c("True", "True", "Estimate", "Estimate"),
  V2 = c("Joint", "Individual", "Joint", "Individual"), V3 = c(
    0.18,
    0.82, 0.37, 0.63
  )
), class = "data.frame", row.names = c(
  "1",
  "2", "3", "4"
))

Upvotes: 3

Related Questions