Reputation: 902
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.
Upvotes: 3
Views: 2958
Reputation: 42564
As of version 2.2.0, ggplot2
allows to plot subtitles and captions which can be utilized for this purpose.
# 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)")
# plot caption (bottom right)
p + labs(caption = "(a)")
Upvotes: 3
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)
Upvotes: 6
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:
Upvotes: 2