fix tree health plot

This commit is contained in:
aaron
2024-03-11 20:03:52 +01:00
parent 94fcd415f8
commit 576255116c

View File

@@ -15,32 +15,29 @@ df <- read_ods("ironwood_data_cleaned.ods", sheet = 1)
# asses the tree health # asses the tree health
## ##
# create a vector with our condition names # First, let's create a vector of condition names corresponding to the health index numbers
condition_names <- c("healthy", "light damage", "medium damage", "severe damage", "at point of death") condition_names <- c("Healthy", "Light damage", "Medium damage", "Severe damage", "At point of death")
# Define colors for each condition # Define colors for each condition
condition_colors <- c("green", "yellow", "orange", "red", "black") condition_colors <- c("green", "yellow", "orange", "red", "black")
# Now, let's create the histogram # Calculate the percentage of trees in each health condition
hist(df$Tree_Health_Index, percentage <- prop.table(table(df$Tree_Health_Index)) * 100
breaks = 0:5 - 0.5, # Setting breaks at midpoints between integers # Now, let's create the bar plot
barplot(percentage,
names.arg = condition_names,
main = "Distribution of Tree Health Index", main = "Distribution of Tree Health Index",
xlab = "Health Index", xlab = "Health Index",
ylab = "Number of Trees", ylab = "Percentage of Trees",
col = condition_colors, # Assigning colors based on the health index values ylim = c(0, max(percentage) + 10),
border = "black", # Border color of the bars col = condition_colors,
xlim = c(-0.5, 4.5), # Setting x-axis limits to include all health index values border = "black")
ylim = c(0, max(table(df$Tree_Health_Index))*1.1), # Setting y-axis limits slightly above the maximum frequency
axes = FALSE) # Suppressing axes for customization
# Add axis labels
axis(1, at = 0:4, labels = condition_names)
axis(2)
# Adding a legend # Adding a legend
legend("topright", legend = condition_names, fill = condition_colors, border.col = "black") legend("topright", legend = condition_names, fill = condition_colors)
# Add a title
title(main = "Distribution of Tree Health Index")
# Adding a grid # Adding a grid
grid(nx = NULL, ny = NULL, col = "lightgray", lty = "dotted") #grid(nx = NULL, ny = NULL, col = "lightgray", lty = "dotted")
# Adding a box around the plot # Adding a box around the plot
box() box()
# Add labels with the percentage of trees in each bar
text(x = barplot(percentage, plot = FALSE), y = percentage, labels = paste0(round(percentage, 1), "%"), pos = 3)
## ##
# Perform a shapiro-wilk normality test # Perform a shapiro-wilk normality test