Home > other >  could not put the numbers in the middle of the bar chart next to the histogram
could not put the numbers in the middle of the bar chart next to the histogram

Time:08-12

I was inspired by one of the codes here in stackoverflow and create a histogram and a barchart next to each other. My code is this

risk <- data.frame(
  stringsAsFactors = FALSE,
  Age = c("0-19", "20-39", "40-65", "65 "),
  obesityrisk = c(0.5567, 0.1004, 0.3691, 0.4526)
)

obesity <- data.frame(  stringsAsFactors = FALSE,
  Age = c("0-19", "20-39", "40-65", "65 "),
  n = c(3567, 6004, 8691, 9966)
)
 
d1 <- ggplot(obesity, aes(x=Age, y=n))   
  geom_bar(stat="identity")    
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE)) 
 
 d2 <- risk %>% 
  mutate(
    Age = fct_rev(factor(Age))
  ) %>% 
  ggplot(aes(Age, obesityrisk))  
  geom_col(fill = colorspace::lighten("gray"), width = 0.7)  
  coord_flip()  
  labs( x = NULL, y = NULL, 
        title = "relative \nobesity rate\nby Age")  
  geom_text(aes(Age, obesityrisk, label = paste0(obesityrisk, "%")), 
            size = 2.5, hjust = 1)  
  theme_classic()  
  theme(
    axis.line = element_blank(),
    axis.text.y = element_text(size = 9, angle = 90, hjust = 0.5),
    axis.ticks = element_blank(),
    axis.text.x = element_blank(),
    plot.title = element_text(color = colorspace::lighten("black", amount = 0.5))
  )
 
 layout <- "
AAAA##
AAAABB
"

d1   d2    plot_layout(design = layout, heights = c(1, 30))  
  plot_annotation(
    title = "Absolute numbers of Obesity by Age in Ireland"
  )

The problem is that the numbers in % are were bad positioned. I want to have it in the middle of the bar chart. How can I fix it, that the numbers are in the middle of the bar chart? The problem is the left side.

I think if someone run the code it is visible the problem. Hopefully

If not please let me know that I can describe it more understandable

enter image description here

CodePudding user response:

To place the labels in the middle of the bars use position_stack(vjust = .5) and set hjust=.5. Also, to format as percentages I used scales::percent which will scale you values by default by 100 (not sure whether that is correct as I don't know your data).

library(dplyr, warn=FALSE)
library(ggplot2)
library(patchwork)
library(forcats)

d1 <- ggplot(obesity, aes(x=Age, y=n))   
  geom_bar(stat="identity")    
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE)) 

d2 <- risk %>% 
  mutate(
    Age = fct_rev(factor(Age))
  ) %>% 
  ggplot(aes(Age, obesityrisk))  
  geom_col(fill = colorspace::lighten("gray"), width = 0.7)  
  coord_flip()  
  labs( x = NULL, y = NULL, 
        title = "relative \nobesity rate\nby Age")  
  geom_text(aes(Age, obesityrisk, label = scales::percent(obesityrisk)), 
            size = 2.5, hjust = .5, 
            position = position_stack(vjust = .5)
            )  
  theme_classic()  
  theme(
    axis.line = element_blank(),
    axis.text.y = element_text(size = 9, angle = 90, hjust = 0.5),
    axis.ticks = element_blank(),
    axis.text.x = element_blank(),
    plot.title = element_text(color = colorspace::lighten("black", amount = 0.5))
  )

layout <- "
AAAA##
AAAABB
"

d1   d2    plot_layout(design = layout, heights = c(1, 30))  
  plot_annotation(
    title = "Absolute numbers of Obesity by Age in Ireland"
  )

  •  Tags:  
  • r
  • Related