Home > Blockchain >  R Stacked Bar Chart: How to change the order of geom text labels
R Stacked Bar Chart: How to change the order of geom text labels

Time:09-03

The geom Texts labels are automatically in decreasing order instead of the data frame.

The question is concerning this part of the snippet "geom_text(aes(label = Freq)..."

EDIT: enter image description here Here you can clearly see the that the order is not followed by geom_text. But Frequency descreasing in all categories :-/

ggplot(df_beine_clan, aes(x = Var2, y = Freq, fill = Var1))   
  geom_bar(stat = "identity")  
  geom_text(aes(label = Freq), vjust = 0, size = 5, nudge_y = 2, nudge_x = -0.5)

See Freq Order enter image description here

How to command that the order should not be changed when rendered on the bar chart?

CodePudding user response:

You could add position_stack like this:

library(ggplot2)
ggplot(df_beine_clan, aes(x = Var2, y = Freq, fill = Var1))   
  geom_bar(stat = "identity")  
  geom_text(aes(label = Freq), position = position_stack(vjust = 0.5), size = 5)

Created on 2022-09-03 with enter image description here

Changing the variable label to label <- sort(Value, decreasing = T) or label <- c("Blue", "Green", "Red", "Blue", "Green", "Red") gives you the two figures below.

enter image description here

enter image description here

  • Related