簡體   English   中英

如何在 ggplot 中使用多個組圖層制作多條線?

[英]How can I make several lines in ggplot with several group layers?

我根據體育博彩和賭場將我的地塊分為 2 個。 如何也可以將線分成幾條線(不同顏色)以顯示不同的市場? 我嘗試在 ggplot 函數的末尾使用 fill=market,但它沒有幫助。

library(ggplot2)
data<-structure(list(wday = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
4L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 
7L, 7L, 7L), .Label = c("Monday", "Tuesday", "Wednesday", "Thursday", 
"Friday", "Saturday", "Sunday"), class = "factor"), market = c("France", 
"France", "Germany", "Germany", "Poland", "Poland", "France", 
"France", "Germany", "Germany", "Poland", "Poland", "France", 
"France", "Germany", "Germany", "Poland", "Poland", "France", 
"France", "Germany", "Germany", "Poland", "Poland", "France", 
"France", "Germany", "Germany", "Poland", "Poland", "France", 
"France", "Germany", "Germany", "Poland", "Poland", "France", 
"France", "Germany", "Germany", "Poland", "Poland"), product_preference = c("Casino", 
"Sportsbook", "Casino", "Sportsbook", "Casino", "Sportsbook", 
"Casino", "Sportsbook", "Casino", "Sportsbook", "Casino", "Sportsbook", 
"Casino", "Sportsbook", "Casino", "Sportsbook", "Casino", "Sportsbook", 
"Casino", "Sportsbook", "Casino", "Sportsbook", "Casino", "Sportsbook", 
"Casino", "Sportsbook", "Casino", "Sportsbook", "Casino", "Sportsbook", 
"Casino", "Sportsbook", "Casino", "Sportsbook", "Casino", "Sportsbook", 
"Casino", "Sportsbook", "Casino", "Sportsbook", "Casino", "Sportsbook"
), ggr = c(3349.80897892753, 161.917715712988, 17700.4568364611, 
-123.342131455399, 17208.7731385281, 3128.51277864992, 2877.17330617787, 
28.5162781278127, 13453.7092912371, -82.8980672268908, 13611.1197727273, 
9910.32070866143, 3939.20578803854, 126.311590466926, 19097.2664228723, 
-94.5491666666667, 16706.9427008929, 2636.63687707641, 3393.43150322119, 
176.953280238925, 23414.9515950069, -72.4428986866791, 16140.8680085653, 
5618.00758333333, 3007.18322084806, 69.4383454281568, 18018.1755748663, 
-77.87698, 19889.0339183673, 5561.69038585209, 4205.12735472371, 
-16.0552268431002, 17166.1121932115, -117.149356025759, 18527.8546597938, 
6806.36808346213, 3446.70375835385, 56.6674850849013, 18026.2400535475, 
-67.3431629701062, 13641.4965135699, 11470.3083969466)), row.names = c(NA, 
-42L), groups = structure(list(wday = structure(c(1L, 1L, 1L, 
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 
7L, 7L), .Label = c("Monday", "Tuesday", "Wednesday", "Thursday", 
"Friday", "Saturday", "Sunday"), class = "factor"), market = c("France", 
"Germany", "Poland", "France", "Germany", "Poland", "France", 
"Germany", "Poland", "France", "Germany", "Poland", "France", 
"Germany", "Poland", "France", "Germany", "Poland", "France", 
"Germany", "Poland"), .rows = structure(list(1:2, 3:4, 5:6, 7:8, 
    9:10, 11:12, 13:14, 15:16, 17:18, 19:20, 21:22, 23:24, 25:26, 
    27:28, 29:30, 31:32, 33:34, 35:36, 37:38, 39:40, 41:42), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -21L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), na.action = structure(43:46, .Names = c("43", 
"44", "45", "46"), class = "omit"), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"))

ggplot() +
  geom_line(data = data,aes(x = wday, y = ggr,group = product_preference))+
  facet_grid(.~product_preference,scales="free")

您可以通過添加color = <grouping variable>來定義線條的color = <grouping variable> 此外,您已經在product_preference上做了一個分面網格,所以似乎沒有必要定義group = product_preference

試試這個:

ggplot() +
   geom_line(data = data,aes(x = wday, y = ggr,color = market, group = market)) +
   facet_grid(.~product_preference,scales="free")

我不確定這是你想要的,但我會用'geom_col'替換geom_line 'geom_col' 也需要 'fill' 屬性,而 'geom_line' 需要 'color'。

所以我的建議如下:

ggplot() +
  geom_col(data = data,aes(x = wday, y = ggr, group = product_preference, fill = market))+
  facet_grid(.~product_preference,scales="free")

這導致以下情節:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM