How to add text on the geom_bar while making the theme_void ?

How can we add the name of the Y variable on the bars ?

relevant posts:


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))
df

ggplot(df, aes(x=reorder(Seller, Num), y=Avg_Cost)) +
  geom_bar(stat='identity') +
  coord_flip() +

  theme_classic() +
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()
        ) +
  theme_void() +
  ggtitle("How to add corresponding variable on the bars ? \n
          literally on the bar (not @ the end as shown below)")

tmp <- mtcars %>% group_by(cyl) %>% summarise(tot_mpg = sum(mpg))
tmp$cyl <- factor(tmp$cyl)
ggplot(mtcars) +
  geom_bar(aes(x = reorder(factor(cyl), mpg), y = mpg), stat="identity") +
  coord_flip() + geom_text(data = tmp, nudge_y = 10, angle = 270,
                           aes(x = cyl, y = tot_mpg, label = tot_mpg))

Example:
image

You can play with the vjust argument in position stack.

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))
df
#>   Seller Avg_Cost Num
#> 1     Ad     5.30   6
#> 2     Rt     3.72   5
#> 3     Ra     2.91   4
#> 4     Mo     2.64   3
#> 5     Ao     1.17   2
#> 6     Do     1.10   1

library(ggplot2)
ggplot(df, aes(x=reorder(Seller, Num), y=Avg_Cost)) +
  geom_col() +
  coord_flip() +
  theme_classic() +
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()
  ) +
  theme_void() +
  geom_text(aes(label = Seller), position = position_stack(0.5), color = "white")


library(dplyr)
#> 
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
tmp <- mtcars %>% group_by(cyl) %>% summarise(tot_mpg = sum(mpg))
tmp$cyl <- factor(tmp$cyl)
ggplot(mtcars) +
  geom_col(aes(x = reorder(factor(cyl), mpg), y = mpg)) +
  coord_flip() + 
  geom_text(data = tmp, color = "white",
            aes(x = cyl, y = tot_mpg, label = tot_mpg), position = position_stack(0.5))

Created on 2019-10-06 by the reprex package (v0.3.0)

Is this what you want ?

2 Likes

Can we left-align the text ?
tried:
position_stack(0.1)

Capture

If you know exactly where to put your label, you can explicitely positioned them in the Y axis

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))
df
#>   Seller Avg_Cost Num
#> 1     Ad     5.30   6
#> 2     Rt     3.72   5
#> 3     Ra     2.91   4
#> 4     Mo     2.64   3
#> 5     Ao     1.17   2
#> 6     Do     1.10   1

library(ggplot2)
ggplot(df, aes(x = reorder(Seller, Num), y = Avg_Cost)) +
  geom_col() +
  coord_flip() +
  theme_classic() +
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()
  ) +
  theme_void() +
  geom_text(aes(label = Seller, y = 0.2), color = "white")

Created on 2019-10-06 by the reprex package (v0.3.0)

2 Likes

Really got confused, when the following code could not produce left-aligned wordings on the geom_bar

library(tidyverse)
df <- data.frame(Seller=c("R statistical programming","Shiny (Web application)",
                          "Python scripting/programming ","Databases (mysql, postgresql)",
                          "Git/Github","Linux"),
                 Avg_Cost=c(10,10,
                            7,7,
                            5,5), Num =c(10,10,
                                         7,7,
                                         5,5))

df

ggplot(df, aes(x=reorder(Seller,Num), y=Avg_Cost)) +
  geom_bar(stat='identity',fill = "#ADD8E6") +
  coord_flip() +

  theme_classic() +
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()
        ) +
  theme_void() +
  geom_text(aes(label = Seller, y = 3), color = "black")

the default is to center the labels at the given position. You can use the hjust parameter to align the text. Notice that I changed the y value to move the label position in general.

library(ggplot2)
df <- data.frame(Seller=c("R statistical programming","Shiny (Web application)",
                          "Python scripting/programming ","Databases (mysql, postgresql)",
                          "Git/Github","Linux"),
                 Avg_Cost=c(10,10,7,7,5,5), 
                 Num =c(10,10, 7,7,5,5))

df
#>                          Seller Avg_Cost Num
#> 1     R statistical programming       10  10
#> 2       Shiny (Web application)       10  10
#> 3 Python scripting/programming         7   7
#> 4 Databases (mysql, postgresql)        7   7
#> 5                    Git/Github        5   5
#> 6                         Linux        5   5

ggplot(df, aes(x=reorder(Seller,Num), y=Avg_Cost)) +
  geom_bar(stat='identity',fill = "#ADD8E6") +
  coord_flip() +
  
  theme_classic() +
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()
  ) +
  theme_void() +
  geom_text(aes(label = Seller, y = 1), color = "black", hjust = 0)

Created on 2019-10-07 by the reprex package (v0.2.1)

2 Likes

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