Getting R plot type "b" with text instead of points

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())

There's an experimental grob in gridExtra to implement this in Grid graphics,

 library(gridExtra)
 grid.newpage() ; grid.barbed(pch=5)

enter image description here

Tags:

R

Ggplot2