简体   繁体   English

ggplot2,x轴不准确显示刻度

[英]ggplot2, x-axis does not display ticks accurately

I´m creating a barplot for my poster and have run into an issue.我正在为我的海报创建一个条形图并且遇到了问题。 I have a factor on my x axis, and a numeric on my y axis.我的 x 轴上有一个因子,y 轴上有一个数字。 I want my x axis to display every level of my factor, but with a differerent name.我希望我的 x 轴显示我的因子的每个级别,但名称不同。

To do that, I used the scale_x_discrete command, which is supposed to allow me to do that.为此,我使用了 scale_x_discrete 命令,它应该允许我这样做。 However, it just deletes all ticks.但是,它只会删除所有刻度。 Please find below all the code required to reproduce the problem.请在下面找到重现问题所需的所有代码。

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()

Convert your x variable to character:将您的 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM