Question: change axis scale on ggplot2
gravatar for niyomiw88
6 weeks ago by
niyomiw880 wrote:


I am trying to plot my pca in ggplot and I've manage to get something (picture attached).

My question is, How do I spread the axis scales to make it look better. This is the worst ggplot that I've ever seen! Please help me fix it.

This is my code:

b <- ggplot(pca, aes(PC1, PC2, col = spp, shape = loc)) + geom_point(size = 3)
b <- b + scale_colour_manual(values = c("red", "blue", "green", "yellow", "brown","purple"))
b <- b + coord_equal() + theme_light()
b + xlab(paste0("PC1 (", signif(pve$pve[1], 3), "%)")) + ylab(paste0("PC2 (", signif(pve$pve[2], 3), "%)"))


P.S: I'm new to R and very eager to learn. I tried setting x and y lim (scale_x_continuous, expand_limits, coord_cartesian etc.)

UPDATE: Here are the first few lines of data, as per request;

structure(list(ind = c("IID", "\"$i\"", "filtered_seq1trimq10_LHA_EU21_1.final_snps_\"$i\"", 
"filtered_seq1trimq10_Tur09_1.final_snps_\"$i\"", "filtered_seq1trimq10_LHA_EU20_1.final_snps_\"$i\"", 
"filtered_seq1trimq10_LHA_AS201_1.final_snps_\"$i\""), PC1 = c("PC1", 
"0.0915729", "0.0946641", "0.0633914", "0.0951517", "-0.235733"
), PC2 = c("PC2", "0.040677", "0.0470369", "-0.0144166", "0.0490094", 


snp pca ggplot R • 228 views
ADD COMMENTlink modified 5 weeks ago by RamRS30k • written 6 weeks ago by niyomiw880

Can you add some of your data to the post using dput(head(pca))? It makes it easier for people to answer the question.

ADD REPLYlink modified 6 weeks ago • written 6 weeks ago by rpolicastro1.7k

Thank you for the tip. I updated my post :)

ADD REPLYlink written 6 weeks ago by niyomiw880
  1. Please see How to add images to a Biostars post to add your images properly. You need the direct link to the image, not the link to the webpage that has the image embedded (which is what you have used here)
  2. Please use the formatting bar (especially the code option) to present your post better. You can use backticks for inline code (`text` becomes text), or select a chunk of text and use the highlighted button to format it as a code block. I've done it for you this time.
ADD REPLYlink written 6 weeks ago by RamRS30k

Thank you so much for the helpful tips. I hardly ever post anything, because I can find answers to most of my questions here already.

ADD REPLYlink written 6 weeks ago by niyomiw880

Your example data is incomplete. Please post complete example data. Change the the intervals on Y-axis as they are numbers. It is not clear what is on the x-axis. If they are numbers (as y-axis values), change the interval. Why is ind character vector has $i in it?

ADD REPLYlink modified 6 weeks ago • written 6 weeks ago by cpad011214k

PC "numbers" are not numeric:

... PC1 = c("PC1", "0.0915729", "0.0946641", "0.0633914" ...

I think you imported the data header as the 1st row, which then turned all following numeric values into character. Please share the code you have used for data import.

ADD REPLYlink written 6 weeks ago by zx87549.6k
gravatar for Nicolas Rosewick
6 weeks ago by
Belgium, Brussels
Nicolas Rosewick9.0k wrote:

It seems that PC1 and PC2 are factors. Change them to numeric and it will work :

pca$PC1 <- as.numeric(as.character(pca$PC1))
pca$PC2 <- as.numeric(as.character(pca$PC2))

Also to spread the axis in ggplot you can use scale_x_continuous() and scale_y_continous()

Example :

# dummy data
dat <- data.frame(x=1:10,y=1:10)
# plot without axis spreading
dat %>% 
  ggplot(aes(x=x,y=y)) + 
# plot with axis spreading : x-axis [-5,15] ; y-axix[-2,12]
dat %>% 
  ggplot(aes(x=x,y=y)) + 
  geom_point() + 
  scale_x_continuous(limits = c(-5,15)) +
  scale_y_continuous(limits = c(-2,12))

enter image description here

ADD COMMENTlink modified 6 weeks ago • written 6 weeks ago by Nicolas Rosewick9.0k

Hey, Sorry for the late reply. But your suggestion worked. Thank you so much. However, it did change the population structure. I am not sure why? All i changed is factor to numeric. I am having trouble embedding the new plot. But I will soon!

ADD REPLYlink modified 5 weeks ago • written 5 weeks ago by niyomiw880
gravatar for RamRS
6 weeks ago by
Baylor College of Medicine, Houston, TX
RamRS30k wrote:

Try library(ggfortify); autoplot(pca) - that might be a good starting point. autoplot will accept a few more arguments to help customize the plot a little.

You can also add columns to the object subjected to the PCA operation that can be used to color/shape by (such as your spp and loc) Let's say that object is dat and you get pca by pca <- prcomp(dat).

You can add the spp and loc columns to dat, then

autoplot(prcomp(dat[,!(colnames(dat) %in% c("spp", "loc"))]), color = "spp", shape = "loc", label = TRUE)

See this page for more details:

ADD COMMENTlink modified 6 weeks ago • written 6 weeks ago by RamRS30k

I really only have one species that belong to 6 geographical areas. I made an error putting "spp" on the plot above. I did give ggfortify a try but honestly, i didn't try hard enough. I can give it another try.

ADD REPLYlink modified 6 weeks ago • written 6 weeks ago by niyomiw880
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1041 users visited in the last hour