用R中的透明直方图绘制散点图 [英] scatterplot with alpha transparent histograms in R
本文介绍了用R中的透明直方图绘制散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在R中使用alpha透明,无标度直方图进行散点图?像这个数字一样?
看起来不是在ggplot2中制作的。
有人知道使用了什么命令?
解决方案
library(ggplot2)
library(gridExtra)$ b $ (100),mean = c(1,5)),y = rlnorm(100,meanlog = c(8) ,6)),组= 1:2)
p1 < - ggplot(DF,aes(x = x,y = y,color = 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 )=
p2 < - ggplot(DF,aes(x = x,color = 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),l (a)(x = y,color = 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))
编辑:
我找不到是什么原因导致密度下方的空间。你可以摆弄剧情边缘以避免它,但我不太喜欢那样。
p2 < - ggplot( DF,aes(x = x,color = 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 ,-0.48,2.2),lines))
p3 < - ggplot(DF,aes(x = y,color = 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,break = NULL,expand = c(0.00,0))+
theme_bw()+
theme0(plot.margin = unit(c(0,1,1.2,-0.48)行))
How can scatter plots with alpha transparent, scale-less histograms can be made in R, like this figure?
looks like it's not made in ggplot2.
does anyone know what command is used?
解决方案
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))
Edit:
I couldn't find out what causes the space below the densities geoms. You can fiddle with the plot margins to avoid it, but I don't really like that.
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"))
这篇关于用R中的透明直方图绘制散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文