6

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))
tjebo
  • 21,977
  • 7
  • 58
  • 94
teucer
  • 6,060
  • 2
  • 26
  • 36

4 Answers4

5

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
  • 118,240
  • 47
  • 247
  • 360
  • @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 Sep 28 '10 at 12:30
  • @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 Sep 28 '10 at 14:27
  • 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 Sep 28 '10 at 15:41
  • Maybe try geom_segment instead? – Brandon Bertelsen Sep 28 '10 at 17:07
  • 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 Sep 28 '10 at 19:05
  • @Richie. Brilliant hack for the plot itself, but do you have any idea how this can be transferred to the legend as well? The white circle does not show in the legend-hence the shape is covered by the line. I second teucer´s request for a geom that would allow type b plots in ggplot2 as well. – Misha Feb 08 '14 at 11:38
  • OK-turns out like hackathon. Using a size aesthetic with similar labels and forcing size with the scale_size_manual to be the same worked out quite allright. Especially when also increasing the legend.key.length in theme. – Misha Feb 08 '14 at 13:46
3

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

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • @baptiste let's assume I have a plot 'p' already, how can I "add" it to the plot (p+geom_barbed())? I have seen that there is no such function (geom_barbed) yet... – teucer Feb 10 '11 at 09:44
  • 1
    for some reason I haven't included this line in the package, `geom_barbed <- GeomBarbed$build_accessor()` which explains why `geom_barbed` isn't recognised (why `qplot` worked is a mystery to me) – baptiste Feb 10 '11 at 16:31
  • Sure; however note that the whole package should be revised as it consists of experimental bits-and-pieces that I have not updated regularly. Further, ggplot2 is currently undergoing a complete rewrite, which will make such code redundant. – baptiste Feb 11 '11 at 06:03
  • @baptiste can you give any link about the rewrite of ggplot2? – teucer Feb 11 '11 at 10:08
  • it's split in different packages, see Hadley's github repository – baptiste Feb 12 '11 at 12:40
  • @baptiste: I find no `GeomBarbed` in ggExtra. Has the package changed? – JohnRos May 17 '15 at 20:15
  • 1
    @JohnRos ggExtra was an experimental package which I retired long ago; it turns out there is a newer, unrelated project with the same name. I'll edit this answer since it's no longer applicable. – baptiste May 17 '15 at 20:56
1

This is now easy with ggh4x::geom_pointpath. Set shape = NA and add a geom_text layer.

library(ggh4x)
#> Loading required package: ggplot2

df <- data.frame(x = rep(1:5, each = 5), 
                 y = c(outer(seq(0, .8, .2), seq(0.02, 0.1, 0.02), `+`)),
                 cat = rep(paste0("a", 1:5)))

ggplot(df, aes(x, y)) +
  geom_text(aes(label = cat)) +
  geom_pointpath(aes(group = cat, shape = NA))

Created on 2021-11-13 by the reprex package (v2.0.1)

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Hello Tjebo, is there a way on the y-axis to remove the ticks and place the grouping values? – GiacomoDB Dec 31 '21 at 10:15
  • 1
    @GiacomoDB I think this one should help you - https://stackoverflow.com/questions/29357612/plot-labels-at-ends-of-lines (just adjust the code to make it the beginning ) – tjebo Dec 31 '21 at 11:33
  • Thank you, I fixed also adding a line on the template I found:`df <- df[complete.cases(df), ] `; just before the graph. I realized it was generating many lines with `NA` values. – GiacomoDB Jan 02 '22 at 10:13
0

Another way to make great slope graphs is using the package CGPfunctions.

library(CGPfunctions)
newggslopegraph(newcancer, Year, Survival, Type)

You have also many options to choose. You can find a good tutorial here:

https://www.r-bloggers.com/2018/06/creating-slopegraphs-with-r/

GiacomoDB
  • 369
  • 1
  • 10