通过ggplot2将纹理类别添加到土壤分类三角形 [英] Add texture classes to soil classification triangle via ggplot2

查看:101
本文介绍了通过ggplot2将纹理类别添加到土壤分类三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些实验数据,想在土壤分类三角形上作图以识别质地.我正在使用在这里找到的三角形:

http://www.ggtern.com/2014 /01/15/usda-textural-soil-classification/

我要显示粘土,沙子和淤泥为点.

这是我的数据:

structure(list(Clay = c(1.67, 1.29, 1.1, 1.57, 1.52, 1.72, 1.53, 
1.47, 1.34, 0.84, 1.18, 1.03, 1.1, 1.08, 1.41, 1.17), Silt = c(59.21, 
59.39, 54.4, 57.65, 58.42, 60.17, 64.98, 60.67, 57.84, 55.73, 
56.7, 50.8, 53.68, 57.92, 59.97, 56.62), Sand = c(39.09, 39.33, 
44.5, 40.79, 40.04, 38.08, 33.47, 37.89, 40.81, 43.44, 42.11, 
48.17, 45.24, 41.01, 38.63, 42.21)), .Names = c("Clay", "Silt", 
"Sand"), row.names = c(NA, -16L), class = "data.frame")

感谢您的建议. AT.

解决方案

很抱歉,CRAN删除了我的软件包,我只是发现了一下,等到我得到解决后,它将得到解决,该源在过渡期间仍然可用此处.

无论如何,您的解决方案非常容易实现,将颜色的美学"定义移动,并从全局"定义填充到仅特定于geom_polygon(...),如下所示:

让我们从对您的问题的链接进行(略作修改)的USDA分类开始:

# Load the required libraries
library(ggtern)
library(plyr)
library(grid)

# Load the Data. (Available in ggtern 1.0.3.0 next version)
data(USDA)

# Put tile labels at the midpoint of each tile.
USDA.LAB = ddply(USDA, 'Label', function(df) {
  apply(df[, 1:3], 2, mean)
})

# Tweak
USDA.LAB$Angle = 0
USDA.LAB$Angle[which(USDA.LAB$Label == 'Loamy Sand')] = -35

# Construct the plot.
# NOTE aes(color=Label,fill=Label) in 3rd line below
base = ggplot(data = USDA, aes(y=Clay, x=Sand, z=Silt)) +
  coord_tern(L="x",T="y",R="z") +
  geom_polygon(alpha = 0.75, size = 0.5, color = 'black',aes(color=Label,fill=Label)) +
  geom_text(data = USDA.LAB,
            aes(label = Label, angle = Angle),
            color = 'black',
            size = 3.5) +
  theme_rgbw() +
  theme_showsecondary() +
  theme_showarrows() +
  custom_percent("Percent") +
  theme(legend.justification = c(0, 1),
        legend.position      = c(0, 1),
        axis.tern.padding    = unit(0.15, 'npc')) +
  labs(title = 'USDA Textural Classification Chart',
       fill  = 'Textural Class',
       color = 'Textural Class')
base

现在您可以添加数据:

df = structure(list(Clay = c(1.67, 1.29, 1.1, 1.57, 1.52, 1.72, 1.53, 1.47, 1.34, 0.84, 1.18, 1.03, 1.1, 1.08, 1.41, 1.17), 
                    Silt = c(59.21,59.39, 54.4, 57.65, 58.42, 60.17, 64.98, 60.67, 57.84, 55.73,56.7, 50.8, 53.68, 57.92, 59.97, 56.62), 
                    Sand = c(39.09, 39.33, 4.5, 40.79, 40.04, 38.08, 33.47, 37.89, 40.81, 43.44, 42.11, 48.17, 45.24, 41.01, 38.63, 42.21)), 
               .Names = c("Clay", "Silt","Sand"), row.names = c(NA, -16L), class = "data.frame")
base + geom_point(data=df,size=3)

产生以下内容:

I have some experimental data and would like to plot on the soil classification triangle to identify the texture. I am using the triangle found here:

http://www.ggtern.com/2014/01/15/usda-textural-soil-classification/

I want to display clay, sand and silt as points.

Here is my data:

structure(list(Clay = c(1.67, 1.29, 1.1, 1.57, 1.52, 1.72, 1.53, 
1.47, 1.34, 0.84, 1.18, 1.03, 1.1, 1.08, 1.41, 1.17), Silt = c(59.21, 
59.39, 54.4, 57.65, 58.42, 60.17, 64.98, 60.67, 57.84, 55.73, 
56.7, 50.8, 53.68, 57.92, 59.97, 56.62), Sand = c(39.09, 39.33, 
44.5, 40.79, 40.04, 38.08, 33.47, 37.89, 40.81, 43.44, 42.11, 
48.17, 45.24, 41.01, 38.63, 42.21)), .Names = c("Clay", "Silt", 
"Sand"), row.names = c(NA, -16L), class = "data.frame")

Thanks for your suggestions. AT.

解决方案

Sorry about CRAN removing my package, I only just found out and will get that resolved when I get some time, the source is still available in the interim HERE.

Anyway, your solution is quite easy to achieve, move the 'aesthetic' definitions for color and fill away from being 'global' definitions, to only being specific to the geom_polygon(...), as per below:

Lets start with a (slightly) modified USDA classification, taken from the link in your question:

# Load the required libraries
library(ggtern)
library(plyr)
library(grid)

# Load the Data. (Available in ggtern 1.0.3.0 next version)
data(USDA)

# Put tile labels at the midpoint of each tile.
USDA.LAB = ddply(USDA, 'Label', function(df) {
  apply(df[, 1:3], 2, mean)
})

# Tweak
USDA.LAB$Angle = 0
USDA.LAB$Angle[which(USDA.LAB$Label == 'Loamy Sand')] = -35

# Construct the plot.
# NOTE aes(color=Label,fill=Label) in 3rd line below
base = ggplot(data = USDA, aes(y=Clay, x=Sand, z=Silt)) +
  coord_tern(L="x",T="y",R="z") +
  geom_polygon(alpha = 0.75, size = 0.5, color = 'black',aes(color=Label,fill=Label)) +
  geom_text(data = USDA.LAB,
            aes(label = Label, angle = Angle),
            color = 'black',
            size = 3.5) +
  theme_rgbw() +
  theme_showsecondary() +
  theme_showarrows() +
  custom_percent("Percent") +
  theme(legend.justification = c(0, 1),
        legend.position      = c(0, 1),
        axis.tern.padding    = unit(0.15, 'npc')) +
  labs(title = 'USDA Textural Classification Chart',
       fill  = 'Textural Class',
       color = 'Textural Class')
base

Now you can add your data:

df = structure(list(Clay = c(1.67, 1.29, 1.1, 1.57, 1.52, 1.72, 1.53, 1.47, 1.34, 0.84, 1.18, 1.03, 1.1, 1.08, 1.41, 1.17), 
                    Silt = c(59.21,59.39, 54.4, 57.65, 58.42, 60.17, 64.98, 60.67, 57.84, 55.73,56.7, 50.8, 53.68, 57.92, 59.97, 56.62), 
                    Sand = c(39.09, 39.33, 4.5, 40.79, 40.04, 38.08, 33.47, 37.89, 40.81, 43.44, 42.11, 48.17, 45.24, 41.01, 38.63, 42.21)), 
               .Names = c("Clay", "Silt","Sand"), row.names = c(NA, -16L), class = "data.frame")
base + geom_point(data=df,size=3)

Which produces the following:

这篇关于通过ggplot2将纹理类别添加到土壤分类三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆