ggplot2旋转x-lables后返回空图

时间:2019-05-29 09:37:57

标签: r ggplot2

我得到了一个数据,用于比较两个基因组(第1列和第2列)及其与该指数的相似性,该指数指示了该物种的基因组比较数:

 Genome1               Genome2             % of similarity 1 in 2   % of similarity 2 in 1       Species        index
 1: GCF_001956585.1 GCF_002860635.1                  65.2                  65.0      Actinomyces_naeslundii     1
 2: GCF_001937545.1 GCF_001937675.1                  78.8                  79.1            Actinomyces_oris     1
 3: GCF_001937545.1 GCF_001937725.1                  80.9                  73.5            Actinomyces_oris     2
 4: GCF_001937675.1 GCF_001937725.1                  78.4                  71.1            Actinomyces_oris     3
 5: GCF_001262035.1 GCF_003130125.1                  92.6                  93.5 Aggregatibacter_aphrophilus     1
 6: GCF_001262035.1 GCF_003252995.1                  82.8                  84.3 Aggregatibacter_aphrophilus     2
 7: GCF_003130125.1 GCF_003252995.1                  84.5                  84.8 Aggregatibacter_aphrophilus     3
 8: GCF_001059425.1 GCF_003130095.1                  92.4                  89.3      Aggregatibacter_segnis     1
 9: GCF_001059425.1 GCF_003252685.1                  90.1                  90.1      Aggregatibacter_segnis     2
10: GCF_003130095.1 GCF_003252685.1                  87.0                  89.8      Aggregatibacter_segnis     3

我试图旋转geom_points图的x标签。 before

标签已成功旋转,但图形为空(不显示数据)。

after

g <- ggplot(data = compareset, aes(x = Species, y =`% of similarity 1 in 2`, col = index))
g + geom_point()
g + theme(axis.text.x = element_text(angle = 90, hjust = 1))

1 个答案:

答案 0 :(得分:0)

该代码对我有用。看到这里

数据

set.seed(1)
myFun <- function(n) {
  a <- do.call(paste0, replicate(5, sample(LETTERS, n, TRUE), FALSE))
  paste0(a, sprintf("%04d", sample(9999, n, TRUE)), sample(LETTERS, n, TRUE))
}

n <- 20
compareset <- data.frame(Species= myFun(n=n), y= rnorm(n), index= factor(rep(c(1,2), n/2)))

您的代码

library(ggplot2)

g <- ggplot(data = compareset, aes(x = Species, y = y, col = index))
g <- g + geom_point()
g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1))
g

正如Z.Lin指出的,您没有将g + geom_point()的结果保存回g。这是我在使用您的代码时“自动”执行的操作,并且有效。所以这就是你应该尝试的。

编辑

感谢您的数据。通过为g重新分配新更改,它可以工作。在这里:

compareset <- data.frame(similarity= c(65.2, 78.8, 80.9, 78.4, 92.6,
                                       82.8, 84.5, 92.4, 90.1, 87.0),
                         Species= c("Actinomyces_naeslundii1", "Actinomyces_oris1", "Actinomyces_oris2", "Actinomyces_oris3", "Aggregatibacter_aphrophilus1",
                                  "Aggregatibacter_aphrophilus2", "Aggregatibacter_aphrophilus3", "Aggregatibacter_segnis1", "Aggregatibacter_segnis2", "Aggregatibacter_segnis3"),
                         index= c(1,1,2,3,1,2,3,1,2,3))

g <- ggplot(data = compareset, aes(x = Species, y = similarity, col = index))
g <- g + geom_point()
g <- g + theme(axis.text.x = element_text(angle = 90, hjust = 1))
g

plot