[R] Legend problem in line charts
Petr PIKAL
petr.pikal at precheza.cz
Thu Jan 19 16:18:48 CET 2012
Hi
> Hi all,
>
> Small problem in generating the line charts.
>
> Question: Legend for the first graph is coming wrong., for second graph
> correctly. Please fix the legend postion at the down of graph.
> Plesae give me the solution.
>
> Thank you
> Devarayalu
>
>
>
> Orange1 <- structure(list(REFID = c(7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8,
> 8, 8, 8, 8, 9, 9, 9, 9), ARM = c(1, 1, 1, 1, 2, 2, 2, 2, 1, 1,
> 1, 1, 2, 2, 2, 2, 1, 1, 2, 2), SUBARM = c(0, 0, 0, 0, 0, 0, 0,
> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), ACTTRT = structure(c(3L,
> 3L, 3L, 3L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L,
> 1L, 2L, 2L), .Label = c("ABC", "DEF", "LCD", "Vehicle"), class =
"factor"),
> TIME1 = c(0, 2, 6, 12, 0, 2, 6, 12, 0, 2, 6, 12, 0, 2, 6,
> 12, 0, 12, 0, 12), ENDPOINT = structure(c(1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L), .Label = "PGA", class = "factor"), BASCHGA = c(0, -39,
> -47, -31, 0, -34, -25, -12, 0, -45, -47, -20, 0, -25, -30,
> -35, 0, -30, 0, -40), STATANAL = structure(c(1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L), .Label = "UNK", class = "factor"), Art_Name =
structure(c(1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> 2L, 2L, 2L, 2L), .Label = c("Bela_2010_206878",
"Dansinger_2010_20687812"
> ), class = "factor")), .Names = c("REFID", "ARM", "SUBARM",
> "ACTTRT", "TIME1", "ENDPOINT", "BASCHGA", "STATANAL", "Art_Name"
> ), row.names = c(NA, 20L), class = "data.frame")
>
Good that you provided data and code.
However there are some unnecessary items in your code.
AFAIU you want to plot three graphs from your data, one for each REFID
with distinct lines for each ACTTRT. If you do not want to use lattice or
ggplot you does not need printing your graphs inside a cycle.
Also be aware that all levels of a factor are preserved in a subset unless
you specifically strip the unused levels. Therefore there is a mismatch in
legend colours and line colours.
And you can also use multipage PDF which is quite convenient.
So I would recommend slight modifications to your code.
#open one pdf
pdf ("pga.pdf")
# set colours outside of cycle
nlev <- max(as.numeric(Orange1$ACTTRT))
colors <- rainbow(nlev)
linetype <- 1:nlev
plotchar <- seq(18,18+nlev,1)
#plot three graphs for each refid
for (i in 1:length(refid)) {
refid1 <- subset(Orange1, REFID == refid[i])
xrange <- range(refid1$TIME1)
yrange <- range(refid1$BASCHGA)
plot(xrange, yrange, type="n", xlab="TIME1 (WK)", ylab="BASCHGA (mm)" )
# change ACTTRT to numeric
actnum <- as.numeric(refid1$ACTTRT)
# add lines and select only appropriate colours for each line
for (j in unique(actnum)) {
ACTTRT <- subset(refid1, actnum==j)
lines(ACTTRT$TIME1, ACTTRT$BASCHGA, type="b", lwd=1.5,lty=linetype[j],
col=colors[j], pch=plotchar[j])
paste("REFID = ", unique(refid1$REFID), "; STATANAL = ",
unique(refid1$STATANAL), sep="") -> x
title(x)
}
#add legend and again select appropriate colours
legend(xrange[1], yrange[2], unique(refid1$ACTTRT), cex=0.8,
col=colors[unique(actnum)],
pch=plotchar[unique(actnum)], lty=linetype[unique(actnum)])
#"bottomright", "bottom", "bottomleft", "left", "topleft", "top",
"topright", "right" and "center"
}
dev.off()
And one comment. Colors is a function in base R (grDevices) so it is
recommendable to use different name for colours vector.
See ?colors
Regards
Petr
>
> unique(Orange1$REFID) -> refid
> # Create Line Chart
> for (i in 1:length(refid)) {
> # convert factor to numeric for convenience
> refid1 <- subset(Orange1, REFID == refid[i])
> refid1$ACTTRTnum <- as.numeric(refid1$ACTTRT)
> nACTTRTs <- max(refid1$ACTTRTnum)
>
> # get the range for the x and y axis
> xrange <- range(refid1$TIME1)
> yrange <- range(refid1$BASCHGA)
>
> # set up the plot
> pdf (paste("pga", i, ".pdf", sep=''))
> print(plot(xrange, yrange, type="n", xlab="TIME1 (WK)",
> ylab="BASCHGA (mm)" ))
> colors <- rainbow(nACTTRTs)
> linetype <- c(1:nACTTRTs)
> plotchar <- seq(18,18+nACTTRTs,1)
>
> # add lines
>
> for (i in 1:nACTTRTs) {
> ACTTRT <- subset(refid1, ACTTRTnum==i)
> print(lines(ACTTRT$TIME1, ACTTRT$BASCHGA, type="b", lwd=1.5,
> lty=linetype[i], col=colors[i], pch=plotchar[i]))
> }
>
> # add a title and subtitle
> paste("REFID = ", unique(refid1$REFID), "; STATANAL = ", unique(refid1
> $STATANAL), sep="") -> x
> title(x)
>
>
> # add a legend
> legend(xrange[1], yrange[2], unique(refid1$ACTTRT), cex=0.8, col=colors,
> pch=plotchar, lty=linetype)
> #"bottomright", "bottom", "bottomleft", "left", "topleft", "top",
> "topright", "right" and "center"
> dev.off()
> }
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list