首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ggplot2的NP图

使用ggplot2的NP图
EN

Stack Overflow用户
提问于 2012-01-23 01:22:28
回答 1查看 662关注 0票数 0

如何使用ggplot2生成NP图?

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

我对R,ggplots很陌生。

编辑是NP图的意思。

当前代码尝试:

代码语言:javascript
复制
#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文件中读取数据

这是我的数据月,按“一月”、"37.50“”二月“、"32.94”“三月”、"25.00“”4月“、"33.33”“五月”、"33.08“”六月“、"29.09”“七月”、"12.00“”8月“、"10.00”“9月”、"6.00“”10月“、"23.00”“11月”、"9.00“”12月“,"14.00“

2>我想在每个绘图点上显示值。同时显示UCL,Cl,LCL的值,并给出x和y的不同标签。

当我读取数据时,问题与csv文件中的顺序不同。怎么修呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-23 05:42:07

您可以将ggplot(tasks,aes(x=x,y=y))geom_linegeom_point结合起来,以获得逐点连接的线路。

如果您还想要绘制UCL/LCL/etc,则添加一个geom_hline (水平线)。要向这些行添加文本,可以使用geom_text

举个例子:

代码语言:javascript
复制
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)

这意味着:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8966316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档