change axis scale on ggplot2
2
0
Entering edit mode
4.3 years ago
niyomiw88 • 0

Hi,

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), "%)"))

image: https://ibb.co/XLp4hCk

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", 
"0.100445"),

pca1

SNP ggplot pca R • 6.2k views
ADD COMMENT
1
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
  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.
    code_formatting
ADD REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
2
Entering edit mode
4.3 years ago

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)) + 
  geom_point()
# 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 COMMENT
0
Entering edit mode

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 REPLY
0
Entering edit mode
4.3 years ago
Ram 44k

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

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

See this page for more details: https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_pca.html

ADD COMMENT
0
Entering edit mode

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 REPLY

Login before adding your answer.

Traffic: 2682 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6