Geom_point: Place the overlapping points with the highest values ​​on top of the others.

I visualize the panel dataset with geom_point, where y = var1, x = yearand color = var2. The problem is that there are many overlapping points, even with horizontal jitter.

Reducing the size of the point or setting a low alpha value is undesirable, as it reduces the visual impact of the second variable, which has a very long right skew. I would like ggplot to put the points with the highest values var2on top of all other overlapping points.

Playable example:

df <- data.frame(diamonds)

ggplot(data = df,aes(x=factor(cut),y=carat,colour=price)) + 
  geom_point(position=position_jitter(width=.4))+
  scale_colour_gradientn(colours=c("grey20","orange","orange3"))

How to place the points with the highest values ​​on df$pricetop of an overlapping stack of points?

+5
source share
2

, ,

library(grid)

d <- data.frame(x=c(0.5,0.52),y=c(0.6,0.6), fill=c("blue","red"),
                stringsAsFactors=FALSE)

grid.newpage()
with(d,grid.points(x,y,def='npc', pch=21,gp=gpar(cex=5, fill=fill)))
with(d[c(2,1),], grid.points(x,y-0.2,def='npc', pch=21,
                             gp=gpar(cex=5, fill=fill)))

.frame , ggplot2 :)

library(ggplot2)
library(plyr)
df <- diamonds[order(diamonds$price, decreasing=TRUE), ]
# alternative with plyr
df <- arrange(diamonds, desc(price))
last_plot() %+% df
+5

ggplot2 , , . . , , ; rank(var2).

df$price :

df <- data.frame(diamonds)
df$orderrank <- rank(df$price,ties.method="first")

ggplot(data = df,aes(x=factor(cut),y=carat,colour=price, order=orderrank)) + 
  geom_point(position=position_jitter(width=.4))+
  scale_colour_gradientn(colours=c("grey20","orange","orange3"))

:

comparison of unordered and ordered plots

( , .)

+1
source

All Articles