Re: [R] Plotting series marked with a symbol on every nth data point, preferably in ggplot...

From: Tribo Laboy <tribolaboy_at_gmail.com>
Date: Mon, 25 Feb 2008 16:45:58 +0900

Hello Gabor and Charilaos

Thanks for the help. I did not realize that the legend argument can take both the symbol and the line. Following your suggestions I have come with the code that follows.

I realized that I can use the 'legend' function to draw over ggplot2 produced plot, so that is a good start, but I am not quite sure how to prepare the data and plot it in ggplot. If I use print with option pretty = FALSE on the object produced by ggplot, it displays the graph without legend, so I can overlay the legend from traditional graphics. This option (pretty = FALSE), however, also removes the X-label and Y-label from the plot.

Thanks again.

Maybe someone can help with the ggplot version of this thing?

Regards,

TL.

#-----------------------START CODE SNIPPET-----------------------------------

#-------------------------------------------------------------
# Try traditional graphics first
#-------------------------------------------------------------
# 1 . Generate signal-like data
#

x1 <- c(26:300)
y1<- sin(0.1*x1) + rnorm(275,mean = 0,sd = 0.1);

x2 <- c(1:350)
y2<-sin(0.1*x2+10) + rnorm(350,mean = 0,sd = 0.1);
x3 <- c(41:280)
y3<-sin(0.1*x3+30) + rnorm(240,mean = 0,sd = 0.1);




# 2 . Plot the three signals (and symbols)
#
# How many points to skip:

N = 15;

# Draw empty axes with limits and labels
plot(NA, NA,type="n", xlim = c(0,400),ylim = c(-1.5,1.5),xlab ="Time",ylab ="Amplitude" )

# Draw the lines

pch1 <- c(15,rep(NA,N));
pch2 <- c(16,rep(NA,N));
pch3 <- c(17,rep(NA,N));
lines(x1, y1, type = "o", col = "red", pch=pch1, cex=1.5, lwd=1.5, lty=1);
lines(x2, y2, type = "o", col = "green", pch = pch2, cex=1.5, lwd=1.5, lty=2) lines(x3, y3, type = "o", col = "blue", pch = pch3, cex=1.5, lwd=1.5, lty=3)

#
# 4 . Add the legend
#

legend("topright",col=c("red","green","blue"), pch=c(15,16,17), lty=c(1,2,3), legend=c("series 1", "series 2", "series 3"), inset=0.05, bg='white')

#-------------------------------------------------------------
# Try the same thing with GGPLOT
#-------------------------------------------------------------

dataset <- rbind(data.frame(Test = "Test 1", x = x1, y = y1),
                 data.frame(Test = "Test 2", x = x2, y = y2),
                 data.frame(Test = "Test 3", x = x3, y = y3));



# 2 . Plot the three signals (and every point as a symbol)
#

ggp <- ggplot(data = dataset, aes(x = x, y = y, colour = Test),) + geom_line() + geom_point()
print(ggp,pretty = FALSE) #Do not plot the legend, but this also removes the x and y labels

#
# 4 . Add the legend (Traditional graphics) over the GGPLOT
#

legend("topright",col=c("red","green","blue"), pch=c(15,16,17), lty=c(1,2,3), legend=c("Test 1", "Test 2", "Test 3"), inset=0.05, bg='white')

#--------------------------END CODE SNIPPET-------------------------------
On Mon, Feb 25, 2008 at 2:05 PM, Gabor Grothendieck <ggrothendieck_at_gmail.com> wrote:

> Try this:
>
>  pch <- c("+", ""); lwd <- 1
>  plot(1:10, type = "o", pch = pch, lwd = lwd)
>  legend("topleft", legend = "data", pch = pch, lwd = lwd)
>
>
>
>
>  On Sun, Feb 24, 2008 at 11:01 PM, Tribo Laboy <tribolaboy_at_gmail.com> wrote:
>  > Hello!
>  >
>  > I am working with signals and a plot of several signals on the same
>  > axes can get quite messy. With lines that are very fractured,
>  > distinction by only the linestyle is not very clear. If I add symbols
>  > to the plot however, there are so many symbols, that they overplot and
>  > the whole plot is unreadable once again. I am looking for advice on
>  > how to make a plot with continuous lines and symbols appearing at
>  > every  nth point. An example of this problem and a solution in SAS can
>  > be found here:
>  > www2.sas.com/proceedings/sugi26/p072-26.pdf
>  >
>  > The obvious solution would be to extract the n-th point from the
>  > dataset and overplot as a new line with symbols, but this does not
>  > change the legend, does it? How can I then have a line+symbol in the
>  > legend represent my curve?
>  >
>  > Now, ideally I would prefer a solution in ggplot, because of the ease
>  > of plotting and changing the plot labels and other properties.
>  > However, I  remember, Hadley mentioned that he is still working on a
>  > new version to have combined line+symbol plots, and probably he hasn't
>  > finished that yet. So any advice is welcome.
>  >
>  > Regards,
>  >
>  > TL
>  >
>  > PS. I did RTFM and I am not posting code, because at this time I
>  > expect a general outline how to do it and what commands to lookup, not
>  > code.
>  >
>  > ______________________________________________
>  > 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.
>  >
>

______________________________________________
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. Received on Mon 25 Feb 2008 - 07:49:06 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 26 Feb 2008 - 02:30:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive