Change order and add data label on bar plots

I am trying to add data labels on bars and also i want to reorder condition on bars such as first "weak", followed by "JAR" and then "strong". I tried few things but no success yet. Please help me if you can. Here is code:

library(ggplot2)
df <- read.csv("F:/jar_file.csv")
colnames(df)[1] = "Sample"
p4 <- ggplot() + geom_bar(aes(y = Sample, x = Percent, fill = Condition), data = df,
                          stat="identity") + 
  scale_fill_manual(values = c("#669999", "#CCCCCC","#FFFFFF")) + 
  theme(axis.title.x=element_blank())
p4

Created on 2020-06-21 by the reprex package (v0.3.0)

You can reorder the levels of the Condition column using the factor() function.

library(ggplot2)
DF <- data.frame(Sample = rep(c("A", "B", "C"), each = 3),
                 Percent = c(20,20, 60, 25, 35, 40, 30, 20, 50),
                 Condition = c("JAR", "strong", "weak", "JAR", "strong", "weak",
                               "JAR", "strong", "weak"))
DF
#>   Sample Percent Condition
#> 1      A      20       JAR
#> 2      A      20    strong
#> 3      A      60      weak
#> 4      B      25       JAR
#> 5      B      35    strong
#> 6      B      40      weak
#> 7      C      30       JAR
#> 8      C      20    strong
#> 9      C      50      weak
ggplot() + geom_bar(aes(y = Sample, x = Percent, fill = Condition), data = DF,
                    stat="identity") + 
  scale_fill_manual(values = c("#669999", "#CCCCCC","#FFFFFF")) + 
  theme(axis.title.x=element_blank())


#Reorder the levels of Condition
DF$Condition = factor(DF$Condition, levels = c("weak", "JAR", "strong"))
ggplot() + geom_bar(aes(y = Sample, x = Percent, fill = Condition), data = DF,
                    stat="identity") + 
  scale_fill_manual(values = c("#669999", "#CCCCCC","#FFFFFF")) + 
  theme(axis.title.x=element_blank())

Created on 2020-06-21 by the reprex package (v0.3.0)

2 Likes

Thanks @FJCC for the help. I reordered the condition.
Do you know how i can put data label in the mid point of each stacked bar and also i just want 2 digits followed by % sign. I tried but it was not working:

#JAR plot
library(ggplot2)
df <- read.csv("F:/jar_file.csv")
colnames(df)[1] = "Sample"
df$Condition <- factor(df$Condition, levels = c("Strong", "JAR", "Weak"))
ggplot(df, aes(y = Sample, x = Percent, fill = Condition)) + 
  geom_bar(stat="identity") + geom_text(aes(label=Percent)) +
  scale_fill_manual(values = c("#CCCCCC", "#669999", "#FFFFFF")) + 
  theme(axis.title.x=element_blank())

Created on 2020-06-24 by the reprex package (v0.3.0)

There is a special position function for this purpose, position_stack().

library(ggplot2)
DF <- data.frame(Sample = rep(c("A", "B", "C"), each = 3),
                 Percent = c(19.013,20.983, 60, 25, 35, 40, 30, 20, 50),
                 Condition = c("JAR", "strong", "weak", "JAR", "strong", "weak",
                               "JAR", "strong", "weak"))
DF$Condition = factor(DF$Condition, levels = c("weak", "JAR", "strong"))
ggplot(aes(y = Sample, x = Percent, fill = Condition), data = DF) + 
  geom_bar(stat="identity") +
  geom_text(aes(x = Percent, label = paste0(round(Percent, 2),"%")), 
            position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values = c("#669999", "#CCCCCC","#FFFFFF")) + 
  theme(axis.title.x=element_blank())

Created on 2020-06-23 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.