ggplot2受异常值影响的色标

ARo*_*son 11 gradient r scale outliers ggplot2

我对一些异常值有困难,使得色标无用.

我的数据有一个基于范围的Length变量,但通常会有一些更大的值.以下示例数据具有介于500和1500之间的95个值,以及超过50,000的5个值.当我想看到500到1500之间的颜色变化时,由此产生的颜色图例倾向于使用10k,20k,... 70k的颜色变化.真的,1300左右的任何东西应该是相同的纯色(可能是中位+/-疯狂),但我不知道在哪里定义.

我对任何ggplot解决方案持开放态度,但理想情况下,较低的值将是红色,中间白色和较高的蓝色(低值很差).在我自己的数据集中,date是ggplot aes()中as.POSIXct()的实际日期,但似乎不影响该示例.

#example data
date <- sample(x=1:10,size=100,replace=T)
stateabbr <- sample(x=1:50,size=100,replace=T)
Length <- c(sample(x=500:1500,size=95,replace=T),60000,55000,70000,50000,65000)
x <- data.frame(date=date,stateabbr=stateabbr,Length=Length)

#main plot
(g <- ggplot(data=x,aes(x=date,y=factor(stateabbr))) +
  geom_point(aes(color=as.numeric(as.character(Length))),alpha=3/4,size=4) + 
  #scale_x_datetime(labels=date_format("%m/%d")) + 
  opts(title="Date and State") + xlab("Date") + ylab("State"))

#problem
g + scale_color_gradient2("Length",midpoint=median(x$Length))
Run Code Online (Sandbox Code Playgroud)

添加trans ="log"或"sqrt"也不是很有效.

谢谢您的帮助!

jor*_*ran 9

这是一个有点棘手的选择:

#Create a new variable indicating the unusual values
x$Length1 <- "> 1500"
x$Length1[x$Length <= 1500] <- NA

#main plot
# Using fill - tricky!
g <- ggplot() +
  geom_point(data = subset(x,Length <= 1500),
             aes(x=date,y=factor(stateabbr),color=Length),size=4) + 
  geom_point(data = subset(x,Length > 1500),
             aes(x=date,y=factor(stateabbr),fill=Length1),size=4)+
  opts(title="Date and State") + xlab("Date") + ylab("State")

#problem
g + scale_color_gradient2("Length",midpoint=median(x$Length))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

因此,这里棘手的部分是使用fill积分,以说服ggplot创造另一个传奇.显然,您可以使用填充比例的不同标签和颜色对其进行自定义.

还有一件事,请阅读布兰登的回答.原则上,您可以通过获取外围值,使用cut为它们创建单独的分类变量来组合这两种方法,然后使用我的技巧和fill比例.这样你就可以指出多个外围点组.


Bra*_*sen 6

从我的评论中,看到?cut

x$colors <- cut(x$Length, breaks=c(0,500,1000,1300,max(x$Length)))

g <- ggplot(data=x,aes(x=date,y=factor(stateabbr),color=colors)) +
    geom_point() + 
    opts(title="Date and State") + 
    xlab("Date") + 
    ylab("State")
Run Code Online (Sandbox Code Playgroud)