Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot function does not take plot type into account in R language

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 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 ...
like image 557
Andrey Adamovich Avatar asked Aug 25 '11 09:08

Andrey Adamovich


People also ask

What is type parameter in plot () function in R?

Plot. The plot() function is used to draw points (markers) in a diagram. The function takes parameters for specifying points in the diagram. Parameter 1 specifies points on the x-axis. Parameter 2 specifies points on the y-axis.

How does the plot function work in R?

The plot() function in R isn't a single defined function but a placeholder for a family of related functions. The exact function being called will depend upon the parameters used. At its simplest, plot() function simply plots two vectors against each other. This gives a simple plot for y = x^2.


1 Answers

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

enter image description here

like image 117
Andrie Avatar answered Oct 06 '22 18:10

Andrie