简体   繁体   English

"如何在具有离散x轴的ggplot中制作折线图?"

[英]How to make a line graph in ggplot with discrete x-axis?

So I have a table as follows:所以我有一个如下表:

data <- structure(list(Variable = c("Content", "Content", "Content", 
"Content", "Content", "Content"), content_type = c("LIVE", 
"LIVE", "LIVE", "VOD", "VOD", "VOD"), CURRENT_STATUS = c("ACTIVE", 
"CANCELLED", "EXPIRED", "ACTIVE", "CANCELLED", "EXPIRED"), `1 Day` = c(0.768763, 0.734808, 0.714794, 1.200606, 0.766533, 
0.765004), `7 Day` = c(1.181393, 0.989723, 1.035509, 
3.793286, 1.734361, 2.112217), `14 Day` = c(1.431612, 
1.123995, 1.206466, 5.428656, 2.226542, 2.868766), 
    `21 Day` = c(1.609405, 1.189455, 1.313989, 6.671368, 2.485925, 
    3.381902), `28 Day` = c(1.785089, 1.237489, 1.415171, 
    7.717914, 2.678954, 3.795692), `35 Day` = c(1.945274, 
    1.274576, 1.488107, 8.644254, 2.815237, 4.101302), `42 Day` = c(2.095211, 1.306524, 1.540499, 9.465924, 2.932306, 
    4.320128), `49 Day` = c(2.286882, 1.344057, 1.616501, 
    10.245316, 3.04714, 4.532644), `56 Day` = c(2.426061, 
    1.369303, 1.666257, 10.937274, 3.169794, 4.709551), `90 Day` = c(2.974361, 1.435433, 1.812601, 13.656243, 
    3.419348, 5.263874)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

I think you just need to pivot your data, order the days, and assign a grouping in the aesthetic:我认为您只需要旋转您的数据,对日期进行排序,并在美学中分配一个分组:

library(tidyverse)

data_long <- data %>%
  pivot_longer(cols = ends_with("Day"),
           names_to = "day",
           values_to = "count") %>%
  mutate(day = as_factor(day)) 

  ggplot(data = data_long, aes(x = day, y = count, group = CURRENT_STATUS)) + 
  geom_line(aes(color = CURRENT_STATUS, linetype = CURRENT_STATUS)) +
  facet_wrap(~content_type)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM