Yang Yang
Yang Yang

Reputation: 902

How to number a figure using ggplot2

For the general plot in R, legend is used to number a figure.

set.seed(100)
Mydata=rnorm(65)
Year=1950:2014
plot(x=Year,y=Mydata,type = "l")
legend("topleft","(a)",bty = "n")

I wonder how we can do the same thing using ggplot2. Thanks. enter image description here

Upvotes: 3

Views: 2958

Answers (3)

Uwe
Uwe

Reputation: 42564

As of version 2.2.0, ggplot2 allows to plot subtitles and captions which can be utilized for this purpose.

subtitle (top left)

# create data frame as required by ggplot2
mydf <- data.frame(Year, Mydata)

library(ggplot2)
p <- ggplot(mydf, aes(Year, Mydata)) + 
  geom_line()

# plot subtitle (top left)
p + labs(subtitle = "(a)")

enter image description here

caption (bottom right)

# plot caption (bottom right)
p + labs(caption = "(a)")

enter image description here

Upvotes: 3

G. Grothendieck
G. Grothendieck

Reputation: 269905

Using grid it can be done independently of the data:

library(ggplot2)
qplot(Year, Mydata, geom = "line")

library(grid)
grid.text("(a)", 0.15, 0.85)

screenshot

Upvotes: 6

LyzandeR
LyzandeR

Reputation: 37889

A way with annotate:

library(ggplot2)
set.seed(100)
Mydata=rnorm(65)
Year=1950:2014
data <- data.frame(Mydata = Mydata, Year = Year)

#plot
ggplot(data, aes(Year, Mydata)) + 
  geom_line() + 
  annotate('text', x = 1960, y = 2, label = '(a)')

Output:

enter image description here

Upvotes: 2

Related Questions