我正在使用ggtern以第三级绘图的形式绘制大型数据集(请参见下面的示例)。
直到达到一定的数据大小,一切都是完美的,就像我在使用geom_density_tern()一样。因为我想可视化一个更复杂的数据集,将其全部加载,所以用ggplot渲染成为不可能(内存方面的限制)。我认为也许可以通过估算分别计算的kde2d矩阵的结果来解决。那就是我被困住的地方。我想知道是否有可能在ggtern中做到这一点?
无论如何,我都会添加一个最小的数据结构案例,并在此刻使用。
require(ggplot2)
require(ggtern)
set.seed(1)
mydata <- data.frame(
x = runif(100, min = 0.25, max = 0.5),
y = runif(100, min = 0.1, max = 0.4),
z = runif(100, min = 0.5, max = 0.7))
plot <- ggtern() +
theme_bw() +
theme_hidetitles() +
geom_density_tern(data = mydata,
aes(x = x, y = y, z = z, alpha = ..level.. ),
size = 0.1, linetype = "solid", fill = "blue")+
geom_point(data = mydata,
aes(x = x, y = y, z = z), alpha = 0.8, size = 1)
plot
这些多余的线再现了三元坐标系中的密度图:
library(MASS)
dataTern = transform_tern_to_cart(mydata$x,mydata$y,mydata$z)
dataTernDensity <- kde2d(x=dataTern$x, y=dataTern$y, lims = c(range(0,1), range(0,1)), n = 400)
image(dataTernDensity$x, dataTernDensity$y, dataTernDensity$z)
points(dataTern$x, dataTern$y, pch = 20, cex = 0.1)
segments(x0 = 0, y0 = 0, x1 = 0.5, y1 = 1, col= "white")
segments(x0 = 0, y0 = 0, x1 = 1, y1 = 0, col= "white")
segments(x0 = 0.5, y0 = 1, x1 = 1, y1 = 0, col= "white")
并获得此图:
在此先感谢您的帮助!
最佳答案
我们可以使用通常在Stat幕后使用的代码来解决此问题。在完全重写该软件包以使其与ggtern 2.0.1
兼容之后,几天前刚刚发布了CRAN上发布的ggplot2 2.0.0
,我熟悉一种适合您需要的方法。顺便说一句,出于您的兴趣,可以在here中找到ggtern 2.0.X
中的新功能的摘要:
在下面,请找到您问题的解决方案和工作代码,这是在等距对数比空间上计算出的密度估计值。
#Required Libraries
library(ggtern)
library(ggplot2)
library(compositions)
library(MASS)
library(scales)
set.seed(1) #For Reproduceability
mydata <- data.frame(
x = runif(100, min = 0.25, max = 0.5),
y = runif(100, min = 0.1, max = 0.4),
z = runif(100, min = 0.5, max = 0.7))
#VARIABLES
nlevels = 7
npoints = 200
expand = 0.5
#Prepare the data, put on isometric logratio basis
df = data.frame(acomp(mydata)); colnames(df) = colnames(mydata)
data = data.frame(ilr(df)); colnames(data) = c('x','y')
#Prepare the Density Estimate Data
h.est = c(MASS::bandwidth.nrd(data$x), MASS::bandwidth.nrd(data$y))
lims = c(expand_range(range(data$x),expand),expand_range(range(data$y),expand))
dens = MASS::kde2d(data$x,data$y,h=h.est,n=npoints,lims=lims)
#-------------------------------------------------------------
#<<<<< Presumably OP has data at this point,
# and so the following should achieve solution
#-------------------------------------------------------------
#Generate the contours via ggplot2's non-exported function
lines = ggplot2:::contour_lines(data.frame(expand.grid(x = dens$x, y = dens$y),
z=as.vector(dens$z),group=1),
breaks=pretty(dens$z,n=nlevels))
#Transform back to ternary space
lines[,names(mydata)] = data.frame(ilrInv(lines[,names(data)]))
#Render the plot
ggtern(data=lines,aes(x,y,z)) +
theme_dark() +
theme_legend_position('topleft') +
geom_polygon(aes(group=group,fill=level),colour='grey50') +
scale_fill_gradient(low='green',high='red') +
labs(fill = "Density",
title = "Example Manual Contours from Density Estimate Data")
关于r - 在ggtern中绘制kde结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34810857/