Can barchart value in facet_wrap be displayed without truncation based on its values ?

Here is a barchart where values are too big/small. Therefore, it seems that truncation happens depending on vjust used
geom_text(aes(label = paste(sprintf("%.1f", importance), "%"), y= importance), vjust = 0 or +/- 1)
However, can there be an option where the value always visible without truncation ?

library(tidyverse)

library(drlib)
viz <- data.frame(type = c(rep("A",5),rep("B",4)),
                  project = c("ABC","BCD","CDE","EFG","FGI","GIK","IKL","KLM","LMO"),
                  complexity = c(.8,.95,.6,.8,.9,.3,.7,.01,.6),
                  impact = c(1,.8,.7,.8,.9,.4,.8,.6,.5),
                  relevance = c(.8,.5,.7,.9,.1,.8,.7,.7,100),
                  stringsAsFactors = F)

#viz[,3:5] <- map(viz[,3:5], scales::percent) 

viz_lng <- viz %>% 
  gather(-c(1,2), key = category, value = importance)%>% 
  arrange(desc(importance))

ggplot(viz_lng[which(viz_lng$importance>0),], 
       aes(x = reorder_within(project, importance, category, ), 
           y= importance, group = project, fill = project)) +
  geom_bar(stat="identity") + 
  xlab("Projects") +
  ggtitle("Project priority") +
  geom_text(aes(label = paste(sprintf("%.1f", importance), "%"), y= importance),  vjust = -.1) +
  #geom_text(aes(label = paste(sprintf("%.1f", importance), "%"), y= importance),  vjust = 0) +
  scale_x_reordered() +
  facet_wrap(vars(category,type), scales="free", ncol = 2) + 
  guides(fill=FALSE) + theme_bw() +
  theme(strip.text = element_text(size = 14),
        axis.text.x = element_text(angle = 45, hjust = 1),
        legend.title = element_blank(),
        axis.text = element_text(size = 14),
        axis.title = element_text(size = 14))

Is this what you mean?

library(tidyverse)

viz <- data.frame(type = c(rep("A",5),rep("B",4)),
                  project = c("ABC","BCD","CDE","EFG","FGI","GIK","IKL","KLM","LMO"),
                  complexity = c(.8,.95,.6,.8,.9,.3,.7,.01,.6),
                  impact = c(1,.8,.7,.8,.9,.4,.8,.6,.5),
                  relevance = c(.8,.5,.7,.9,.1,.8,.7,.7,100),
                  stringsAsFactors = F)

viz_lng <- viz %>% 
  gather(-c(1,2), key = category, value = importance)%>% 
  arrange(desc(importance))

ggplot(viz_lng[which(viz_lng$importance>0),], 
       aes(x = project, 
           y = importance, group = project, fill = project)) +
  geom_bar(stat="identity") + 
  xlab("Projects") +
  ggtitle("Project priority") +
  geom_text(aes(label = paste(sprintf("%.1f", importance), "%"), y= importance),  vjust = -.1) +
  facet_wrap(vars(category,type), scales="free", ncol = 2) + 
  guides(fill=FALSE) +
  theme_bw() +
  scale_y_continuous(expand = expand_scale(mult = c(0, .2))) +
  theme(strip.text = element_text(size = 14),
        axis.text.x = element_text(angle = 45, hjust = 1),
        legend.title = element_blank(),
        axis.text = element_text(size = 14),
        axis.title = element_text(size = 14))

1 Like

So this line makes the magic. Thanks for quick help

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