rggplot2bar-chartpercentagestacked

Is there an easy way to make an horizontal single stacked Barplot with R?


I'd appreciate it if anyone could help me with making a single stacked Barplot with R. Here is a small example of data to plot:

cell_type   Percentage
CD20 B cells    15.00
CD4 T cells 25.00
Other cells 60.00

This is what I used, but cannot modify it too much.

p1 <-  ggplot(Data, aes(x = "", y = percentage, fill = cell_type))

p1

p2 <-  p1 + geom_col()
p2

Many thanks in advance for your help.


Solution

  • Maybe you want something like this:

    library(tidyverse)
    df %>%
      mutate(dummy = "v") %>%
      ggplot(aes(x = dummy, y = Percentage, fill = cell_type)) +
      geom_col() +
      geom_text(aes(label = paste0(Percentage, "%")),
                position = position_stack(vjust = 0.5)) +
      theme_minimal() +
      labs(x = "", y = "Percentage")
    

    Output:

    enter image description here