How to start text label from extreme left in bar plot using geom_col and geom_text in ggplot2?

I tried a bar plot using the below codes (apologies for bad codes, I'm not a pro). I want the text inside the bar to start from the extreme left (from the y-axis).

Thank you .

library(tidyverse)
library(ggrepel)
library(ggfittext)
library(RColorBrewer)
library(ggthemes)

hours <- c("2 hrs", "2 hrs", "2 hrs", "2 hrs",
           "4 hrs", "4 hrs", "6 hrs")

plan <- tribble(
  ~hrs, ~task, 
  8, "Data viz using ggplot2", 
  6, "Dynamic docs using r markdown",
  6, "Practice exercises",
  4, "Intro to R & RStudio",
  4, "Citations using citr",
  4, "Tables using gt",
  4, "Slides using xaringan"
)

plan %>% 
  mutate(task = fct_reorder(task, hrs)) %>% 
  ggplot(aes(hrs, task, fill = task)) +
  geom_col(stat="identity", alpha=.6, width=.7) +
  scale_fill_brewer(palette = "Set2") +
  theme_tufte() +
  geom_text(aes(label=task), 
            hjust = 0.95, 
            size = 7) +
  labs(
    title = "Topics you will learn",
    x = "",
    y = ""
    ) + 
   theme(
     plot.title = element_text(size = 25, hjust = 0.5),
     legend.position = "none",
     axis.text.y = element_text(size = 25),
     axis.text.x = element_blank(),
     axis.ticks.x = element_blank()) + 
  scale_y_discrete(labels= hours) +
  annotate("rect", xmin = 4.5, xmax = 8.5, ymin = 0.7, ymax = 2.8, color = "white", fill= "orange") +
  annotate("text", x = 6.5, y = 2.50, label = c("Total 11 sessions"), size = 7, color = "white", fontface=2) +
  annotate("text", x = 6.5, y = 2.00, label = c("7pm to 9pm IST"), size = 7, color = "white", fontface=2) +
  annotate("text", x = 6.5, y = 1.50, label = c("Tue - Thur - Sat"), size = 7, color = "white", fontface=2) +
  annotate("text", x = 6.5, y = 1.00, label = c("Sept 5 - 29, 2020"), size = 7, color = "white", fontface=2) 

Is this what you mean?

library(tidyverse)

hours <- c("2 hrs", "2 hrs", "2 hrs", "2 hrs",
           "4 hrs", "4 hrs", "6 hrs")

plan <- tribble(
    ~hrs, ~task, 
    8, "Data viz using ggplot2", 
    6, "Dynamic docs using r markdown",
    6, "Practice exercises",
    4, "Intro to R & RStudio",
    4, "Citations using citr",
    4, "Tables using gt",
    4, "Slides using xaringan"
)

plan %>% 
    mutate(task = fct_reorder(task, hrs)) %>% 
    ggplot(aes(hrs, task, fill = task)) +
    geom_col(alpha = .6, width = .7) +
    scale_fill_brewer(palette = "Set2") +
    geom_text(aes(label = task),
              x = 0,
              hjust = 0, 
              size = 7) +
    labs(
        title = "Topics you will learn",
        x = "",
        y = ""
    ) + 
    scale_y_discrete(labels = hours) +
    theme(
        plot.title = element_text(size = 25, hjust = 0.5),
        legend.position = "none",
        axis.text.y = element_text(size = 25),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) +
    annotate("rect", xmin = 4.5, xmax = 8.5, ymin = 0.7, ymax = 2.8, color = "white", fill = "orange") +
    annotate("text", x = 6.5, y = 2.50, label = c("Total 11 sessions"), size = 7, color = "white", fontface = 2) +
    annotate("text", x = 6.5, y = 2.00, label = c("7pm to 9pm IST"), size = 7, color = "white", fontface = 2) +
    annotate("text", x = 6.5, y = 1.50, label = c("Tue - Thur - Sat"), size = 7, color = "white", fontface = 2) +
    annotate("text", x = 6.5, y = 1.00, label = c("Sept 5 - 29, 2020"), size = 7, color = "white", fontface = 2) 

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

1 Like

Thank you @andresrcs. I was looking for exactly this outcome. Once again my sincere thanks for your prompt reply. :slight_smile:

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.