2015-08-26 57 views
0

我有這個情節,我加載在這裏。我有興趣將遠離零線的異常/遠點(我將選擇多遠)標記出來。在x軸上,我希望只有異常將被寫入x軸。使用ggplot的情節

這是我的代碼:

x_4 <- seq(1000,9999,1) 
y1_4 <- comparison4[,1] 
y2_4 <- comparison4[,2] 
y3_4 <- comparison4[,3] 
df <- data.frame(x_4,y4_4,y4_4,y4_4) 
require(ggplot2) 
ggplot(df, aes(x_4)) +     
    geom_line(aes(y=y1_4), colour="red") + 
    geom_line(aes(y=y2_4), colour="green")+ 
    geom_line(aes(y=y3_4), colour="blue") + 
    scale_x_continuous(breaks=c(1000,2000,3000,4000,5000,6000,7000,8000,9000,9999)) + 
    geom_point(aes(y=y1_4),shape=8, colour="red",size=0.01)+ 
    geom_point(aes(y=y2_4),shape=6, colour="green",size=0.01)+ 
    geom_point(aes(y=y3_4),shape = 4, colour="blue",size=4)+ 
    xlab("Digit") + 
    ylab("Frequency") + 
    ggtitle("Four Digits Benford Distribution") 
+0

你的代碼不能運行。 – hrbrmstr

+1

我認爲你需要提供'compare4'? – SN248

+0

也許你應該給你的問題一個更具體的標題。 – maj

回答

0

您可以根據閾值繪製之前子集數據。這裏有樣本數據:

# sample data 
df <- data.frame(x_4 = seq(1000,9999,1), 
       y1_4 = rnorm(9000), 
       y2_4 = rnorm(9000), 
       y3_4 = rnorm(9000)) 

library(dplyr) 
library(tidyr) 

## Set a threshold value 
threshold <- 2 

## Turn data to long form and keep only those above 0 +- threshold 
dft <- gather(data = df,key,value,-x_4) %>% 
    filter(value > threshold | value < -threshold) 

require(ggplot2) 

ggplot(dft, aes(x=x_4,y=value,colour=key,shape=key)) +     
    geom_line() + 
    geom_point() + 
    scale_x_continuous(breaks=c(1000,2000,3000,4000,5000,6000,7000,8000,9000,9999)) + 
    scale_color_manual(values = c("red","green","blue")) + 
    scale_shape_manual(values = c(8,6,4)) + 
    xlab("Digit") + 
    ylab("Frequency") + 
    ggtitle("Four Digits Benford Distribution") 

enter image description here