簡體   English   中英

用 ggplot2 package 修改 plot

[英]Modify plot with ggplot2 package

這是我的數據集的一個小例子。這個集包含大約 52 周的每周數據。您可以使用以下代碼查看數據:

# CODE
 #Data

ARTIFICIALDATA<-dput(structure(list(week = c(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, 43, 44, 
45, 46, 47, 48, 49, 50, 51, 52), `2019 Series_1` = c(534.771929824561, 
350.385964912281, 644.736842105263, 366.561403508772, 455.649122807018, 
533.614035087719, 829.964912280702, 466.035087719298, 304.421052631579, 
549.473684210526, 649.719298245614, 537.964912280702, 484.982456140351, 
785.929824561404, 576.736842105263, 685.508771929824, 514.842105263158, 
464.491228070175, 608.245614035088, 756.701754385965, 431.859649122807, 
524.315789473684, 739.40350877193, 604.736842105263, 669.684210526316, 
570.491228070175, 641.649122807018, 649.298245614035, 664.210526315789, 
530.385964912281, 754.315789473684, 646.80701754386, 764.070175438596, 
421.333333333333, 470.842105263158, 774.245614035088, 752.842105263158, 
575.368421052632, 538.315789473684, 735.578947368421, 522, 862.561403508772, 
496.526315789474, 710.631578947368, 584.456140350877, 843.19298245614, 
563.473684210526, 568.456140350877, 625.368421052632, 768.912280701754, 
679.824561403509, 642.526315789474), `2020 Series_1` = c(294.350877192983, 
239.824561403509, 709.614035087719, 569.824561403509, 489.438596491228, 
561.964912280702, 808.456140350877, 545.157894736842, 589.649122807018, 
500.877192982456, 584.421052631579, 524.771929824561, 367.438596491228, 
275.228070175439, 166.736842105263, 58.2456140350878, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA)), row.names = c(NA, -52L), class = c("tbl_df", "tbl", 
"data.frame")))

所以下一個陡峭的是 plot 這個數據和 ggplot2。 所以你可以在下面看到我的 plot

library(tidyverse)
library(ggplot2)

    ARTIFICIALDATA_rec <- ARTIFICIALDATA %>% 
      gather(key = Year_indicator, value = time_series_value, -1)

    your_plot <- ggplot(data = ARTIFICIALDATA_rec, aes(x = week, y = time_series_value, group = Year_indicator)) +
      geom_line(aes(color = Year_indicator)) +
      scale_x_continuous(name = "Week of the year", limits=c(0, 52), breaks=seq(0,52,2))

在此處輸入圖像描述

所以這看起來像我的 plot,但這里缺少一些東西。即我想要綠色線更改虛線(大小 = 1 線型 = 2)。那么任何人都可以幫助我如何修改它?

只需 map Year_indicator也在sizelinetype上。 然后可以通過scale_xxxx_manual設置線條的大小和線型。 嘗試這個:

library(tidyverse)
library(ggplot2)

ARTIFICIALDATA_rec <- ARTIFICIALDATA %>% 
  gather(key = Year_indicator, value = time_series_value, -1)

your_plot <- ggplot(data = ARTIFICIALDATA_rec, aes(x = week, y = time_series_value, group = Year_indicator)) +
  geom_line(aes(color = Year_indicator, linetype = Year_indicator, size = Year_indicator)) +
  scale_x_continuous(name = "Week of the year", limits=c(0, 52), breaks=seq(0,52,2)) +
  scale_linetype_manual(values = c(1, 2)) +
  scale_size_manual(values = c(.5, 1))
your_plot
#> Warning: Removed 36 row(s) containing missing values (geom_path).

代表 package (v0.3.0) 於 2020 年 4 月 4 日創建

我們到了:

library(tidyverse)

    ARTIFICIALDATA_rec <- ARTIFICIALDATA %>% 
      gather(key = Year_indicator, value = time_series_value, -1)

your_plot <- ggplot(data = ARTIFICIALDATA_rec, aes(x = week, y = time_series_value, group = Year_indicator)) +
    geom_line(aes(linetype = Year_indicator, color = Year_indicator, size = Year_indicator)) +
    scale_linetype_manual(values = c("solid", "dashed")) +

scale_x_continuous(name = "Week of the year", limits=c(0, 52), breaks=seq(0,52,2)) + scale_color_manual(values = c('red','green')) + scale_size_manual(值 = c(1,1)) + theme_bw()

這是 plot:

在此處輸入圖像描述

暫無
暫無

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

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