r - Hardcode hexadecimal string values as color in ggplot -


i have data frame "x" , "y" columns numeric values, , third column "cluster" hexidecimal string, example seen below:

library(ggplot2) library(scales) collist = c(scales::hue_pal()(3),"#520090") dat = data.frame(x=runif(100,0,1),y=runif(100,0,1),cluster=sample(1:4, 100, replace=t)) dat$cluster = factor(dat$cluster) levels(dat$cluster) = c(collist) head(dat) 

i trying create scatterplot "x" , "y" columns mapped x , y axis, , points colored according hexadecimal value stored in "cluster" column. have tried following:

ggplot(dat,aes(x,y))+ geom_point(aes(colour = cluster), alpha=0.5) 

however, assigns default first 4 values stored in scales::hue_pal()(4), , have changed last 1 dark purple color hexadecimal value #520090. trying change default hexadecimal values appearing text in legend. tried unsuccessfully hardcode in "cluster 1", "cluster 2", ..., "cluster 4" legend text:

ggplot(dat,aes(x,y))+ geom_point(aes(colour = cluster), alpha=0.5) + theme(legend.text = element_text("cluster 1","cluster 2","cluster 3","cluster 4")) 

any advice appreciated!

in order color dots based on cluster identity, cluster name (i.e., hex values) needd mapped set of aesthetic values.

since want have hex values cluster column represent actual colors, can use scale_color_manual function , give levels of cluster column values parameter. changes labels, set desired labels value.

ggplot(dat, aes(x,y)) + geom_point(aes(colour = cluster), alpha=0.5) + scale_color_manual(values = levels(dat$cluster),                     labels = c("cluster1","cluster2","cluster3", "cluster4")) 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -