在R中具有alpha透明直方图的散点图

在R中具有alpha透明直方图的散点图,r,plot,ggplot2,R,Plot,Ggplot2,如何在R中绘制具有alpha透明、无标度直方图的散点图,如下图所示 看起来它不是ggplot2制造的 有人知道使用的是什么命令吗?我不知道是否有一个包可以直接这样做,但我确信这可以在R中完成。透明度很简单:为给定的透明度向颜色的RGB规范中添加另外两个数字: #FF0000 # red #FF0000FF # full opacity #FF000000 # full transparency 使用布局功能也可以轻松组合不同的绘图。对于垂直密度图,它与x和y切换的水平图相同。给出的示例可以很容

如何在R中绘制具有alpha透明、无标度直方图的散点图,如下图所示

看起来它不是ggplot2制造的


有人知道使用的是什么命令吗?

我不知道是否有一个包可以直接这样做,但我确信这可以在R中完成。透明度很简单:为给定的透明度向颜色的RGB规范中添加另外两个数字:

#FF0000 # red
#FF0000FF # full opacity
#FF000000 # full transparency
使用
布局
功能也可以轻松组合不同的绘图。对于垂直密度图,它与x和y切换的水平图相同。给出的示例可以很容易地扩展到包括颜色、较小的边距等。如果这个描述不够充分,我可以尝试提出一个更详细的示例。

库(ggplot2)
library(ggplot2)
library(gridExtra)

set.seed(42)
DF <- data.frame(x=rnorm(100,mean=c(1,5)),y=rlnorm(100,meanlog=c(8,6)),group=1:2)

p1 <- ggplot(DF,aes(x=x,y=y,colour=factor(group))) + geom_point() +
  scale_x_continuous(expand=c(0.02,0)) +
  scale_y_continuous(expand=c(0.02,0)) +
  theme_bw() +
  theme(legend.position="none",plot.margin=unit(c(0,0,0,0),"points"))

theme0 <- function(...) theme( legend.position = "none",
                               panel.background = element_blank(),
                               panel.grid.major = element_blank(),
                               panel.grid.minor = element_blank(),
                               panel.margin = unit(0,"null"),
                               axis.ticks = element_blank(),
                               axis.text.x = element_blank(),
                               axis.text.y = element_blank(),
                               axis.title.x = element_blank(),
                               axis.title.y = element_blank(),
                               axis.ticks.length = unit(0,"null"),
                               axis.ticks.margin = unit(0,"null"),
                               panel.border=element_rect(color=NA),...)

p2 <- ggplot(DF,aes(x=x,colour=factor(group),fill=factor(group))) + 
  geom_density(alpha=0.5) + 
  scale_x_continuous(breaks=NULL,expand=c(0.02,0)) +
  scale_y_continuous(breaks=NULL,expand=c(0.02,0)) +
  theme_bw() +
  theme0(plot.margin = unit(c(1,0,0,2.2),"lines")) 
  
p3 <- ggplot(DF,aes(x=y,colour=factor(group),fill=factor(group))) + 
  geom_density(alpha=0.5) + 
  coord_flip()  + 
  scale_x_continuous(labels = NULL,breaks=NULL,expand=c(0.02,0)) +
  scale_y_continuous(labels = NULL,breaks=NULL,expand=c(0.02,0)) +
  theme_bw() +
  theme0(plot.margin = unit(c(0,1,1.2,0),"lines"))

grid.arrange(arrangeGrob(p2,ncol=2,widths=c(3,1)),
             arrangeGrob(p1,p3,ncol=2,widths=c(3,1)),
             heights=c(1,3))
图书馆(gridExtra) 种子(42)
DF这条线让你接近,但可能不太接近你想去的地方:看也很好!如何使密度图更靠近轴,使其与原始图形中的边界框接触?
p2 <- ggplot(DF,aes(x=x,colour=factor(group),fill=factor(group))) + 
  geom_density(alpha=0.5) + 
  scale_x_continuous(breaks=NULL,expand=c(0.02,0)) +
  scale_y_continuous(breaks=NULL,expand=c(0.00,0)) +
  theme_bw() +
  theme0(plot.margin = unit(c(1,0,-0.48,2.2),"lines")) 

p3 <- ggplot(DF,aes(x=y,colour=factor(group),fill=factor(group))) + 
  geom_density(alpha=0.5) + 
  coord_flip()  + 
  scale_x_continuous(labels = NULL,breaks=NULL,expand=c(0.02,0)) +
  scale_y_continuous(labels = NULL,breaks=NULL,expand=c(0.00,0)) +
  theme_bw() +
  theme0(plot.margin = unit(c(0,1,1.2,-0.48),"lines"))