controlling order of points in ggplot2 in R? -


suppose i'm plotting dense scatter plot in ggplot2 in r each point might labeled different color:

df <- data.frame(x=rnorm(500)) df$y = rnorm(500)*0.1 + df$x df$label <- c("a") df$label[50] <- "point" df$size <- 2 ggplot(df) + geom_point(aes(x=x, y=y, color=label, size=size)) 

when this, scatter point labeled "point" (green) plotted on top of red points have label "a". controls z ordering in ggplot, i.e. controls point on top of which? example, if wanted "a" points on top of points labeled "point" (meaning partially or hide point)? depend on alphanumerical ordering of labels? i'd find solution can translated rpy2. thanks

ggplot2 create plots layer-by-layer , within each layer, plotting order defined geom type. default plot in order appear in data.

where different, noted. example

geom_line

connect observations, ordered x value.

and

geom_path

connect observations in data order


there known issues regarding ordering of factors, , interesting note response of package author hadley

the display of plot should invariant order of data frame - else bug.


this quote in mind, layer drawn in specified order, overplotting can issue, when creating dense scatter plots. if want consistent plot (and not 1 relies on order in data frame) need think bit more.


create second layer

if want values appear above other values, can use subset argument create second layer drawn afterwards. need explicitly load plyr package .() work.

set.seed(1234) df <- data.frame(x=rnorm(500)) df$y = rnorm(500)*0.1 + df$x df$label <- c("a") df$label[50] <- "point" df$size <- 2 library(plyr) ggplot(df) + geom_point(aes(x = x, y = y, color = label, size = size)) +   geom_point(aes(x = x, y = y, color = label, size = size),               subset = .(label == 'point')) 

enter image description here

update

in ggplot2_2.0.0, subset argument deprecated. use e.g. base::subset select relevant data specified in data argument. , no need load plyr:

ggplot(df) +   geom_point(aes(x = x, y = y, color = label,  size = size)) +   geom_point(data = subset(df, label == 'point'),              aes(x = x, y = y, color = label, size = size)) 

or use alpha

another approach avoid problem of overplotting set alpha (transparancy) of points. not effective explicit second layer approach above, however, judicious use of scale_alpha_manual should able work.

eg

# set alpha = 1 (no transparency) point(s) of interest # , low value otherwise ggplot(df) + geom_point(aes(x=x, y=y, color=label, size=size,alpha = label)) +    scale_alpha_manual(guide='none', values = list(a = 0.2, point = 1)) 

enter image description here


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -