将直方图与R中的散点图结合 [英] combine histogram with scatter plot in R

查看:172
本文介绍了将直方图与R中的散点图结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用辅助轴在一个绘图中生成一个带有直方图和散点图的绘图.详细地,这是一个示例数据:

I am trying to produce a plot with histogram and scatter plot in just one plot using a secondary axis. In detail, here is an example data:

#generate example data

set.seed(1)
a <- rnorm(200,mean=500,sd=35)
data <- data.frame(a = a,
                   b = rnorm(200, mean=10, sd=2),
                   c = c(rep(1,100), rep(0,100)))

# produce a histogram of data$a
hist(a, prob=TRUE, col="grey")

#add a density line
lines(density(a), col="blue", lwd=2)

#scatter plot 
plot(data$a,data$b,col=ifelse(data$c==1,"red","black"))

我要做的是将直方图和散点图结合在一起.这意味着我的x轴为data$a,我的主要y轴为直方图的frequency/density,我的次要y轴为data$b.

What I want to do is to combine the histogram and scatter plot together. This implies my x-axis will be data$a, my primary y-axis is the frequency/density for the histogram and my secondary y-axis is data$b.

推荐答案

也许是这样的...

# produce a histogram of data$a
hist(a, prob=TRUE, col="grey")

#add a density line
lines(density(a), col="blue", lwd=2)

par(new = TRUE)

#scatter plot 
plot(data$a,data$b,col=ifelse(data$c==1,"red","black"),
     axes = FALSE, ylab = "", xlab = "")
axis(side = 4, at = seq(4, 14, by = 2))

这篇关于将直方图与R中的散点图结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆