ggplot2 条形图不显示 scale_x_discrete() 中定义的标签
ggplot2 bar plot does not display labels as defined in scale_x_discrete()
我的数据集如下:
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)
剧情如下:
我的问题是:为什么标签没有出现在 x 轴上?
尝试以下操作:
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()
我认为这里唯一的问题是您使用的是 aes(Index, scaled_Gain)
而不是 aes(Feature, scaled_Gain)
。
我的数据集如下:
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)
剧情如下:
我的问题是:为什么标签没有出现在 x 轴上?
尝试以下操作:
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()
我认为这里唯一的问题是您使用的是 aes(Index, scaled_Gain)
而不是 aes(Feature, scaled_Gain)
。