Almog Angel
Almog Angel

Reputation: 55

ggplot2 return an empty plot after rotating x-lables

I got a data for comparing two genomes (column 1 and 2) and their similarity with the index indicating the number of genomes comparisons for the species:

 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

I was trying to rotate the x-labels of a geom_points graph. first plot

The label was successfully rotated but the graph was empty (show no data).

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

Upvotes: 0

Views: 327

Answers (1)

user11538509
user11538509

Reputation:

The code works for me. See here

Data

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

Your code

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

As Z.Lin points out you didn't save the result of g + geom_point() back to g. This is what I did "automatically" while using your code and it worked. So this is what you should try.

EDIT

Thanks for the data. With reassigning new changes to g it works. Here:

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

Upvotes: 1

Related Questions