ggplot 객체의 x 축이 인자 또는 문자 분류 객체가 될 시계열 데이터를 더 잘 그리는 방법을 아는 사람이 있는지 궁금합니다. 다음은 겨울에 다양성이 낮아지고 멸종되는 인구의 예입니다. 그러면 인구는 가을에 증가 할 것입니다. 이 데이터를 표시하는 더 좋은 방법이 있습니까? 모든 제안을 주시면 감사하겠습니다.
library(ggplot2)
library(data.table)
d <- rep(c(0, 10, 6, 0), 4)
time <- rep(c("Fall", "Fall", "Winter", "Winter"), 4)
year <- rep(1:4, each=4)
dt <- data.table(cbind(d, time, year))
dt$d <- as.numeric(dt$d)
dt$year <- as.numeric(dt$year)
ggplot(dt, aes(x=interaction(time, year), y=d, group=1)) +
geom_line(position="identity", size=1) +
geom_point(size=2)+
labs(x="Year",
y= "Diversity") +
theme_classic()
특히, 저는 매년 겨울과 가을 사이에 휴식을 취하고 싶습니다. 그래서 매년 새해 사이의 차이.
나는 다른 접근 방식을 사용합니다-색상을 사용하여 모든 겨울과 가을 사이의 휴식을 시각화하고 ( geom_rect
), x 축을 연속 ( 1
데이터 크기 에서)으로 변환 합니다.
OP 데이터 사용 :
library(ggplot2)
library(data.table)
# Prepare data
# Create continuous data for the x-axis
dt[, X := 1:.N]
# Define begging of the year
break_labels <- dt[, min(X), year]
# Plot
ggplot(dt, aes(X, d)) +
# Use 0.5 to extend colour around the X
geom_rect(aes(xmin = X - 0.5, xmax = X + 0.5, ymin = -Inf, ymax = Inf, fill = time)) +
geom_point(size = 3) +
geom_line(size = 1, linetype = 2) +
# Specify wanted colour code
scale_fill_manual(values = c("#EDBB99", "#D6EAF8")) +
# Specify breaks only for the begging of the year
scale_x_continuous(breaks = break_labels$V1, labels = break_labels$year) +
labs(
x = "Year",
y = "Diversity",
fill = "Season"
) +
theme_classic()
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다