Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

r - Remove Default Background Color and Make Title in Plot Zone

I have this ggplot2 plot which I want to modify to fit the following conditions:

  1. Remove default background that is hash colour to be plain.
  2. Make (a) to be the plot title located within the plot area that is not close to the line (automatically).
  3. Make $phi = .8$ to be automatically at the head of the line (still within the plot area).
  4. And sd = 1 to be automatically at the tail of the line.

EDIT

  1. The four(4) Border lines to be present.
  2. Gridlines to be grey colour.

.

## simulate ARIMA(1, 0, 0)
set.seed(799837)
ts <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 10)

gplot <- ggplot(NULL, aes(y = ts, x = seq_along(ts))) + scale_x_continuous(breaks = seq(1,10,2)) +
  geom_line(color = "#F2AA4CFF") + geom_point(color = "#101820FF") + xlab('Time') + ylab('Series') + ggtitle(expression("(a)
sd = 1
"~phi~"=.8")) + 
  theme(axis.text = element_text(size = 10, angle = 0, vjust = 0.0, hjust = 0.0), axis.title = element_text(size = 10), axis.title.x = element_text(angle = 0, hjust = 0.5, vjust = 0.5, size = 10), axis.title.y = element_text(angle = 90, hjust = 0.5, vjust = 0.5, size = 14), plot.title = element_text(size = 14, margin = margin(t = 25, b = -20, l = 0, r = 0))) + 
scale_y_continuous(expand = c(0.0, 0.00))

gplot

ggplot here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is an example how of this might look semi automatic. The specifics depend on how different your actual time series data is, but this might help as inspiration. Exact positions and text styling can of course be tweaked to your liking.

set.seed(799837)
ts <- arima.sim(n = 10, model = list(ar = 0.95, order = c(1, 0, 0)), sd = 10)    

gplot <- ggplot(NULL, aes(y = ts, x = seq_along(ts))) + 
      geom_line(color = "#F2AA4CFF") + 
      geom_point(color = "#101820FF") + 
      annotate("text", x = mean(seq_along(ts)), y = max(ts) * 1.1, label = "(a)")+
      annotate("text", x = min(seq_along(ts)), y = max(ts) * 1.1, label = 'paste(~phi~"=.8")', parse = TRUE )+ 
      annotate("text", x= max(seq_along(ts)), y = ts[[max(seq_along(ts))]] * 1.1, label = "sd=1") +
      xlab('Time') + 
      ylab('Series') + 
      theme(axis.text = element_text(size = 10, angle = 0, vjust = 0.0, hjust = 0.0), 
            axis.title = element_text(size = 10), 
            axis.title.x = element_text(angle = 0, hjust = 0.5, vjust = 0.5, size = 10), 
            axis.title.y = element_text(angle = 90, hjust = 0.5, vjust = 0.5, size = 14), 
            plot.title = element_text(size = 14, margin = margin(t = 25, b = -20, l = 0, r = 0)),
            panel.background = element_blank()) + 
      scale_x_continuous(breaks = seq(1,10,2)) +
      scale_y_continuous(expand = c(0.0, 0.00))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...