ggplot2条形图不显示scale_x_discrete()中定义的标签

时间:2017-12-09 07:57:40

标签: r ggplot2 labels

我的数据集如下:

 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

我的代码如下:

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)

情节如下:

enter image description here

我的问题是:为什么标签不会出现在x轴上?

1 个答案:

答案 0 :(得分:1)

尝试以下方法:

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

我认为这里唯一的问题是您使用aes(Index, scaled_Gain)代替aes(Feature, scaled_Gain)