我的数据框看起来像这样
Datetime <- c("2015-09-29AM", "2015-09-29PM" ,"2015-09-30AM", "2015-09-30PM", "2015-10-01AM" ,"2015-10-01PM"
,"2015-10-02AM", "2015-10-02PM" ,"2015-10-03AM" ,"2015-10-03PM", "2015-10-04AM" ,"2015-10-04PM"
,"2015-10-05AM", "2015-10-05PM", "2015-10-06AM" ,"2015-10-06PM")
FailRate_M1 <- c(0.0000000,0.0000000,0.9615385,0.9009009,0.0000000,1.4492754,1.5151515,0.0000000,0.8849558,0.0000000,4.4444444,0.7142857
,0.0000000,10.3448276,0.0000000,0.0000000)
df1 <- data.frame(Datetime,FailRate_M1)
现在我使用来自&#34; qichart&#34;的qic函数。打包并获得此图。
library(qicharts)
qic(FailRate_M1,
x = Datetime,
data = df1,
chart = 'c',
runvals = TRUE,
cex = 1.2,
main = 'Measurement Fail Rate (M1)',
ylab = 'MFR (%)',
xlab = 'Datetime')
可以使用ggplot绘制此图吗?或者它可以转换为ggplot格式?请提供您的意见并帮助我解决这个问题。
有许多功能都有自己定制的绘图方式,但我最好还是想看看我们是否可以将这些绘图转换为ggplot。
我尝试了以下
p1<- qic(FailRate_M1,
x = Datetime,
data = df1,
chart = 'c',
runvals = TRUE,
cex = 1.2,
main = 'Measurement Fail Rate (M1)',
ylab = 'MFR (%)',
xlab = 'Datetime')
然后我尝试使用ggplot
library(ggplot2)
sp <- ggplot(p1, aes(x = Datetime, y = FailRate_M1))+
geom_point(size=2.5)
sp
并收到以下错误&#34; 错误:ggplot2不知道如何处理类qic的数据&#34;
答案 0 :(得分:3)
以Jason对您的p1数据框的回答为基础。
您可以访问qic函数返回的值,并在函数调用中使用print.out = TRUE在控制台中查看它们。
使用dplyr更新了答案:
library(dplyr)
library(ggplot2)
library(ggExtra)# optional for plot tidying
df2 <- data.frame(p1$labels,p1$y,p1$cl,p1$ucl) %>%
dplyr::rename(y = p1.y,
Datetime = p1.labels,
cl = p1.cl,
ucl= p1.ucl)
p <- ggplot(df2, aes(x = Datetime, y = y, group = 1)) +
theme_minimal() +
geom_line(color = "steelblue", size = 1) +
geom_point(color = "steelblue", size = 3) +
geom_point(data = subset(df2, FailRate_M1 >= 10), color = "red", size = 4) +
geom_hline(aes(yintercept = cl)) +
geom_hline(aes(yintercept = ucl)) +
labs(title = "Measurement Fail Rate (M1)",
y = "MFR (%)")
p <- p + removeGrid() + rotateTextX() #from ggExtra,personal preference
p
拦截线不再是硬编码的。 我从所有运行/控制图表中删除了无关的水平线,ggExtra的removeGrid和rotateTextX()比同等的ggplot2语法更容易记住(至少对我而言)
答案 1 :(得分:2)
我不熟悉qicharts::qic
正在做什么,但下面用ggplot2
模仿图形的核心元素:
library(ggplot2)
my_value <- min(df1$FailRate_M1) + 6
ggplot(df1, aes(x = Datetime, y = FailRate_M1, group = 1)) +
geom_line(color = "steelblue", size = 1) +
geom_point(color = "lightgreen", size = 3) +
geom_point(data = subset(df1, FailRate_M1 >= 10), color = "red", size = 4) +
geom_hline(aes(yintercept = c(1.3, 4.8))) +
geom_hline(aes(yintercept = my_value), linetype = 2) +
labs(title = "Measurement Fail Rate (M1)",
y = "MFR (%)")
一些有助于理解的笔记:
x
是一个因素时,您需要使用aes(group = 1)
,以便ggplot()
知道数据&#34;属于一起&#34;应该&#34;连接&#34;。在这种情况下,用一条线。geom_point
的多次调用。第一个将绘制所有点。第二个将仅显示subset
数据,其中df1$FailRate_M1 >= 10
的颜色为red
。可能不明显的是lightgreen
点下面有red
点。geom_hline
时,我正在使用yintercepts
功能绘制多个c()
。或者,您可以拨打geom_hline
两次。