2012-01-23 7 views
0

ggplot2를 사용하여 NP 차트를 생성하는 방법은 무엇입니까?ggplot2를 사용하는 NP 차트 ​​

막대 그래프, 포인트 차트를 생성하는 간단한 Rscript를 만들었습니다. CSV 파일로 데이터를 제공하고 있습니다. 얼마나 많은 컬럼을 지정해야하고 gplot 함수에서 어떤 인수를 전달해야합니까?

저는 R에 매우 익숙합니다. ggplots입니다.

편집 : This은 NP 차트의 의미입니다.

현재 코드 시도 : 내가 Rscript cne.R 11_16.pdf "point" "data.csv"하여이 스크립트를 호출 Y .CSV 파일에서

#load library ggplot2 
library(ggplot2) 

#get arguments 
args <- commandArgs(TRUE) 
pdfname <- args[1] 
graphtype <- args[2] 
datafile <- args[3] 

#read csv file 
tasks <- read.csv(datafile , header = T) 
#name the pdf from passed arg 1 
pdf(pdfname) 

#main magic that generates the graph 
qplot(x,y, data=tasks, geom = graphtype) 
#clean up 
dev.off() 

2 열 X가 있습니다. 대단히 @이 mathematical.coffee


덕분에 내가 필요하지만
1> 내가 CSV 파일에서 데이터를 읽는하고 다음 포함 된 데이터

이 내 데이터입니다

월, 속도 " 월 ","37.50 " "2월 ","32.94 " "월 ","25.00 " "4월 ","33.33 " "월 ","33.08 " "6월 ","29.09 " "7월 " , "12.00" "Aug", "10.00" "9 월", "6.00" "시월", "23.00" "11월", "9.00" "12월", "14.00"

2> 나는 각각의 플로팅 포인트에 값을 표시합니다. 또한 UCL, Cl, LCL에 대한 값을 표시하고 x 및 y에 다른 레이블을 제공합니다.

문제 데이터를 읽을 때 문제가 csv 파일에서와 같은 순서가 아닙니다. 그것을 고치는 방법?

+1

이/당신이 현재 차트를 생성하는 일의 작은 재현 할 예를 들어 줄 수 있습니까? 데이터가 어떤 형식인지 알지 못한다면 전달할 인수에 대해 조언 할 수 없습니다. –

+1

용어 NP 차트 ​​정의에 도움이 될 수도 있습니다. 나는 당신이 이것을 가정한다고 생각합니다 - http://en.wikipedia.org/wiki/Np-chart. – neilfws

+0

답장을 보내 주셔서 감사합니다. @ mathematical.coffee : CSV 파일을 읽고 pdf 형식으로 막대를 생성하는 스크립트를 만들었습니다. ' #LOAD 라이브러리 ggplot2 라이브러리 (ggplot2) #get 인수 인수 <- commandArgs (TRUE) pdfname <- 인수 [1] graphtype <- 인수 [2] 데이터 파일 <- 인수 [3] #read CSV 파일 작업 <- read.csv (데이터 파일 헤더 = T)가 통과 ARG 1 PDF (pdfname)에서 PDF를 # NAME 그래프 qplot 생성 마법 (X, Y #main , data = tasks, geom = graphtype) # 클린 업 dev.off()' .csv 파일에 2 열 x, y가 있습니다. 이 스크립트는'Rscript cne.R 11_16.pdf '라고합니다. freqpoly "data.csv" – henna

답변

2

ggplot(tasks,aes(x=x,y=y))geom_linegeom_point을 결합하여 포인트로 연결된 행을 얻습니다.

추가로 UCL/LCL/등을 그리려면 geom_hline (가로줄)을 추가하십시오. 이 줄에 텍스트를 추가하려면 geom_text을 사용할 수 있습니다.

예 :

library(ggplot2) 

# generate some data to use, say monthly up to a year from today. 
n <- 12 
tasks <- data.frame(
    x = seq(Sys.Date(),by="month",length=n), 
    y = runif(n)) 
CL = median(tasks$y)  # substitue however you calculate CL here 
LCL = quantile(tasks$y,.25) # substitue however you calculate LCL here 
UCL = quantile(tasks$y,.75) # substitue however you calculate UCL here 
limits = c(UCL,CL,LCL) 
lbls = c('UCL','CL','LCL') 

p <- ggplot(tasks,aes(x=x,y=y)) +   # store x/y values 
    geom_line() +       # add line 
    geom_point(aes(colour=(y>LCL&y<UCL))) + # add points, colour if outside limits 
    opts(legend.position='none',   # remove legend for colours 
      axis.text.x=theme_text(angle=90)) # rotate x axis labels 

# Now add in the limits. 
# horizontal lines + dashed for upper/lower and solid for the CL 
p <- p + geom_hline(aes(yintercept=limits,linetype=lbls)) +   # draw lines 
    geom_text(aes(y=limits,x=tasks$x[n],label=lbls,vjust=-0.2,cex=.8)) # draw text 

# display 
print(p) 

제공 : 당신이 시도 무엇

an np-plot