我想用 R 为我的硕士论文做一个风筝图。 我使用了以下代码:
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
并得到以下图片:
基本上缺少的是 Kite 结构。代码中有什么东西阻止它吗?我将不胜感激任何帮助。
我尝试将 y 更改为丰度,这实现了某种风筝结构,但我希望物种位于 y 轴上。我的目标是根据丰度值获取 y 轴上的物种和风筝结构。
使用geom_ribbon,然后facet:
ggplot(data_long) +
geom_ribbon(aes(
x = Date,
ymin = -Abundance/2,
ymax = Abundance/2,
fill = Species
)) +
facet_wrap(~Species, ncol = 1, scales = "fixed")