我已经发现了这个问题,它在过去对我有所帮助: grid: Grid graphics flickering
但是,我已经在我的代码中修复了这个问题,并且在绘制多行时仍然会看到闪烁。使用plot命令绘制的黑线看起来很好,但是当数据从传感器流入时,红线闪烁。
(我稍后会添加轴标题)
这里是有问题的代码(我尝试堆积图表并且没有帮助):
dev.hold
plot(dataX, dataY, type="l", lwd=2, ylim = yLimits)
lines(dataX, dataY2, type="l", lwd=2, col="red")
#par(new=TRUE)
#plot(dataX, dataY2, type="l", lwd=2, ylim = yLimits, col = "red")
dev.flush
整个文件不是很大(约120行)所以我会提供完整的上下文。以上几行即将结束。
# Clean up if last run crashed:
if(exists("com"))
{
close(com)
}
#
# Reset environment
#
# Set working directory to dir of this source file.
setwd(getSrcDirectory(function(x) {x}))
rm(list = ls()) # Remove environemnent variables
graphics.off() # Close any open graphics
#
# Libraries
#
library(serial)
#
# Functions
#
MovingAverage <- function(data, window)
{
return (filter(data, rep(1/window, window), sides = 1, circular = 1))
}
PulseFilter <- function(data)
{
highPass <- 45
lowPass1 <- 6
lowPass2 <- 6
data <- data - MovingAverage(data, highPass)
data <- MovingAverage(data, lowPass1)
data <- MovingAverage(data, lowPass2)
return (data)
}
#
# Script
#
# configures settings for con. Modify this file for local configuration.
source("com_config.R")
open(con)
stopTime <- Sys.time() + 10
textRaw <- ""
while(Sys.time() < stopTime)
{
textNew <- read.serialConnection(con)
if(0 < nchar(textNew))
{
textRaw <- textNew
textLines <- strsplit(textRaw, "\n")
for(i in 1:length(textLines[[1]]))
{
textData <- strsplit(textLines[[1]][i], ", ")
if(4 == length(textData[[1]]))
{
if(!exists("dataTime"))
{
dataTime <- as.numeric(textData[[1]][1])/1000
dataIR <- as.numeric(textData[[1]][2])
dataRed <- as.numeric(textData[[1]][3])
dataState <- as.numeric(textData[[1]][4])
}
else
{
dataTime <- c(dataTime, as.numeric(textData[[1]][1])/1000)
dataIR <- c(dataIR, as.numeric(textData[[1]][2]))
dataRed <- c(dataRed, as.numeric(textData[[1]][3]))
dataState <- c(dataState, as.numeric(textData[[1]][4]))
}
}
}
filterData <- 0
adjustOffset <- 1
if(exists("dataTime"))
{
dataX <- dataTime
dataY <- dataIR
dataY2 <- dataRed
if(1 == filterData) # Enable filtering of displayed data
{
if(100 < length(dataX))
{
dataY <- PulseFilter(dataY)
dataY2 <- PulseFilter(dataY2)
}
}
xLimits <- c(0, 0)
xLimits[2] <- max(dataX)
xLimits[1] <- xLimits[2] - 10
dataY <- dataY[xLimits[1] < dataX]
dataY2 <- dataY2[xLimits[1] < dataX]
dataX <- dataX[xLimits[1] < dataX]
if(1 == adjustOffset)
{
dataY <- dataY - mean(dataY)
dataY2 <- dataY2 - mean(dataY2)
}
yLimits <- c(0, 0)
yLimits[2] <- max(c(dataY, dataY2))
yLimits[1] <- min(c(dataY, dataY2))
dev.hold
plot(dataX, dataY, type="l", lwd=2, ylim = yLimits)
lines(dataX, dataY2, type="l", lwd=2, col="red")
#par(new=TRUE)
#plot(dataX, dataY2, type="l", lwd=2, ylim = yLimits, col = "red")
dev.flush
}
stopTime <- Sys.time() + 1
}
}
close(con)
rm(con) # remove con, so I can check if the script crashed.
答案 0 :(得分:1)
matplot应强制一次性绘制绘图,这样可以减少闪烁
matplot(dataX, cbind(dataY, dataY2), type="l", lwd=2, ylim = yLimits, col = c("black", "red"), lty = 1)