组合直方图和折线图

Combining histogram and a line graph

我创建了直方图和折线图,其 y 轴值如图所示不同。

我想将两个图表放在一个图中。

这是一个基本情节的解决方案:

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

或 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

在 ggplot 中,如果你需要这样做来制作图表,你可以缩放轴