18
A
回答
33
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))
編輯:
我無法找出是什麼原因導致下面的密度geoms的空間。你可以擺弄劇情邊緣來避開它,但我不太喜歡那個。
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"))
+0
非常好!你怎麼能讓密度圖更接近軸線,以便它們像原始圖中那樣觸摸圖的邊界框? – user248237dfsf
0
我不知道是否有一個包直接做,但我確定這可以在R.完成。透明度很容易:你添加另一個兩位數的顏色的RGB規範的一個給定透明度:
#FF0000 # red
#FF0000FF # full opacity
#FF000000 # full transparency
使用layout
函數也可以輕鬆地組合不同的圖。至於垂直密度圖,它與切換x和y的水平圖相同。給出的例子here可以很容易地擴展爲包括顏色,較小的邊距等。如果這種描述不夠充分,我可以嘗試提出一個更詳細的例子。
相關問題
- 1. 散點圖與R中
- 2. Alpha與兩個透明圖像混合
- 3. 散點圖對於點密集的區域,alpha仍然不透明
- 4. R ggplot geom_hex alpha透明度
- 5. 繪製直方圖或散點圖與當前的直方圖的matplotlib
- 6. 任何可以使散點圖中的繪圖點在R中更透明的方法?
- 7. 對齊直方圖和散點圖在R
- 8. Matplotlib散點圖和直方圖
- 9. 將直方圖放在散點圖上
- 10. gnuplot的 - 設置透明直方圖
- 11. 的Python - 堆疊兩個直方圖與散點圖
- 12. Matplotlib:繪製具有不透明邊緣的透明直方圖
- 13. 散景中的透明圖例
- 14. R - 差散點圖
- 15. Java透明度:Graphics2D.setComposite(...,alpha)與顏色(...,alpha)
- 16. 使用因子變量在R中的散點圖上刻畫直方圖
- 17. C#Alpha混合透明圖片框
- 18. ggplot圖例alpha完全透明
- 19. 散點圖中垂直線的點
- 20. 如何用SPSS製作散點圖半透明的點?
- 21. ggplot2 stat_density2d的透明度和Alpha級別R和地圖和圖層
- 22. 如何在Matplotlib的3D散點圖中關閉透明度?
- 23. 添加直線/方程以散點圖
- 24. 的R - 直方圖
- 25. 直方圖中的R
- 26. R:直方圖中的簇
- 27. 與熱圖條XY散點圖在邊緣中的R
- 28. 顏色直方圖與R中
- 29. PNG與alpha透明SVG與potrace
- 30. [R標識散點圖
該線程被關閉,但可能不太想要的位置是:http://stackoverflow.com/questions/8545035/scatterplot-with-marginal-histograms-in- ggplot2 –
參見http://blog.mckuhn.de/2009/09/learning-ggplot2-2d-plot-with.html –