Home > Enterprise >  Move subtitle to left direction (aligning y axis area) in ggplot2
Move subtitle to left direction (aligning y axis area) in ggplot2

Time:01-28

Given sample data and ggplot plotting code below:

df <- data.frame(Seller=c("Ad","Rt","Ra","Mo","Ao","Do"), 
                 Avg_Cost=c(5.30,3.72,2.91,2.64,1.17,1.10), Num=c(6:1))

text <- "Real estate agents often refer to a home's curb appeal, the first impression 
it makes on potential buyers. As a would-be seller, it's important to take as dispassionate 
a look as possible at the outside of your home."

ggplot(df, aes(x=reorder(Seller, Num), y=Avg_Cost))  
  geom_bar(stat='identity')  
  coord_flip()  
  labs(
    title = 'Costs of Selling a Home',
    subtitle = stringr::str_wrap(text, 80)
  )  
  theme(
    plot.title = element_text(hjust = 0.5),
    plot.subtitle = element_text(hjust = 0), 
    plot.margin = unit(c(0.1, 0, 0, 0), "cm")
  )

Result:

enter image description here

I attempt to slightly adjust subtitle to left direction (as the red arrow shows), the final subtitle will be relocated in the area of black rectangle.

I've tried by adjusting values of hjust in plot.subtitle(), but I didn't get the chance of success. How could I achieve that? Thanks.

Reference:

enter image description here

Below is OP's plot, without coord_flip(), with geom_col() instead of geom_bar(..., stat = "identity") and switch of the aesthetics.

library(ggplot2)
p <- ggplot(df, 
       aes(x=Avg_Cost,
           y=reorder(Seller, Num))
       )  
  geom_col()   
  # geom_bar(stat='identity')  
  # coord_flip()  
  labs(
    title = 'Costs of Selling a Home',
    subtitle = stringr::str_wrap(text, 80)
  )  
  theme(
    plot.title = element_text(hjust = 0.5),
    plot.subtitle = element_text(hjust = 0), 
    plot.margin = unit(c(0.1, 0, 0, 0), "cm")
  )

Reference: enter image description here

  •  Tags:  
  • Related