2

Example data set:

n   value
100000  20
200000  30
300000  25
400000  40
500000  12

The following code:

require(ggplot2)
data <- read.table("test", sep = "\t", header = TRUE,)
ggplot(data, aes(n, value)) + 
geom_point(aes(n,value)) + geom_line(aes(n,value))
dev.off()

will produce the following output:

enter image description here

However, I would like to get the following output:

enter image description here

jsguy
  • 2,069
  • 1
  • 25
  • 36

3 Answers3

4

As suggested by @Roland, you can add text to your plot using geom_text or geom_label or annotate. Here is the code using geom_text:

data <- structure(list(n = c(100000L, 200000L, 300000L, 400000L, 500000L
), value = c(20L, 30L, 25L, 40L, 12L)), .Names = c("n", "value"
), class = "data.frame", row.names = c(NA, -5L))

library(ggplot2)
ggplot(data, aes(x=n, y=value)) + 
geom_point(aes(n,value)) + geom_line(aes(n,value))+
geom_text(aes(label=value), hjust=c(-1,0,0,-1,2), vjust=c(1,-.5,1.5,0,0))

enter image description here

hjust controls horizontal justification and vjust controls vertical justification. Details are given at this link. Using geom_label

ggplot(data, aes(x=n, y=value)) + 
geom_point(aes(n,value)) + geom_line(aes(n,value))+
geom_label(aes(label=value), hjust=c(-1,0,0,-1,2), vjust=c(1,-.5,1.5,0,0))

the result is:

enter image description here

It is also possible to use annotate as follows:

ggplot(data, aes(x=n, y=value)) + 
geom_point(aes(n,value)) + geom_line(aes(n,value))+
annotate("text", x=data$n, y=data$value, label=data$value, 
   hjust=c(-1,0,0,-1,2), vjust=c(1,-.5,1.5,0,0))

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
3

Another option is library(ggrepel) to minimize the point/label overlap automatically. I think it's more reliable than position_jitter() and useful if you can't/don't want to hardcode all of your dodge values:

ggplot(data, aes(n, value)) + 
    geom_point() +
    geom_line() +
    # geom_text(aes(label = value), position = "jitter")
    geom_text_repel(aes(label = value),
                    point.padding = unit(.25, "lines"))

geom_label_repel() is available as well and there a lot of options for setting the "dodge" areas size.

enter image description here

Nate
  • 10,361
  • 3
  • 33
  • 40
1

If you specify the data argument in the call of ggplot the following calls of geom_point etc. will inherit it, so you don't need to specify it again. Using dplyr piping notation (%>%) and geom_text the result would be:

library(dplyr)
library(ggplot2)

tribble(
   ~n, ~value,
   100000,  20,
   200000,  30,
   300000,  25,
   400000,  40,
   500000,  12
) %>% ggplot(aes(n, value)) + 
                geom_point() + 
                geom_line() + 
                geom_text(aes(label=value), nudge_y=1, nudge_x=-10000)

This is essentially the same as calling

ggplot(data, aes(n, value)) + 
       geom_point() + 
       geom_line() + 
       geom_text(aes(label=value), nudge_y=1, nudge_x=-10000)

enter image description here

shosaco
  • 5,915
  • 1
  • 30
  • 48