【问题标题】:How to assign the data of table to the coordinates X and Y of a (qcc) plot with R (Rstudio)?如何将表格的数据分配给带有R(Rstudio)的(qcc)图的坐标X和Y?
【发布时间】:2017-11-17 11:37:49
【问题描述】:

我是 R 脚本的新手,不要对我太苛刻:)。

所以我有一个如下所示的 CSV 文件:

Day |Date       |Temperature    |Pression   |Flow
----|-----------|---------------|-----------|------
1   |5/10/2017  |85             |4          |100
2   |5/11/2017  |85             |4.5        |102
3   |5/12/2017  |88             |5.2        |103
4   |5/13/2017  |83             |4.1        |99 
..  |.....      |..             |...        |..

TABLE

我执行了必要的步骤来导入我的 CSV 数据。

我使用 qcc 函数通过 Xbar 进行质量控制。

我得到了我的图表,但坐标 X 和 Y 是倒置的。 Xbargrap

我希望在 X 坐标中,日期的值具有正确的格式(不是 17300,而是 5-10-2017),而在 Y 坐标中,坐标是压力。

我尝试了几个,但我不能:/

这是我的程序:

# Library
library(qcc)
library(readr)
library(Rserve)
Rserve(args = "--vanilla")

# Data column filter from CSV file imported
Test <- read_csv("C:/Users/..../Desktop/Test.csv", 
                 col_types = cols(Date = col_date(format = "%m/%d/%Y")))


diams <- qcc.groups(Test$Date,Test$Pression)
#diams <- with(Test, qcc.groups(Day, Temperature))

#Background color
qcc.options(bg.margin = "white", bg.figure = "gray95")

#Xbar graph (means of a continuous process variable)
qcc(
    data = diams,
    type = "xbar",
    sizes = 5,
    title = "Sample X-bar Chart Title", # Replacement title
    digits = 2, # Limit the signifciant figures
    plot = TRUE)

你能帮帮我吗?

感谢您的回答。

【问题讨论】:

    标签: r csv rstudio


    【解决方案1】:

    您应该能够得到以下图表

    从下面的代码:

     set.seed(5557)
       df3 <- data.frame(
      "index" = 1:48,
      "date" = seq.Date(from = as.Date("2017-5-10"),
                        to = as.Date("2017-6-26" ), by="1 day"),
      "Temperature" = rnorm(n=48, mean=83, sd=5)
       )
    
       require(ggQC)
       require(ggplot2)
       ggplot(df3, aes(x=date, y=Temperature)) + 
         stat_QC(method = "XmR") + #draw the QC lines
         stat_QC_labels(method = "XmR") + # label the QC lines
         geom_point() + geom_line() # draw points and lines
    

    【讨论】:

    • 非常感谢@Kgrey 的帮助。我只有一个问题,你知道我怎样才能拥有 UCL 和 LCL 的价值吗?因为使用函数 XmR_UCL() 或 XmR_UCL()。它不起作用。
    • 没关系,我找到了:v_ucl &lt;-xBar_one_UCL(Test$Temperature)v_lcl &lt;-xBar_one_LCL(Test$Temperature)
    【解决方案2】:

    可以使用 ggQC 包实现带有控制图线的 ggplot

    这里有 2 个模拟您的数据集的示例(加上分面奖励)

    每天一次观察

    #Simulate some data
    df <- data.frame(
      "index" = 1:48,
      "date" = seq.Date(from = as.Date("2017-5-10"),
                        to = as.Date("2017-6-26" ), 
               by="1 day"),
      "pression" = rnorm(n=48, mean = 4.7, sd = .2)
    )
    
    require(ggQC)
    require(ggplot2)
    
    ggplot(df, aes(x=date, y=pression)) + 
       stat_QC(method = "XmR") + #draw the QC lines
       stat_QC_labels(method = "XmR") + # label the QC lines
       geom_point() + geom_line() # draw points and lines
    

    每天 5 次观察

    #Make some data with group size = 5
    df2 <- data.frame(
           "index" = 1:48*5,
           "date" = rep(
              seq.Date(from = as.Date("2017-5-10"),
                       to = as.Date("2017-6-26" ), 
                       by="1 day"),
              each = 5
              ),
          "pression" = rnorm(n=48*5, mean = 4.7, sd = .2)
           )
    
    require(ggQC)
    require(ggplot2)
    ggplot(df2, aes(x=date, y=pression)) + 
       geom_point(alpha=.3) + # show the individuals
       stat_summary(fun.y = mean, geom="point", color="black") + #show mean by day
       stat_summary(fun.y = mean, geom="line", color="red") + #draw a line
       stat_QC() + # draw the qc lines
       stat_QC_labels() # write the labels on the lines
    

    因为它的 ggplot 你还可以做一些很酷的事情,比如按月分面(例如)

    df2$month <- cut(df2$date, breaks = "1 month")
    ggplot(df2, aes(x=date, y=pression)) + 
      geom_point(alpha=.3) + # show the individuals
      stat_summary(fun.y = mean, geom="point", color="black") + #show mean by day
      stat_summary(fun.y = mean, geom="line", color="red") + #draw a line
      stat_QC() + # draw the qc lines
      stat_QC_labels() + # write the labels on the lines
      facet_grid(.~month, scales = "free_x")
    

    【讨论】:

    • 感谢 Kgrey 的帮助,我成功解封了一些点,但此代码 df &lt;- data.frame(Test, Test$Date&lt;- seq.Date(from = as.Date("2014-9-15"), to = as.Date("2017-6-8"), by="1 month") ) 有错误。这是错误:Error in $&lt;-.data.frame(*tmp*, Date, value = c(16328, 16358, 16389, : replacement has 33 rows, data has 871
    • 我不确定您的代码的语法。但 33 行来自日期范围之间的 33 个月。我认为您想要以下之一:#Make the dates df$Date
    • 我在这里找到了解决方案:link。但是我有 ggQC,我的图表上没有 LCL 和 UCL 的数据。这是代码:date &lt;- seq(from = valueDatemin, to = valueDatemax, by="1 day") Test &lt;- Test[Test$Date %in% date, ] ... p &lt;- ggplot(Test, aes(x=Date, y=Temperature)) + stat_QC_labels(method = "xBar.rBar") + # label the QC lines stat_QC(method = "xBar.rBar") +geom_point() + geom_line()
    【解决方案3】:

    我会说:尝试学习使用 data.table 和 ggplot2 包。如果你使用这些......那么绘制日期也会变得更容易。

    你可以试试这段代码

    library(data.table)
    library(ggplot2)
    
    # Use fread (fast-read) to get the csv
    table = fread("C:/Users/..../Desktop/Test.csv")
    # convert to date
    table[,Date := as.Date(Date,format = "%m/%d/%Y)]
    
    #use ggplot to plot the a line
    # aes stands for aesthetics
    ggplot(data = table,aes(x = Date,y = Pression)) + geom_line()
    # If you want to keep your lines.. you can add them with geom_vline()
    

    【讨论】:

    • 您的程序可以运行,但我想将 qcc 图与 UCL 一起使用 LCL 限制。
    猜你喜欢
    • 2021-11-21
    • 2017-03-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多