圖為ggplot2從ver.3.3.3升級到ver前后的區別。3.3.4
library(RColorBrewer)
library(ggplot2)
df <- structure(list(name = structure(c(11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L), .Label = c("5303 B", "5303 A",
"5302 B", "5302 A", "5301 B", "5301 A", "5202 B", "5202 A", "5201 B",
"5201 A", "5101 B", "5101 A"), class = "factor"), stadio = c(2,
2, 4, 4, 6, 6, 8, 8, 10, 10, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10,
1, 1, 3, 3, 7, 7, 9, 9, 11, 11, 1, 1, 3, 3, 7, 7, 9, 9, 11, 11
), variable = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("start_date",
"end_date"), class = "factor"), value = c("15/10/2021", "03/01/2022",
"03/01/2022", "30/03/2022", "30/03/2022", "30/03/2023", "30/03/2023",
"15/09/2023", "15/09/2023", "29/12/2023", "15/10/2021", "03/01/2022",
"03/01/2022", "30/03/2022", "30/03/2022", "30/03/2023", "30/03/2023",
"15/09/2023", "15/09/2023", "29/12/2023", "26/11/2021", "14/01/2022",
"14/01/2022", "30/06/2022", "30/06/2022", "30/03/2023", "30/03/2023",
"15/09/2023", "15/09/2023", "29/12/2023", "26/11/2021", "14/01/2022",
"14/01/2022", "30/06/2022", "30/06/2022", "30/03/2023", "30/03/2023",
"15/09/2023", "15/09/2023", "29/12/2023"), rating = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2", "3"), class = "factor")), row.names = c(NA,
-40L), class = c("tbl_df", "tbl", "data.frame"))
ggplot(df, aes(as.POSIXct(as.Date(value, "%d/%m/%Y")), name, colour = factor(stadio,levels=1:11)))
guides(alpha = "none",
colour = guide_legend(
override.aes = list(size = 4)))
geom_line(aes(size=rating, alpha=rating))
scale_alpha_manual(values=c(.5,1))
labs(colour="(Actual/Baseline)", x = "", y = "")
scale_colour_manual(breaks = c("1", "3", "7","9","11"), values = brewer.pal(11,"PiYG"))
scale_size_manual(breaks = levels(df$rating), values = as.integer(levels(df$rating)), guide = "none")
uj5u.com熱心網友回復:
從幫助:
“值:一組美學值,用于將資料值映射到。這些值將按順序(通常按字母順序)與比例的限制匹配,或者如果提供了中斷。如果這是一個命名向量,那么這些值將是而是根據名稱匹配。不匹配的資料值將被賦予 na.value。”
所以在這一行:
scale_colour_manual(breaks = c("1", "3", "7","9","11"), values = brewer.pal(11,"PiYG"))
您定義了 5 個中斷,因此 ggplot 僅使用調色板的前 5 種顏色。如果洗掉 "breaks = c("1", "3", "7","9","11")" 繪圖是正確的,但圖例不顯示所有 11 個值。
要繪制所有 11 種顏色并使圖例僅顯示圖例中的一半值,您需要定義一個函式來選擇要顯示的值。
oddones <- function(x) {
ifelse(as.integer(x)%%2, x, "")
}
ggplot(df, aes(as.POSIXct(as.Date(value, "%d/%m/%Y")), name, colour = factor(stadio,levels=1:11)))
guides(alpha = "none", colour = guide_legend(override.aes = list(size = 4)))
geom_line(aes(size=rating, alpha=rating))
scale_alpha_manual(values=c(.5,1))
labs(colour="(Actual/Baseline)", x = "", y = "")
scale_colour_manual(breaks = oddones, values = brewer.pal(11,"PiYG"))
scale_size_manual(breaks = levels(df$rating), values = as.integer(levels(df$rating)), guide = "none")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/371964.html
上一篇:使用向量作為密度的三元圖
