使用ggplot绘图

时间:2015-08-26 11:10:18

标签: r plot ggplot2

我有这个我在这里加载的情节。我有兴趣从零线标记异常/远点(我将选择多远)的那些点。在x轴上,我希望只有异常将写在x轴上。

这是我的代码:

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") 

1 个答案:

答案 0 :(得分: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