The plot function does not take into account the type of plot in the language R

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="green") q() 

In the resulting PNG image data for "PortalServer1", dots and black are drawn, but they must be drawn with red lines. The other is expected with green lines. What am I doing wrong?

EDIT . Here is the structure of 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 ... 
+8
r plot visualization
source share
2 answers

@GavinSimpson has already commented on how to fix your problem. It should have been a comment, but it's too long. I am simply explaining what happened to your plot with your data in its original form.

You draw data of type factor . Therefore, when you call the plot function, the S3 method dispatcher will call plot.factor .

If you read the help for ?plot.factor , you will notice that the type of graph that you then receive 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 color is red, although ...

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") 

enter image description here

+8
source share

A somewhat hacky solution, but it works for all factors, not just timestamps.

Edit

 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 does not initially display the actual data, but only the axis. Then it draws real data using lines , which avoids calling plot.factor

0
source share

All Articles