5

I have the following R codes running in RStudio, with the output also shown below:

df2  %>%
  ggplot(aes(
    x = JANUARY,
    y = value,
    fill = JANUARY,
    group = year
  )) +
  geom_col(
    position = position_dodge(.65),
    width = .5
  ) +
  geom_text(aes(
    y = value + max(value) * .03,
    label = round(value * 100) %>% str_c('%')
  ),
  position = position_dodge(.65)
  ) +
  geom_text(aes(
    y = y_pos,
    label = str_remove(year, 'X')
  ),
  color = 'white',
  angle = 90,
  fontface = 'bold',
  position = position_dodge(0.65)
  ) +
  scale_y_continuous(
    breaks = seq(0, .9, .1),
    labels = function(x) round(x * 100) %>% str_c('%')
  ) +
  scale_fill_manual(values = c(
    rgb(47, 85, 151, maxColorValue = 255),
    rgb(84, 130, 53, maxColorValue = 255),
    rgb(244, 177, 131, maxColorValue = 255),
    rgb(112, 48, 160, maxColorValue = 255),
    rgb(90, 48, 100, maxColorValue = 255)
  )) +
  theme(
    plot.title = element_text(hjust = .5),
    panel.background = element_blank(),
    panel.grid.major.y = element_line(color = rgb(.9, .9, .9)),
    axis.ticks = element_blank(),
    legend.position = 'none'
  ) +
  xlab('') +
  ylab('') +
  ggtitle('Month of JANUARY (as at 01 January)')

Output is:

column chart

As you can see, the value "0%" under "D-Final" is causing the labels inside the bars to disappear below the x-axis.

I want to remove the "0%" and get the labels back into position inside the bars. How can I modify my codes to achieve this?

Data (df2) added:

JANUARY year  value y_pos
   <fct>   <chr> <dbl> <dbl>
 1 D-150   X2016  0.26 0.12 
 2 D-90    X2016  0.49 0.21 
 3 D-60    X2016  0.63 0.265
 4 D-30    X2016  0.73 0.325
 5 D-Final X2016  0.81 0    
 6 D-150   X2017  0.28 0.12 
 7 D-90    X2017  0.5  0.21 
 8 D-60    X2017  0.64 0.265
 9 D-30    X2017  0.77 0.325
10 D-Final X2017  0.82 0    
11 D-150   X2018  0.33 0.12 
12 D-90    X2018  0.51 0.21 
13 D-60    X2018  0.62 0.265
14 D-30    X2018  0.77 0.325
15 D-Final X2018  0.78 0    
16 D-150   X2019  0.24 0.12 
17 D-90    X2019  0.42 0.21 
18 D-60    X2019  0.53 0.265
19 D-30    X2019  0.65 0.325
20 D-Final X2019  0    0    
3
  • 3
    Does df2 %>% filter(value > 0.01) %>% in the first line help? Otherwise, please add your data to the question through dput. Commented Feb 4, 2019 at 12:20
  • Thanks. I have added the data "df2" to the question. Commented Feb 4, 2019 at 12:31
  • Yes the code you provided eliminates the zero values in the column chart but the position of my labels inside the bars for "D-Final" remain the same. Commented Feb 4, 2019 at 12:33

2 Answers 2

3

It's not really about 0%, at least this point. Position of the labels is predefined and given by y_pos, so you may simply alter it yourself with, e.g.,

df2$y_pos[df2$JANUARY == "D-Final"] <- 0.4

As to remove 0%, the first line could be replaced by

df2 %>% filter(value > 0.01) %>% 

This gives

enter image description here

Apparently y_pos was defined with

df2 %>% group_by(JANUARY) %>% mutate(y_pos = min(value) / 2)

Hence, as to avoid this issue, in this case (since all the other value by group are similar) you may instead use

df2 %>% group_by(JANUARY) %>% mutate(y_pos = max(value) / 2)
Sign up to request clarification or add additional context in comments.

Comments

2

in the first line you can filter df2 to get all non-zero values using df2 %>% filter(value!=0)

1 Comment

It removes the zero but I still have the labels inside the bars for "D-Final" not appearing fully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.