0
> aggregate(dat[, 3:7], by=list(dat$TRT), FUN=mean)
  Group.1   DBP1  DBP2   DBP3   DBP4   DBP5
1       A 116.55 113.5 110.70 106.25 101.35
2       B 116.75 115.2 114.05 112.45 111.95

I wish to create a lines plot were the x-axis are the names (DBP1, DBP2, ..., DBP5).

It takes two seconds in Excel (I admit) and gives exactly what I want: enter image description here

To be clear, the question is about getting the two rows of data into the plot, not about how they are displayed (i.e. with what line/point/color combination).

TMOTTM
  • 3,286
  • 6
  • 32
  • 63
  • Not exactly, examples all only include ONE row of data, which I specifically added in my question – TMOTTM Aug 17 '16 at 19:56
  • 1
    you can add another layer with command 'lines()' – Jan Sila Aug 17 '16 at 19:57
  • 1
    http://www.statmethods.net/graphs/index.html – Tim Aug 17 '16 at 20:00
  • why downvote, its obviously a struggle and far from obvious – TMOTTM Aug 17 '16 at 20:06
  • @TMOTTM the answer is quite obvious if you consult `?plot`, specifically the information under the argument `type` – Sycorax Aug 17 '16 at 20:36
  • @GeneralAbrial Type "b" is just the graphical option for lines and points. It does not help with providing categorical arguments, nor does it help with adding two rows of data as separate data entries to plot. To be clear, I removed that part of the question and made it explicit. Still downvote? – TMOTTM Aug 18 '16 at 03:36
  • Your edit happened after I left my comment, so I can't really be held accountable for reading what you hadn't yet written. I didn't downvote, although the tooltip for the downvote arrow states "this question does not show any research effort." Perhaps that's why? – Sycorax Aug 18 '16 at 03:40
  • near-duplicate: http://stackoverflow.com/questions/14860078/plot-multiple-lines-data-series-each-with-unique-color-in-r – Ben Bolker Aug 18 '16 at 20:37
  • @BenBolker except that they're using numerical data as the x-argument, here it's categorical – TMOTTM Aug 19 '16 at 04:24

5 Answers5

4

With dplyr, tidyr and ggplot2

Data

zz <- "Group.1   DBP1  DBP2   DBP3   DBP4   DBP5
A 116.55 113.5 110.70 106.25 101.35
B 116.75 115.2 114.05 112.45 111.95"

df <- read.table(text = zz, header = TRUE)

Load Required Packages

library(dplyr)
library(tidyr)
library(ggplot2)

Tidy

df_tidy <- df %>% 
    gather(key, value, -Group.1)

Plot

ggplot(data = df_tidy, aes(x = key, y = value)) +
    geom_line(aes(color = Group.1)) +
    ylim(90, 120)

Output

enter image description here

emehex
  • 9,874
  • 10
  • 54
  • 100
1

First step: use melt from the reshape2 package:

d <- aggregate(
               dat[, 3:7],
               by=list(dat$TRT),
               FUN=mean
     )
m <- melt(d
     id="TRT",
     measure.vars=c("DBP1","DBP2","DBP3","DBP4","DBP5")
)

Then use

xyplot(m$value~m$variable, type="o", group=m$TRT, auto.key=list(TRUE))

enter image description here

TMOTTM
  • 3,286
  • 6
  • 32
  • 63
1

Simplest possible (??) base-R answer:

dd <- read.table(header=TRUE,text="
       Group.1   DBP1  DBP2   DBP3   DBP4   DBP5
       A 116.55 113.5 110.70 106.25 101.35
       B 116.75 115.2 114.05 112.45 111.95")

matplot() is the basic function for plotting multiple parallel sequences, but (1) it requires that the series be in columns of a matrix; (2) it can't handle character variables, so you have to drop the first column; (3) if you want the group names as axis labels, you have to add that with a separate axis() command. Unfortunately it's not (that I know of) possible to suppress just one of the axes, so you have to suppress them both (axes=FALSE), then add them both manually.

par(las=1) ## horizontal y-axis labels (cosmetic)
matplot(t(dd[,-1]),type="b",axes=FALSE,
        ylab="",ylim=c(90,120),
        col=c("red","blue"),pch=16,lty=1)
axis(side=2)                            ## y-axis (default labels)
axis(side=1,at=1:5,label=names(dd)[-1]) ## x-axis
box()                                   ## bounding box
legend("bottomleft",legend=dd$Group.1,
       col=c("red","blue"),lty=1,pch=16)

enter image description here

If you want to dispense with legend, nice tick-marks, etc., then just matplot(t(dd[,-1]),...) will do it.

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Fine, the only thing I dont like about it is you're overwriting the axis indeces with the string indeces. – TMOTTM Aug 18 '16 at 20:17
  • Can you clarify please? I thought that's what you wanted. Or are you unhappy that it has to be done manually? – Ben Bolker Aug 18 '16 at 20:20
0

A simple R code can be:

A <- c(116.55, 113.5, 110.70, 106.25, 101.35)
B <- c(116.75, 115.2, 114.05, 112.45, 111.95)

plot(A, type="n")
axis(1, at=1:5, labels=c("DBP1","DBP2","DBP3","DBP4","DBP5"))
lines(A, col="blue")
lines(B, col="red")

Alternate way:

plot(A, type="l", col="blue")
axis(1, at=1:5, labels=c("DBP1","DBP2","DBP3","DBP4","DBP5"))
lines(B, col="red")
Dr Nisha Arora
  • 632
  • 1
  • 10
  • 23
-2

A simple approach is to custom your plot, step-by-step.

First, plot the first line and specify you don't when an x-axis to be drawn. Add the second line. Add your custom x-axis with the labels you want. Add points on the values you just plot.

Translate into R :

data <- matrix(c(116.55,113.5,110.7,106.25,101.35,116.75,115.2,114.05,112.45,111.95), nrow=2)

plot(data[1,], type="l", xaxt="n")
axis(1, at=1:5, labels=c("DBP1","DBP2","DBP3","DBP4","DBP5"))
lines(data[2,])
points(data[1,])
points(data[2,])

the xaxt="n" specify that you want no x-axis text.

Here is a good reference : http://www.statmethods.net/advgraphs/axes.html

enter image description here

Then, make it beautiful!

If you want a simpler approach for the future, here is a basic function you can improve

plot.Custom <- function(yy, xLabels, ...){
    plot(yy[1,], type="l", xaxt="n", ...)
    axis(1, at=1:dim(yy)[2], labels=xLabels)
    for(i in 1:dim(yy)[1]){
        lines(yy[i,])
        points(yy[i,])
    }
}
 plot.Custom(data, c("DBP1","DBP2","DBP3","DBP4","DBP5"))
  • I was honestly little struggling with using factor(x) on x-axis which didnt allow me do proper lines. You solution is great! – Jan Sila Aug 17 '16 at 19:59
  • I can't believe there is not an easier way to do this since it's such a common task. – TMOTTM Aug 17 '16 at 20:00
  • 1
    @TMOTTM there is. in the call to plot, set `type='b'`. or just read the documentation. – Sycorax Aug 17 '16 at 20:06
  • 1
    @GeneralAbrial +1 for `type="b"`. I've never used it. With it in head, the `matplot` function could be used : `matplot(t(data), type="b", pch="o", xaxt="n")` –  Aug 17 '16 at 20:20