首页 文章

使用ggplot2的NP图表

提问于
浏览
0

我如何使用ggplot2生成NP图表?

我做了简单的Rscript,生成条形图,点图表 . 我是通过csv文件提供数据的 . 我需要指定多少列,在gplot函数中我需要传递哪些参数?

我是R,ggplots的新手 .

EDITThis是NP图表的含义 .

当前代码尝试:

#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()

在.csv文件中有2列x,y我通过 Rscript cne.R 11_16.pdf "point" "data.csv" 调用此脚本 .


非常感谢@ mathematical.coffee这是我需要的
1>我正在读取包含以下数据的csv文件中的数据

这是我的数据月份,评分“Jan”,“37.50”“Feb”,“32.94”“Mar”,“25.00”“Apr”,“33.33”“May”,“33.08”“Jun”,“29.09”“ Jul“,”12.00“”Aug“,”10.00“”Sep“,”6.00“”Oct“,”23.00“”Nov“,”9.00“”Dec“,”14.00“

2>我想在每个绘图点上显示值 . 并且还显示UCL,Cl,LCL的值,并为x和y赋予不同的标签 .

我读取数据时的问题与csv文件中的顺序不同 . 怎么解决?

1 回答

  • 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

相关问题