tags:

views:

87

answers:

1

Hi,

is there a way in ggplot2 to get the plot type "b"? See example:

x <- c(1:5)
y <- x 
plot(x,y,type="b")

Ideally, I want to replace the points by their values to have something similar to this famous example:

alt text

EDIT: Here some sample data (I want to plot each "cat" in a facet with plot type "b"):

`df <- data.frame(x=rep(1:5,9),y=c(0.02,0.04,0.07,0.09,0.11,0.13,0.16,0.18,0.2,0.22,0.24,0.27,0.29,0.31,0.33,0.36,0.38,0.4,0.42,0.44,0.47,0.49,0.51,0.53,0.56,0.58,0.6,0.62,0.64,0.67,0.69,0.71,0.73,0.76,0.78,0.8,0.82,0.84,0.87,0.89,0.91,0.93,0.96,0.98,1),cat=rep(paste("a",1:9,sep=""),each=5))`

Regards, Musa

+3  A: 

Set up the axes by drawing the plot without any content.

plot(x, y, type = "n")

Then use text to make your data points.

text(x, y, labels = y)

You can add line segments with lines.

lines(x, y, col = "grey80")

EDIT: Totally failed to clock the mention of ggplot in the question. Try this.

dfr <- data.frame(x = 1:5, y = 1:5)
p <- ggplot(dfr, aes(x, y)) + 
  geom_text(aes(x, y, label = y)) + 
  geom_line(col = "grey80")
p

ANOTHER EDIT: Given your new dataset and request, this is what you need.

ggplot(df, aes(x, y)) + geom_point() + geom_line() + facet_wrap(~cat)

YET ANOTHER EDIT: We're starting to approach a real question. As in 'how do you make the lines not quite reach the points'.

The short answer is that that isn't a standard way to do this in ggplot2. The proper way to do this would be to use geom_segment and interpolate between your data points. This is quite a lot of effort however, so I suggest an easier fudge: draw big white circles around your points. The downside to this is that it makes the gridlines look silly, so you'll have to get rid of those.

ggplot(df, aes(x, y)) + 
   facet_wrap(~cat) +
   geom_line() + 
   geom_point(size = 5, colour = "white") + 
   geom_point() + 
   opts(panel.background = theme_blank())
Richie Cotton
the ggplot version is not exactly what I wanted...
teucer
@Musa: The question is ambiguous. You ask for `type="b"` which is points and lines, but the picture is text and lines, which is what I gave you. Ask a better question and you'll get a better answer.
Richie Cotton
@Richie: first of all thx for your effort. The question is about having the type "b" in ggplot, at the end it does not matter if it is points and lines or texts and lines. I guess if I have the type, I could replace points with texts quite easily (geom_point->geom_text)
teucer
the last edit corresponds to the plot type "o" and not "b", please have look at http://www.statmethods.net/graphs/images/lines0.png
teucer
Maybe try geom_segment instead?
Brandon Bertelsen
The effort to points ratio of this question is very poor.
Richie Cotton
Thx Richie. The trick white the white points is brilliant! I knew that I could do it with geom_segment, but as you pointed it out, it is a lot of effort (you need to compute xend and yend manually!). However, I believe that it would be nice to have this in standard in ggplot2 (maybe as a parameter in geom_path?).
teucer