ggplot2,x轴无法正确显示刻度

时间:2019-11-06 13:37:14

标签: r ggplot2

我正在为我的海报创建一个barplot,但是遇到了问题。 我的x轴上有一个因子,而我的y轴上有一个数字。我希望我的x轴显示因子的每个级别,但名称不同。

为此,我使用了scale_x_discrete命令,该命令应该允许我执行此操作。但是,它只是删除所有刻度。请在下面找到重现该问题所需的所有代码。

F_Value <- c(13.011, 8.827, 15.586, 7.223, 16.190, 9.844)
Pathway <- as.factor(1:6)
Transformation <- as.factor(c(1, 1, 2, 2, 3, 3))
Outlier <- as.factor(c("Outliers", "Outlier Removed",
                       "Outliers", "Outlier Removed",
                       "Outliers", "Outlier Removed"))
df <- as.data.frame(cbind(F_Value, Pathway, Transformation, Outlier))
df


ggplot(df, 
       aes(x = Pathway, 
           y = F_Value, f
           ill = factor(Outlier))) + 
  geom_bar(stat = "identity", 
           position = "dodge") +
  scale_x_discrete(labels = c("1" = "Raw", "2" = "Raw, Trimmed", 
                              "3" = "Log", "4" = "Log, Trimmed", 
                              "5" = "Normalised", "6" = "Normalised, Trimmed")) +
  scale_fill_discrete(name = "Outliers", 
                      labels = c("Trimmed", "Not Trimmed")) +
  xlab("Pathway") +
  ylab("F-Statistic") +
  ggtitle("F-Statistics by Pathway") +
  theme_light()

1 个答案:

答案 0 :(得分:3)

将x变量转换为字符:

ggplot(df, 
       aes(x = as.character(Pathway), 
           y = F_Value, f
           ill = factor(Outlier))) + 
  geom_bar(stat = "identity", 
           position = "dodge") +
  scale_x_discrete(labels = c("1" = "Raw", "2" = "Raw, Trimmed", 
                              "3" = "Log", "4" = "Log, Trimmed", 
                              "5" = "Normalised", "6" = "Normalised, Trimmed")) +
  scale_fill_discrete(name = "Outliers", 
                      labels = c("Trimmed", "Not Trimmed")) +
  xlab("Pathway") +
  ylab("F-Statistic") +
  ggtitle("F-Statistics by Pathway") +
  theme_light()
相关问题