我有以下資料框和基于它的堆積面積圖:
df <- data.frame (Year = c("2010", "2010", "2010", "2010", "2011","2011","2011","2011","2012","2012","2012","2012","2013","2013","2013","2013"),
Sales = c(100000000,200000000,50000000,500000000,400000000,200000000,400000000,145000000,100000000,456000000,345000000,321000000,100000000,200000000,250000000,400000000),
Category = c("A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D","A", "B", "C", "D"))
df$Year <- as.integer(df$Year)
df %>%
ggplot(aes(x = Year, y = Sales, fill = Category))
geom_area() scale_x_continuous(breaks=2010:2013)
現在我想自動化這個程序,根據資料框中最早的年份向圖表添加標題,所以我想以某種方式編碼:“min(df$Year)
按類別銷售”。
uj5u.com熱心網友回復:
您可以使用paste
inggtitle()
或labs(title = )
函式。
library(dplyr)
library(ggplot2)
df %>%
ggplot(aes(x = Year, y = Sales, fill = Category))
geom_area()
scale_x_continuous(breaks=2010:2013)
ggtitle(paste("Sale since", min(df$Year), "by categories"))
資料
df <- structure(list(Year = c(2010L, 2010L, 2010L, 2010L, 2011L, 2011L,
2011L, 2011L, 2012L, 2012L, 2012L, 2012L, 2013L, 2013L, 2013L,
2013L), Sales = c(1e 08, 2e 08, 5e 07, 5e 08, 4e 08, 2e 08, 4e 08,
1.45e 08, 1e 08, 4.56e 08, 3.45e 08, 3.21e 08, 1e 08, 2e 08,
2.5e 08, 4e 08), Category = c("A", "B", "C", "D", "A", "B", "C",
"D", "A", "B", "C", "D", "A", "B", "C", "D")), row.names = c(NA,
-16L), class = "data.frame")
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/469532.html