rf7
rf7

Reputation: 2191

ggplot2 bar plot does not display labels as defined in scale_x_discrete()

My dataset is the following:

 Index  Feature scaled_Gain sign_Correlation
    1   a   1.00000000  1
    2   b   0.60999674  1
    3   c   0.54824913  1
    4   d   0.11134079  1
    5   e   0.06530486  1
    6   f   0.06470836  1
    7   g   0.06263247  1
    8   h   0.04166633  1
    9   i   0.03242897  -1
    10  j   0.02913138  1

My code is the following:

ggplot(data = plot_data) +
geom_bar(aes(x = Index, y = scaled_Gain, fill = sign_Correlation), stat = "identity", alpha = 0.5) + 
theme_economist() + ggtitle("Scaled Gain")  + scale_x_discrete(labels = plot_data$Feature)

The plot is the following:

enter image description here

My question is: Why the labels do not appear in the x-axis?

Upvotes: 1

Views: 1555

Answers (1)

tyluRp
tyluRp

Reputation: 4768

Try the following:

library(ggplot2)

ggplot(df, aes(Feature, scaled_Gain, fill = factor(sign_Correlation))) +
  geom_bar(stat = "identity", alpha = 0.5) + 
  ggtitle("Scaled Gain") + 
  labs(fill = "Sign Correlation") +
  ggthemes::theme_economist()

enter image description here

I think the only problem here is that you're using aes(Index, scaled_Gain) instead of aes(Feature, scaled_Gain).

Upvotes: 1

Related Questions