重叠散点图,散点图和密度线

时间:2019-06-12 16:08:25

标签: r ggplot2 scatter kernel-density

是否可能有重叠的密度图和穿过散点图的线?

背景:我有一些数据可以用来建立回归模型。我获取了数据中变量之一的唯一值,然后看到了预测在这些唯一值之间的变化,并绘制了这些预测。我希望原始数据的密度,即唯一值的散点图的密度/直方图位于我的预测线的背景中。

要说明:

library(dplyr)
iris2 = iris %>% mutate(pw_int=round(Petal.Width))
iris2_summary = iris2 %>% group_by(pw_int) %>% summarize(mean=mean(Sepal.Length))

所以我假装Petal.Width的每个取整值的Sepal.Length的平均值是我的模型。

library(ggpubr)

ggscatterhist(
  iris, x = "Petal.Width", y = "Sepal.Length",
  #color = "Species", # comment out this and last line to remove the split by species
  #margin.plot = "histogram", density plots
  margin.params = list(color = "red", size = 0.2)
)

enter image description here

我只想知道x轴的直方图/密度,但是我不知道怎么做。

尝试

lines(iris2_summary$pw_int,iris2_summary$mean,col="green",type="l")

对我没有任何帮助。似乎下面的方法可能是一种方法,但看起来确实很糟糕:

par(new=TRUE)

plot( iris2_summary$pw_int, iris2_summary$mean, type="l", col="green" )

还有一个我不知道如何在后台获取密度的问题。

enter image description here

所需的输出

enter image description here

0 个答案:

没有答案
相关问题