【问题标题】:Ternary plot and filled contour三元图和填充轮廓
【发布时间】:2012-06-04 09:50:22
【问题描述】:

用户,我想了解一些关于三元图(“vcd”)的提示。

我有这个数据框:

a <- c(0.1, 0.5, 0.5, 0.6, 0.2, 0, 0, 0.004166667, 0.45) 
b <- c(0.75,0.5,0,0.1,0.2,0.951612903,0.918103448,0.7875,0.45)
c <- c(0.15,0,0.5,0.3,0.6,0.048387097,0.081896552,0.208333333,0.1) 
d <- c(500,2324.90,2551.44,1244.50, 551.22,-644.20,-377.17,-100, 2493.04) 
df <- data.frame(a, b, c, d)

我正在构建一个三元情节:

ternaryplot(df[,1:3], df$d)

如何映射连续变量d,得到类似的结果?

【问题讨论】:

  • 欢迎来到 StackOverflow。您可能应该用您正在编写的语言标记您的问题,或者至少在您的问题中提及该语言。为此,您可以使用edit 按钮。
  • RSiteSearch("ternary contour") 开始,看看是否有帮助?还有library("sos"); findFn("ternary contour")
  • 谢谢 Ben,我也在看这个代码:r.789695.n4.nabble.com/… 但它相当复杂。
  • 在我对 ggplot2 的三元扩展中有一个修改过的 geom / stat_density2d 函数。 ggtern.com。看看这里:ggtern.com/faceting

标签: r plot ternary interpolation


【解决方案1】:

我需要解决一个类似的问题,这部分是编写一个包作为 ggplot2 扩展的催化剂,用于三元图。该软件包可通过CRAN 获得。

这个问题的输出:

构建上述内容的代码

#Orignal Data as per Question
a <- c(0.1, 0.5,0.5, 0.6, 0.2, 0          , 0         , 0.004166667, 0.45) 
b <- c(0.75,0.5,0  , 0.1, 0.2, 0.951612903,0.918103448, 0.7875     , 0.45)
c <- c(0.15,0  ,0.5, 0.3, 0.6, 0.048387097,0.081896552, 0.208333333, 0.10) 
d <- c(500,2324.90,2551.44,1244.50, 551.22,-644.20,-377.17,-100, 2493.04) 
df <- data.frame(a, b, c, d)

#For labelling each point.
df$id <- 1:nrow(df)

#Build Plot
ggtern(data=df,aes(x=c,y=a,z=b),aes(x,y,z)) + 
  stat_density2d(geom="polygon",
                 n=400,
                 aes(fill=..level..,
                 weight=d,
                 alpha=abs(..level..)),
                 binwidth=100) + 
  geom_density2d(aes(weight=d,color=..level..),
                 n=400,
                 binwidth=100) +
  geom_point(aes(fill=d),color="black",size=5,shape=21) + 
  geom_text(aes(label=id),size=3) + 
  scale_fill_gradient(low="yellow",high="red") + 
  scale_color_gradient(low="yellow",high="red") + 
  theme_tern_rgbw() + 
  theme(legend.justification=c(0,1), legend.position=c(0,1)) + 
  guides(fill = guide_colorbar(order=1),
         alpha= guide_legend(order=2),
         color="none") + 
  labs(  title= "Ternary Plot and Filled Contour",
         fill = "Value, V",alpha="|V - 0|")

#Save Plot
ggsave("TernFilled.png")

【讨论】:

  • @NicholasHamilton 除了两种颜色的渐变,是否有可能获得多色渐变?
  • 有一个三 (3) 种颜色渐变 (docs.ggplot2.org/current/scale_gradient2.html),您可以在其中指定低、中和高颜色,但除此之外,我认为它会违反语法的原则Wilkinson 的图形,是 ggplot2 的基础理论(参见此处ggtern.com/resources
  • @NicholasHamilton 嗨,我正在尝试复制您的示例,但是似乎不推荐使用 ggtern 中的 stat_density_2d 。我尝试了 stat_density_tern 但无法将重量带入内部。任何帮助深表感谢。 TIA
【解决方案2】:

这可能不是最优雅的方法,但它可以工作(从头开始,但不使用ternaryplot:我不知道该怎么做)。

a<- c (0.1, 0.5, 0.5, 0.6, 0.2, 0, 0, 0.004166667, 0.45) 
b<- c (0.75,0.5,0,0.1,0.2,0.951612903,0.918103448,0.7875,0.45)
c<- c (0.15,0,0.5,0.3,0.6,0.048387097,0.081896552,0.208333333,0.1) 
d<- c (500,2324.90,2551.44,1244.50, 551.22,-644.20,-377.17,-100, 2493.04) 
df<- data.frame (a, b, c)


# First create the limit of the ternary plot:
plot(NA,NA,xlim=c(0,1),ylim=c(0,sqrt(3)/2),asp=1,bty="n",axes=F,xlab="",ylab="")
segments(0,0,0.5,sqrt(3)/2)
segments(0.5,sqrt(3)/2,1,0)
segments(1,0,0,0)
text(0.5,(sqrt(3)/2),"c", pos=3)
text(0,0,"a", pos=1)
text(1,0,"b", pos=1)

# The biggest difficulty in the making of a ternary plot is to transform triangular coordinates into cartesian coordinates, here is a small function to do so:
tern2cart <- function(coord){
    coord[1]->x
    coord[2]->y
    coord[3]->z
    x+y+z -> tot
    x/tot -> x  # First normalize the values of x, y and z
    y/tot -> y
    z/tot -> z
    (2*y + z)/(2*(x+y+z)) -> x1 # Then transform into cartesian coordinates
    sqrt(3)*z/(2*(x+y+z)) -> y1
    return(c(x1,y1))
    }

# Apply this equation to each set of coordinates
t(apply(df,1,tern2cart)) -> tern

# Intrapolate the value to create the contour plot
resolution <- 0.001
require(akima)
interp(tern[,1],tern[,2],z=d, xo=seq(0,1,by=resolution), yo=seq(0,1,by=resolution)) -> tern.grid

# And then plot:
image(tern.grid,breaks=c(-1000,0,500,1000,1500,2000,3000),col=rev(heat.colors(6)),add=T)
contour(tern.grid,levels=c(-1000,0,500,1000,1500,2000,3000),add=T)
points(tern,pch=19)

【讨论】:

    【解决方案3】:

    我之前的回答使用了密度估计。这是一个使用线性回归的例子。

    df <- data.frame(a, b, c, d)
    ggtern(df,aes(a,c,b)) + 
      geom_interpolate_tern(aes(value=d,fill=..level..),
                            binwidth=500,
                            colour="white") +
      geom_point(aes(fill=d),color="black",shape=21,size=3) + 
      scale_fill_gradient(low="yellow",high="red") +
      theme(legend.position=c(0,1),legend.justification=c(0,1)) + 
      labs(fill="Value, d")
    

    【讨论】:

      【解决方案4】:

      非常感谢您的提示,这是我的最终结果:

      #Rename header
      names(SI) [6] <- "WATER%"
      names(SI) [7] <- "VEGETATION%"
      names(SI) [8] <- "SOIL%"
      
      #pdf(file="prova_ternary12.pdf", width = 5, height =5)
      ##++++++++++++++++++++++++++++++
      install.packages("colourschemes", repos="http://R-Forge.R-project.org")
      library(colourschemes)
      rs = rampInterpolate ( limits =c(-0.8 , 0.8),
                             ramp = c("red4", "red", "orangered", "orange", "darkgoldenrod1", "white", 
                                      "cyan2", "blue", "darkblue", "blueviolet", "purple3") )
      rs(-0.8)
      rs(-0.6000)
      rs(-0.4)
      rs(-0.2)
      rs(0)
      rs(0.2)
      rs(0.4)
      rs(0.6000)
      rs(0.8000)
      
      
      
      #++++++++++++++++++++++++++++++
      
      #TERNARYPLOT (vcd)
      library(vcd)
      png(file="ternary.png", width=800, height=800)
       ternaryplot(
        SI[,6:8],
        bg = "lightgray",
        grid_color = "black",
        labels_color = "black",   
        dimnames_position = c("corner"),
        #dimnames = 10,
        newpage = T,
        #dimnames_color = "green",
        border = "black",
        pop=T,
        #SI$MEAN_b2b6.tm,
        col=rs(SI$MEAN_b2b6.TM_V2),
        #col = ifelse(SI$MEAN_b1b6.tm > 0, "blue", "#cd000020"), 
        pch=13, cex=.4, prop_size = F,
        labels = c("outside"),
        #size=SI$MEAN_b1b6.tm,
        main="b4b6  -TM data-")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-17
        • 1970-01-01
        • 1970-01-01
        • 2013-01-01
        • 2013-10-13
        • 2014-11-26
        • 1970-01-01
        相关资源
        最近更新 更多