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

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

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
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.
Log in to answer this question.
Can you add some of your data to the post using
dput(head(pca))? It makes it easier for people to answer the question.Thank you for the tip. I updated my post :)
codeoption) to present your post better. You can use backticks for inline code (`text` becomestext), 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.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.
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
indcharacter vector has$iin it?PC "numbers" are not numeric:
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.