组合直方图和折线图

时间:2018-12-30 17:46:17

标签: r line histogram

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

Histogram

Line graph

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

1 个答案:

答案 0 :(得分:0)

这是具有基本情节的解决方案:

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中,如果需要执行此操作以绘制图形,则可以缩放轴