0

I have created histogram and a line graph whose y-axis values are different as shown.

Histogram

Line graph

I want to have the two graphs in one plot.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Jackline
  • 59
  • 1
  • 9
  • 3
    Could you please include in your question the code that you used to generate your two current plots? – G5W Dec 30 '18 at 17:47
  • You are asking just for two plots on one page? One of which is a `ggplot2` product and the other is a base `plot` result? I'm pretty sure this has been asked and answered. So you should first search (possibly adding the search term `gridBase`) and if you find the existing answers cannot deliver the desired result then p[edit] your question to reflect this and stated how they fail to deliver the necessary results. – IRTFM Dec 30 '18 at 18:34
  • If there's no response within a suitable period of time, say 2 hours, to the two comments, then I suggest closing as a highly probably duplicate of https://stackoverflow.com/questions/14124373/combine-base-and-ggplot-graphics-in-r-figure-window – IRTFM Dec 30 '18 at 18:41
  • 1
    Possible duplicate of [Combine base and ggplot graphics in R figure window](https://stackoverflow.com/questions/14124373/combine-base-and-ggplot-graphics-in-r-figure-window) – phiver Dec 31 '18 at 10:56

1 Answers1

0

This is a solution with basic plot:

set.seed(16011991)
x <- rnorm(100)
dots <- data.frame(c(-3,-2,-1,0,1,2,3),c(10,20,30,40,30,20,10))
hist(x)
lines(dots, col = "blue", lwd = 2)

plot

Or ggplot:

set.seed(16011991)
x <- data.frame(norm = rnorm(100))
dots <- data.frame(x=seq(-3,3,0.05), y=log(1:length(seq(-3,3,0.05))))

ggplot(data = x, aes(x=norm, color='red')) +
  geom_histogram(bins = 30, fill="white", show.legend = FALSE, size=1.1) +
  geom_line(data = dots, aes(x=x,y=y), color= 'blue', size=1.1) +
  labs(title= 'My histogram', x = 'N(0,1)', y='Count') +
  theme_stata()

ggplot

in ggplot you can scale the axes if you need to do this to make your graph

K. Peltzer
  • 326
  • 3
  • 7