Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206459

Set colors of Factors in R

$
0
0

I am attempting to make two scatter plots with ggplot2, which will have different groupings, but I would like the colors of the factors/groups to stay the same in both plots. The code below produces different colors depending on the order.

type<-c("Aorta", "Aorta", "Coronary", "Coronary", "Pulmonary", "Vein", "Vein")
PC1<-sample(x=1:10, size = 7)
PC2<-sample(x=1:10, size = 7)
pca<-data.frame(type, PC1, PC2)
ggplot(pca, aes(x=PC1, y = PC2, color = pca$type)) + 
    geom_point(size=5)


type2<-c("Aorta", "Aorta", "Coronary", "Coronary", "Pulmonary", "Vein", "Vein", "Kidney", "Kidney", "Stomach", "Stomach")
PC1<-sample(x=1:20, size = 11)
PC2<-sample(x=1:20, size = 11)
pca2<-data.frame(type2, PC1, PC2)
ggplot(pca2, aes(x=pca2$PC1, y = pca2$PC2, col=pca2$type2)) + 
    geom_point(size=5) 

I have attempted to do something similar to this suggestion (Manually setting group colors for ggplot2) , but am having some difficulty translating the bar in this exmaple to a scatter in my example.


Viewing all articles
Browse latest Browse all 206459

Trending Articles