基于樣本時間序列資料和情節代碼打擊:
df <- structure(list(date = structure(c(18901, 18902, 18903, 18904,
18905, 18906, 18907, 18908, 18909, 18910, 18911, 18912, 18913,
18914), class = "Date"), value = c(190.3, 174.9, 163.2, 168.4,
168.6, 168.2, 163.5, 161.6, 172.9, 166.5, 175.2, 197.7, 212.1,
177.9), type = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L)), row.names = c(NA, -14L), class = "data.frame")
df$type <- as.factor(df$type)
df$date <- as.Date(df$date)
ggplot(df, aes(x=date, y=value, group=type,
color = type, fill = type))
geom_area(alpha=0.4)
geom_hline(yintercept=max(df$value), linetype='dotted', col = 'red')
scale_color_manual(values=c("1" = "gray", "2" = "red", "3" = "blue"),
aesthetics = c("color", "fill"), name = "type")
theme(
# axis.text.x=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank())
出去:

我想更進一步,只顯示y軸標簽的最大值和最小值以及軸標簽的開始日期和結束日期x。效果將類似于下圖。

我怎么能用 ggplot2 做到這一點?真誠的感謝。
uj5u.com熱心網友回復:
您可以通過將breaks比例設定為等于映射到x和的變數范圍來實作您想要的結果y:
筆記:
如果你想從零開始 y 尺度,你可以做
breaks = c(0, max(df$value)).除洗掉軸標題的通過
theme,你可以通過簡單地做到這一點labs。
library(ggplot2)
ggplot(df, aes(
x = date, y = value, group = type,
color = type, fill = type
))
geom_area(alpha = 0.4)
scale_y_continuous(breaks = range(df$value))
scale_x_date(breaks = range(df$date))
scale_color_manual(
values = c("1" = "gray", "2" = "red", "3" = "blue"),
aesthetics = c("color", "fill"), name = "type"
)
labs(x = NULL, y = NULL)

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358871.html
