Enix
Enix

Reputation: 1

Kite Diagramm in R

i want to do a Kite Diagram in R for my masters thesis. I used the following code:

setwd("C:/Users/elisa/Documents/Jena/Elisa Wendisch/R_Files/Kite")
data <- read.table(file = "KITE_DipteraS1.txt", header = TRUE, dec = ",", sep = #"\t", na.strings = TRUE)

#clean Dates of X
colnames(data) <- gsub("^X", "", colnames(data))  # Remove leading 'X' added by R
colnames(data) <- trimws(colnames(data))         # Remove leading/trailing spaces

# Reshape the data from wide to long format
data_long <- melt(data, id.vars = "Species", variable.name = "Date", value.name = "Abundance")

unique(data_long$Date)
colnames(data)


# Convert Date column to proper Date format
data_long$Date <- as.Date(data_long$Date, format = "%d.%m.%Y")
data_long$Species <- as.factor(data_long$Species)
# Filter out rows with missing or non-numeric values
data_long <- data_long[!is.na(data_long$Abundance), ]
data_long <- rbind(
  data_long,
  transform(data_long, Abundance = -Abundance)  # Mirror the abundance
)

# Add mirrored abundance for the kite effect

head(data_long)
str(data_long)

ggplot(data_long, aes(x = Date, y = Species, fill = Species, height = Abundance)) +
  geom_area(position = "identity", alpha = 0.7) +  # Use geom_area to create the area plot
  theme_minimal() +
  labs(title = "Kite Diagram", x = "Date", y = "Species") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, size = 8)) +  # Rotate and reduce label size
  scale_fill_viridis_d(option = "plasma") +  # Use discrete color scale for species
  scale_x_date(date_breaks = "5 days", date_labels = "%d.%m.%Y")  # Show every 5th day

And got the following picture:

Kite Diagramm with a bars instead of kites

What is bascically missing is the Kite structure. Is there something in the code preventing it? I would appreciate any help.

I tried to change y into Abundance, which achieves a somewhat kite structure, but i want the Species to be on the y-axis. My goal is to get the species on the y-axis and the kite structur based on the abundance values.

Upvotes: 0

Views: 44

Answers (1)

Michiel Duvekot
Michiel Duvekot

Reputation: 1881

Use a geom_ribbon, then facet:

ggplot(data_long) +
  geom_ribbon(aes(
    x = Date,
    ymin = -Abundance/2,
    ymax = Abundance/2,
    fill = Species
  )) +  
  facet_wrap(~Species, ncol = 1, scales = "fixed")

Upvotes: 0

Related Questions