-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPebbleCountGrainSizePlots
More file actions
84 lines (68 loc) · 2.66 KB
/
PebbleCountGrainSizePlots
File metadata and controls
84 lines (68 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
---
title: "Icicle Grain Size Distribution"
author: "JVDH"
date: "2025-03-07"
output: html_document
editor_options:
chunk_output_type: console
---
```{r}
library(tidyverse)
#getwd()
#setwd("//neo/Terra/ClientFiles/U-Z/YN_Reach_Assessments_2024_240213/Design_Analysis/Geomorphology/Icicle/PebbleCounts_GrainSize/RawData")
#df <- read_csv("GrainSizeDistributionForPieCharts.csv")
# Convert data to long format for plotting
df_long <- df %>%
pivot_longer(cols = c(SandPct, GravelPct, CobblePct, BoulderPct),
names_to = "SedimentType",
values_to = "Percentage") %>%
mutate(Percentage_Plot = Percentage * 100) %>%
mutate(SedimentType = str_sub(SedimentType, 1, -4))
class(df_long$Percentage_Plot)
```
plot with for loop
```{r}
library(ggrepel) # For better text placement
unique_reaches <- unique(df_long$reach)
# Loop through each reach and generate a pie chart
for (reach_name in unique_reaches) {
# Filter data for the current reach
df_filtered <- df_long %>%
filter(reach == reach_name)
# Generate the pie chart
p <- ggplot(df_filtered, aes(x = "", y = Percentage_Plot, fill = SedimentType)) +
geom_bar(stat = "identity", width = 1) +
coord_polar(theta = "y") +
theme_void() +
theme(legend.position = "right") +
labs(title = paste("Sediment Composition -", reach_name)) +
geom_text_repel(aes(label = ifelse(Percentage_Plot > 0,
paste0(SedimentType, ": ", round(Percentage_Plot, 1), "%"),
"")),
position = position_stack(vjust = 0.5),
size = 3,
box.padding = 0.5, # Adjusts distance between labels
point.padding = 0.2,
segment.color = "grey50") # Adds guiding lines to labels
# Print the plot
print(p)
# Optionally, save the plot as an image file
ggsave(filename = paste0("Sediment_Composition_", reach_name, ".png"), plot = p, width = 6, height = 6, dpi = 300)
}
```
or plot individual charts
```{r}
# Filter data for the selected reach
df_filtered <- df_long %>%
filter(reach == "Reach 20 GC 1") # update as needed
# Generate the pie chart for the selected reach
ggplot(df_filtered, aes(x = "", y = Percentage_Plot, fill = SedimentType)) +
geom_bar(stat = "identity", width = 1) +
coord_polar(theta = "y") +
theme_void() +
theme(legend.position = "right") +
labs(title = paste("Sediment Composition -", "Reach 20 GC 1")) +
geom_text(aes(label = paste0(SedimentType, ": ", round(Percentage_Plot, 1), "%")),
position = position_stack(vjust = 0.5),
size = 3) # Adjust text size as needed
```