i'm plotting segments ggplot , want color segments depending on associated value, comparing values coming different sources. original data frame has more 10000 values per experiment, plotting few subset (~30-50).
so far have used strategy using colorramppallete:
col <- colorramppalette(c("blue", "white", "red")) then generate colors based on each value of every experiment, whole range having base red , blue gradient (median in ~0)
dat_original$colvalue1 <- col(10)[as.numeric(cut(dat_original$value1, breaks=10))] dat_original$colvalue2 <- col(10)[as.numeric(cut(dat_original$value2, breaks=10))] after subsetting, use assigned color color segment. want compare values different experiments, when generate colors, distributions different (some have wider range) colors cannot compared (eg similar negative value has whiter color in 1 compared other experiment)
as want compare segments/values different experiments, , range can differ want assign values based on limited distribution, ie. normal distribution / range, e.g. -5,5 mean=0 , sd=1, colors intensity can compared. more specifically, if value in 1 -1.2 , in second -2 latter should more blue, if 1 -1.2 , other -1.25 difference should non-existing.
a dummy data frame of how subset looks like
ident <- c("abc", "abc", "abc", "def", "def", "ghf", "ghf", "ghf") a3 <- c(10, 15, 20, 30, 45, 60, 80, 90) a4 <- c(14, 19, 28, 40, 55, 75, 85, 100) value1 <- c(-3.7,-1.8,-1.5, 2,0.5, -0.5,-0.9, -1.5) value2 <- c(-2.2,-3.8,-4, 1.2,1.5, -0.8,-1.9, -0.5) # dat <- data.frame(ident, a1, a2, a3, a4, value1, value2) #plot, i'm using after assigning colors colvalue1 or 2 ggplot(dat) + geom_segment(aes(x=a3, xend=a4, y=as.numeric(ident), yend=as.numeric(ident)), size=4, color=dat$colvalue1) + geom_segment(aes(x=a3, xend=a4, y=as.numeric(ident)+0.2, yend=as.numeric(ident)+0.2), size=4, , color=dat$colvalue1) any appreciated.
you can map numeric vector color , use scale_color_gradient()
ggplot(dat) + geom_segment(aes(x=a3, xend=a4, y=as.numeric(ident), yend=as.numeric(ident),color=value1), size=4) + geom_segment(aes(x=a3, xend=a4, y=as.numeric(ident)+0.2, yend=as.numeric(ident)+0.2,color=value2),size=4) + scale_color_gradient2(low="blue",high="red")+ theme(panel.background=element_rect(fill="grey")) 
Comments
Post a Comment