我有一个土壤质地数据集。我制作了一个土壤纹理三角形,代表不同项目的土壤纹理。
我想向纹理三角形添加质心,但仅限于具有不同纹理的项目。我想在一张图上显示,即使在一个项目中具有不同的纹理,它们也属于同一个项目。
是否可以使用 ggplot2 来完成此操作,或者是否有特定的包可以做到这一点?
这是我尝试使用 ggplot2 代码。我尝试了在土壤纹理三角形上绘制质心的两种版本。我能够添加这些质心,但在我的试用版本 1 中,并非所有具有不同纹理的项目都有质心。 在我的试用版本 2 中,完全不同的项目有质心。我希望所有具有不同纹理的项目都有质心,并希望它们全部显示在图中。
`# Plotting the soil texture triangle
theme_set(theme_bw())
p \<-filtered_soil_triangle %\>%
ggtern(aes(
x = SAND,
y = CLAY,
z = SILT,
color = Project )) +
geom_point(size = ifelse(is.na(filtered_soil_triangle $Project), 0, 6)) +
theme_showarrows() +
labs(yarrow = "Clay (%)",
zarrow = "Silt (%)",
xarrow = "Sand(%)",
color = "Project") +
scale_color_paletteer_d("rcartocolor::Safe") + # Use the Safe color palette
theme_clockwise()+
theme(
legend.title = element_text(size = 14), # Increase legend title size
legend.text = element_text(size = 12) # Increase legend text size
)`
`# Trial Version 1
## Add arrows connecting points with the same Project and different map_texture
### Create a new data frame for segments
segments_data \<- filtered_soil_triangle %\>%
group_by(Project, map_texture) %\>%
arrange(SAND, CLAY, SILT) %\>%
mutate(next_SAND = lead(SAND),
next_CLAY = lead(CLAY),
next_SILT = lead(SILT)) %\>%
filter(!is.na(next_SAND))
### Add segments to the plot
p + geom_segment(data = segments_data,
aes(x = SAND, y = CLAY, z = SILT,
xend = next_SAND, yend = next_CLAY, zend = next_SILT),
arrow = arrow(length = unit(0.01, "inches")),
color = "black",
linewidth = 0.5)`
`# Trial Version 2
## Add arrows connecting points with the same Project and different map_texture
### Create a new data frame for segments
segments_data \<- filtered_soil_triangle %\>%
group_by(Project) %\>%
arrange(SAND, CLAY, SILT) %\>%
mutate(next_SAND = lead(SAND),
next_CLAY = lead(CLAY),
next_SILT = lead(SILT),
next_map_texture = lead(map_texture)) %\>%
filter(!is.na(next_SAND) & map_texture != next_map_texture)
### Add segments to the plot
p + geom_segment(data = segments_data,
aes(x = SAND, y = CLAY, z = SILT,
xend = next_SAND, yend = next_CLAY, zend = next_SILT),
arrow = arrow(length = unit(0.01, "inches")),
color = "black",
linewidth = 0.5) # Use line width instead of size`
问题需要专业澄清;我认为不需要“质心”作为摘要指标。然而,如果每个项目的平均值是一个有效的指标,那么这里有一个用
ggplot
(tidyverse
) 和 ggtern
来实现的表示。 您可以通过单个站点 group_by(Project)
、summarise()
和 filter()
删除项目。通过一些aes
的理论调整,可以表示平均值和位置。
library(tidyverse)
library(ggtern)
# Data per site
soil_texture_df <- data.frame(SAND=c(12, 20, 80),
SILT=c(50, 48, 5)) |>
mutate(CLAY = 100 - SAND - SILT) |>
mutate(Project = c("A","A","B"))
#Summarise per project
by_project <- soil_texture_df |> group_by(Project) |>
summarise( across(SAND:CLAY, mean), n = n()) |>
filter(n>1)
by_project |>
ggtern(aes(
x = SAND, y = CLAY, z = SILT, color = Project )) +
geom_point( alpha = 0.5, aes(size ="Project average"))+
geom_point( alpha = 1, data = soil_texture_df, aes(size ="Site")) +
scale_size_manual( values = c("Site"=2 ,"Project average" =6 ) )
创建于 2024 年 12 月 19 日,使用 reprex v2.1.0