-1

enter image description hereenter image description hereI wanted to make a bar graph with ggplot. I specify the order and colors for "fill" but one bar's order and color do not change. Here are my codes:

ggplot(data) +
  geom_col(mapping = aes(x = Groups, y = Ratio, fill = Domains), position = "dodge") +
  scale_color_manual(values = c(C1 = "pink", V1 = "blue", V2 = "maroon", C2 = "orange", 
                                V3 = "violet", C3 = "green", V4 = "brown", C4 = "sky blue", 
                                V5 = "red", C5 = "yellow", GP41 = "tan"), aesthetics = c("colour", "fill"))
data$Domains <- factor(data$Domains, levels = c("C1", "V1", "V2", "C2", "V3", "C3", "V4", "C4", "V5", "C5", "GP41"))

Although C5's color in the label is marked by yellow but it does not get changed in the graph and it comes after GP41. Only this bar has an issue.

enter image description here

Elmira
  • 1
  • 2
  • 3
    Welcome to SO! To help us to help you would you mind sharing [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. To share your data, you could type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 10))` for the first ten rows of data. – stefan Mar 10 '22 at 07:09
  • 2
    This said, one reason for this kind of issue might be that there is no category "C5" in you data or some kind of typo, e.g. the category contains a leading space. To check have a look at `data$Domains` or `unique(data$Domains)`. – stefan Mar 10 '22 at 07:12
  • 1
    It seems like C5 has no data to show in the bar graph. It is hard to know, though, if you don't provide a [reproducible example](https://stackoverflow.com/help/minimal-reproducible-example?ssslallow). For R it is very easy with [reprex](https://cran.r-project.org/web/packages/reprex/vignettes/reprex-dos-and-donts.html) – Carlos Davila Mar 10 '22 at 07:14

2 Answers2

1

While the order of the named vector supplied to values within scale_fill_manual() determines the order in the legend, it doesn't dictate the order of the bars. You show code to set the order of the levels for data$Domains but it looks like you're running it after the plotting. Try doing it before and see if that helps.

Here's an example of what I mean about the order of the legend vs the order of the bars in the plot.

library(tidyverse)

d <- tibble(x = rep(letters[1:3], each = 3),
            y = runif(1:9),
            grp = rep(LETTERS[1:3], times = 3))

colours <- setNames(c("cadetblue", "hotpink", "black"), 
                    c("C", "B", "A"))

d %>% 
  ggplot(aes(x, y, fill = grp)) +
  geom_col(position = "dodge") +
  scale_fill_manual(values = colours)

Created on 2022-03-10 by the reprex package (v2.0.1)

Dan Adams
  • 4,971
  • 9
  • 28
0

The code is working fine and C5 contains value then the issue is where you define the levels of your domains variable (eg. before or after the ggplot()).

You can also use @stefan's code line:

data$Domains <- factor(data$Domains, levels=unique(data$Domains))

Sample code:

data$Domains <- factor(data$Domains, levels = c("C1", "V1", "V2", "C2", "V3", "C3", "V4", "C4", "V5", "C5", "GP41"))  # before the ggplot
#data$Domains <- factor(data$Domains, levels=unique(data$Domains))


 library (ggplot2)



data$Domains <- factor(data$Domains, levels = c("C1", "V1", "V2", "C2", "V3", "C3", "V4", "C4", "V5", "C5", "GP41"))  # before the ggplot
#data$Domains <- factor(data$Domains, levels=unique(data$Domains))

data$Groups <-factor(data$Groups, levels = c("1", "2", "3", "4"))

library (ggplot2)


ggplot(data) +
  geom_col(mapping = aes(x = Groups, y = Ratio, fill = Domains), position = "dodge") +
  scale_color_manual(values = c(C1 = "pink", V1 = "blue", V2 = "maroon", C2 = "orange", 
                                V3 = "violet", C3 = "green", V4 = "brown", C4 = "sky blue", 
                                V5 = "red", C5 = "yellow", GP41 = "tan"), aesthetics = c("colour", "fill"))+
  theme_minimal()

Plot:

enter image description here

Sample code:

data$Groups <-factor(data$Groups, levels = c("1", "2", "3", "4"))


library (ggplot2)

 ggplot(data) +
  geom_col(mapping = aes(x = Groups, y = Ratio, fill = Domains), position = "dodge") +
  scale_color_manual(values = c(C1 = "pink", V1 = "blue", V2 = "maroon", C2 = "orange", 
                                V3 = "violet", C3 = "green", V4 = "brown", C4 = "sky blue", 
                                V5 = "red", C5 = "yellow", GP41 = "tan"), aesthetics = c("colour", "fill"))+
  theme_minimal())

data$Domains <- factor(data$Domains, levels = c("C1", "V1", "V2", "C2", "V3", "C3", "V4", "C4", "V5", "C5", "GP41")) 

#after the ggplot. If you look at the plot, you'll notice that the order is alphabetical: C1,C2,C3,C4,C5,GP41, V1,V2,V3,V4 and V5
    #data$Domains <- factor(data$Domains, levels=unique(data$Domains))

Plot: #As you can see, the bars are arranged in alphabetical order.

Sample data:

        data<-structure(list(...1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 
NA), Groups = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, NA), .Label = c("1", "2", "3", "4"), class = "factor"), 
    Domains = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
    10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 
    2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 
    5L, 6L, 7L, 8L, 9L, 10L, 11L, NA), .Label = c("C1", "V1", 
    "V2", "C2", "V3", "C3", "V4", "C4", "V5", "C5", "GP41"), class = "factor"), 
    Ratio = c(0.231, 0.2, 0.359, 0.357, 0.371, 0.462, 0.424, 
    0.35, 0.3, 0.4, 0.358, 0.5, 0.16, 0.41, 0.531, 0.514, 0.692, 
    0.515, 0.45, 0.2, 0.575, 0.401, 0.362, 0.36, 0.36, 0.571, 
    0.371, 0.571, 0.484, 0.425, 0.1, 0.375, 0.408, 0.4, 0.16, 
    0.256, 0.54, 0.514, 0.538, 0.666, 0.175, 0.4, 0.4, 0.42, 
    NA), ...5 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA)), spec = structure(list(cols = list(...1 = structure(list(), class = c("collector_double", 
"collector")), Groups = structure(list(), class = c("collector_double", 
"collector")), Domains = structure(list(), class = c("collector_character", 
"collector")), Ratio = structure(list(), class = c("collector_double", 
"collector")), ...5 = structure(list(), class = c("collector_logical", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"),  row.names = c(NA, 
-45L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
))
Rfanatic
  • 2,224
  • 1
  • 5
  • 21
  • Thank you for your comment. I attached the sample of the data that I used. C5 has value in it but specifically for C5, I can't change either the color or the order. – Elmira Mar 10 '22 at 14:49
  • can you dput(data) – Rfanatic Mar 10 '22 at 14:52
  • @Elmira could englarge the image or post dput(data) – Rfanatic Mar 10 '22 at 15:02
  • I reupload the picture of the data set. When I make the data frame by codes that you posted, it works fine. But when I used the CSV file, it had the same issue. I am curious to know what the problem is with my datasets. – Elmira Mar 10 '22 at 17:28
  • @Elmira updated the post with your code and it is working fine – Rfanatic Mar 11 '22 at 08:38