-1

I want to improve my chart on two different points.

I don't like the numbers on the bottom line so they have to be removed. I want the data label behind the chart and not half in it.

Code for the calculation

select f.ClubName, round(w.Waarde/w.Selectie,0) as Gem_waarde
from fact f
join waarde w
on w.WDID = f.WDID
order by Gem_waarde desc

Chart code


ggplot(df_5, aes(x = fct_reorder(ClubName, Gem_waarde), y = Gem_waarde,fill = as_factor(ClubName))) +
  geom_col(position = "dodge2") +
  theme(legend.position = "none") +
  coord_flip() +
  #ylim(20000,5000000)+
  labs(x = "Club", y = "waarde per speler") +
    geom_text(aes(label = Gem_waarde, Gem_waarde = Gem_waarde + 0.05),
    position = position_dodge(0.9),vjust = 0)

enter image description here

Example Data
Club Selectie waarde
1   29  375900000
2   26  211500000
3   28  94780000
4   25  74050000
5   25  44300000
6   27  31050000
7   26  20980000
8   27  19480000
9   25  18980000
10  28  17580000
11  29  17330000
12  26  16330000
13  30  14180000
14  21  14000000
15  28  12330000
16  27  12280000
17  31  11930000
18  28  960000
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294

1 Answers1

1

If I understand right, you want that labels to be plotted into each bar chart not outside. For that, you can use hjust argument in geom_label.

To remove numbers on the bottom line, you can use axis.text.x = element_blank(). I don't think you really need to use position_dodge in your plot.

library(ggplot2)
ggplot(df, aes(x = reorder(Variable, Value), y = Value, fill = Variable))+
  geom_bar(stat = "identity") +
  geom_text(aes(label = Value), hjust = 2)+
  coord_flip()+
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        legend.position = "none")

enter image description here

EDIT: With OP data

With the example data you provided, the issue came from the large spread of values, so labeling each barplot with their values will be quite complicated as obviously, 960000 is not going to fit into its corresponding barplot.

My personal advices will be to round this data to the million and also to change the y scale in order that no values will be put outside the graph. Then set hjust to 1 in order to have the number right aligned on the top of its corresponding barplot.

In code, it looks like that:

ggplot(df, aes(x = reorder(as.factor(Club), waarde), y = waarde, fill = as.factor(waarde)))+
  geom_bar(stat = "identity") +
  geom_text(aes(label = paste0(waarde/1000000," M")), hjust = 1)+
  scale_y_continuous(limits = c(-15,380)*1000000)+
  coord_flip()+
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        legend.position = "none")

enter image description here

Does it look satisfying for your you ?

Data - Initial example

df = data.frame(Variable = LETTERS[1:10],
                Value = sample(1:1000,10))
dc37
  • 15,840
  • 4
  • 15
  • 32