r - Plot points with 3 different colors -
i have following 2 columns part of dataframe , function below:
x 705.7553 789.2789 689.7431 559.8025 629.9365 564.9095 y -0.0596123270783229 0.644158691971081 -0.433854284204926 -0.365746109442603 0.566685929975495 0.398462720589891
function:
plotm<- function( all.res, m, u ) { plot(data$x, data$y, log="x", pch=20, cex=0.7, col = ifelse( data$y < m, "red", "black" ), xlab = "y", ylab = expression(log[2]~fold~change)) }
function call:
plotm(data, .05) # plots graph red points when data$y < m , black when data$y > m
but i'm interested in following:
plotm(data, 0.05, 0.1 ) # plot graph red points when data$y < m, green when data$y > u, , rest colored black
can please? thanks
we first generate toy data:
set.seed(1) x <- rnorm(100) y <- rnorm(100)
you can like:
m <- c(-inf, -0.5, 0.5, inf) # specify cut points, here -0.5 , 0.5 cols <- cut(y, breaks = m) plot(x, y, col = cols)
we here use fact cut
returns factor
, , use numeric values of levels colors. write like:
cols <- as.character(cut(y, breaks = m, labels = c("black", "red", "green")))
to construct colours.
alternatively, can use nest ifelse
. i.e.
plot(x, y, col = ifelse(y < -0.5, "red", ifelse(y > 0.5, "green", "black")))
Comments
Post a Comment