I have the following R script:
X <- read.table("/tmp/run178/data/monitor/portal_free_heap_monitor.log", header=T, sep=";")
P1 <- subset(X, Server=="PortalServer1")
P2 <- subset(X, Server=="PortalServer2")
png("mygraph.png")
plot(P1$Time, P1$HeapFreePercent, type="l", col="red")
lines(P2$Time, P2$HeapFreePercent, col="g开发者_JS百科reen")
q()
In resulting PNG image data for "PortalServer1" is drawn with dots and in black, but it's supposed to be drawn with red lines. The other one is drawn as expected with green lines. What am I doing wrong?
EDIT: Here is the structure of the X:
> str(X)
'data.frame': 5274 obs. of 3 variables:
$ Time : Factor w/ 2654 levels "2011.08.24 14:39:29",..: 1 1 2 2 3 3 4 4 5 5 ...
$ Server : Factor w/ 2 levels "PortalServer1",..: 1 2 1 2 1 2 1 2 1 2 ...
$ HeapFreePercent: int 42 49 41 49 41 49 41 49 41 49 ...
@GavinSimpson already commented on how to fix your problem. This should have been a comment, but is too long. I simply explain what happened to your plot with your data in its original form.
You are plotting data of type factor
. Consequently when you call the function plot
, the S3 method dispatching will invoke plot.factor
.
If you read the help for ?plot.factor
you will notice that the type of plot you then get also depends on the type of your second parameter. Since this is also a factor, ultimately your plot is drawn by spineplot
. Thus your type="l"
is essentially ignored. The colour is plotted in red, though...
Reverse engineering your data, I get something like this:
X <- data.frame(
Time = sort(sample(letters, 100, replace=TRUE)),
Server = sample(c("PortalServer1", "PortalServer2"), 100, replace=TRUE),
HeapFreePercent = runif(100))
str(X)
P1 <- subset(X, Server=="PortalServer1")
P2 <- subset(X, Server=="PortalServer2")
plot(P1$Time, P1$HeapFreePercent, type="l", col="red")
lines(P2$Time, P2$HeapFreePercent, col="green")
A somewhat hacky solution but it works for all factors, not just timestamps.
Change
plot(P1$Time, P1$HeapFreePercent, type="l", col="red")
lines(P2$Time, P2$HeapFreePercent, col="green")
to
plot(P1$Time, P1$HeapFreePercent, type="n")
lines(P1$Time, P1$HeapFreePercent, type="l", col="red")
lines(P2$Time, P2$HeapFreePercent, col="green")
This code first draws no actually data, just axis. Then it draws the real data using lines
, which avoids calling plot.factor
精彩评论