[英]How to assign the data of table to the coordinates X and Y of a (qcc) plot with R (Rstudio)?
我是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
.. |..... |.. |... |..
我執行了導入CSV數據的必要步驟。
我使用qcc函數對Xbar進行質量控制。
我得到我的圖,但是坐標X和Y顛倒了。 Xbargrap
我希望在X坐標中使用正確格式的Date的值(不是17300,而是在5-10-2017),在Y坐標中使用Pressure的坐標。
我嘗試了幾次,但不能:
這是我的程序:
# 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)
你可以幫幫我嗎 ?
謝謝您的回答。
我會說:嘗試學習使用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()
帶有控制圖線的ggplot可以通過ggQC軟件包來實現
這是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,所以您還可以按月完成諸如facet之類的很酷的事情(例如)
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")
從下面的代碼:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.