ggplot: geom_bar stacking order and labels

shashwat vajpeyi

I am plotting the following data using ggplot:

df<- data.frame(name= c("g1","g1","p1","p1"),fc = c(-1.32,-2.11,-1.17,-3.23),fdr = c(.0001,.0001,.07,.0002),cond= c("2v1","3v1","2v1","3v1"))


head(df)
name    fc   fdr cond
 g1  -1.32 .0001 2v1
 g1  -2.11 .0001 3v1
 p1  -1.17   .07 2v1
 p1  -3.23 .0002 3v1

Using ggplot code:

df$name<- as.factor(df$name)
df$name <- relevel(df$name, as.character(df$name[3]))

ggplot(df, aes(name,fc), group=variable)+
geom_col(aes(fill=factor(as.numeric(fdr)<0.05)), width = 0.98,color="white")+
coord_flip()+scale_fill_manual(values = c("FALSE"= "#00BFC4","TRUE"= "#F8766D"))+
geom_hline(yintercept = 0, colour = "gray" )+
geom_text(aes(label=round(fc,2)),angle = 90, position = position_stack(vjust = 0.5), size =3.5, color= "white")

Resulting in this plot: enter image description here

The plot for p1 seems flipped where the bar for -1.17 is on top, while the label is still on the bottom. I would like the gray bar to be on the bottom and the label "1.17" in the middle of it. I would appreciate any help I can get. Thanks

Peter

I recommend setting up as much of the plotting characteristics in the data.frame as possible. That will help with keeping the aes from one layer to another consistent.

library(ggplot2)
library(dplyr)

df <- data.frame(name = c( "g1",  "g1",  "p1",  "p1"), 
                 fc   = c(-1.32, -2.11, -1.17, -3.23), 
                 fdr  = c(.0001, .0001,   .07, .0002), 
                 cond = c("2v1", "3v1", "2v1", "3v1"))

# use dplyr to set the name and fill factors
# use the group_by to generate the cumsum for defining where the label should be
# located.
df <-
  df %>%
  dplyr::mutate(name = factor(name, levels = c("p1", "g1")),
                fill = factor(fdr < 0.05, c(TRUE, FALSE), c("fdr < 0.05", "fdr >= 0.05"))) %>%
  dplyr::group_by(name) %>%
  dplyr::mutate(text_y = -cumsum(-fc)) %>%
  dplyr::ungroup()

# the plot
ggplot(df) +
  aes(x = name, y = fc, fill = fill) + 
  geom_col(width = 0.98, color="white") +
  coord_flip() +
  geom_hline(yintercept = 0, colour = "gray" ) +
  geom_text(mapping  = aes(y = text_y, label = round(fc, 2)),
            angle    = 90,
            color    = "white",
            position = position_stack(vjust = 0.5),
            size     = 3.5,
            color    = "white")

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Stacking labels in ggplot

How to vertically center labels with geom_bar (ggplot2)

R ggplot2 geom_bar add labels with % sign

ggplot2, geom_bar, dodge, order of bars

Control the fill order and groups for a ggplot2 geom_bar

Change order of filling variable on a ggplot geom_bar

R: ggplot geom_bar fill order not following levels

R: ggplot2 geom_text data labels for binned geom_bar

How to remove select labels in ggplot geom_bar plot and center these labels?

In ggplot geom_bar, the y axis labels are cluttered at the bottom of bar chart

How to put labels over geom_bar for each bar in R with ggplot2

geom_bar overlapping labels

R ggplot2: labels inside bars, no stacked geom_bar

ggplot2: Value labels in stacked barplot with geom_bar(position = 'fill')

Set starting point for breaks for labels in ggplot using geom_bar [R]

ggplot grouped geom_bar - adding labels of factor categories to the x-axis label

How to add custom labels from a dataset on top of bars using ggplot/geom_bar in R?

Adding labels to individual % inside geom_bar() using R / ggplot2

How can I put labels on geom_bar() in ggplot2 (without a manual y_position)

Reverse stacking order without affecting legend order in ggplot2 bar charts

Control fill colour order in graph and legend for ggplot::geom_bar position_stack with positive and negative values

order and fill with 2 different variables geom_bar ggplot2 R

Proportion with ggplot geom_bar

Order the variables geom_bar

Order geom_bar by groups

R: change bar labels in geom_bar

ggplot2: geom_bar(); how to alternate order of fill so bars are not lost inside a bar with a higher value?

Out of order text labels on stack bar plot (ggplot)

Order Facet Bar Plot Labels ggplot2