多个折线图在R中的一个绘图中

jaxagkaj  于 2023-03-15  发布在  其他
关注(0)|答案(1)|浏览(245)

下面是我在R中的 Dataframe 示例:

structure(list(Series = c("A", "B", "A", "B", "A", "B", "A", 
"B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B"
), date = structure(c(19415, 19415, 19415, 19415, 19415, 19415,
19415, 19415, 19415, 19415, 19416, 19416, 19416, 19416, 19416,
19416, 19416, 19416, 19416, 19416), class = "Date"), ccy = c("CHF",
"CHF", "EUR", "EUR", "GBP", "GBP", "JPY", "JPY", "USD", "USD",
"CHF", "CHF", "EUR", "EUR", "GBP", "GBP", "JPY", "JPY", "USD",
"USD"), exp = c(-25.0260386883321, -13.4876728854438, 29.9242170197296,
28.1863060040971, -21.1816468152161, -17.6427844100702, -125.811601419233, 
-116.187338952966, -1574.53746806177, -944.410592575117, 13.687475030189,
5.55257290367663, 22.1373554559174, 9.839306286417, 9.60824459042358,
8.43043547492962, 75.9856738242059, 19.8785641759769, -2531.47779912341,
-313.689149325403)), row.names = c(NA, 20L), class = "data.frame")

对于列ccy中的每个唯一值,我想创建一个简单的折线图,其中包含两个系列(列series中的A和B),值来自列dateexp
每个图表的格式都是一样的,线条有两种不同的颜色,标题标识ccy所指的内容,输出将是一个方形折线图网格。
最好的解决办法是什么?

8hhllhi2

8hhllhi21#

您可以使用ggplot来完成此操作。

df = structure(list(Series = c("A", "B", "A", "B", "A", "B", "A", 
                          "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B", "A", "B"
), date = structure(c(19415, 19415, 19415, 19415, 19415, 19415,
                      19415, 19415, 19415, 19415, 19416, 19416, 19416, 19416, 19416,
                      19416, 19416, 19416, 19416, 19416), class = "Date"), ccy = c("CHF",
                                                                                   "CHF", "EUR", "EUR", "GBP", "GBP", "JPY", "JPY", "USD", "USD",
                                                                                   "CHF", "CHF", "EUR", "EUR", "GBP", "GBP", "JPY", "JPY", "USD",
                                                                                   "USD"), exp = c(-25.0260386883321, -25.0260386883321, 29.9242170197296,
                                                                                                   29.9242170197296, -21.1816468152161, -21.1816468152161, -125.811601419233,
                                                                                                   -125.811601419233, -1574.53746806177, -1574.53746806177, 13.687475030189, 
                                                                                                   13.687475030189, 22.1373554559174, 22.1373554559174, 9.60824459042358,
                                                                                                   9.60824459042358, 75.9856738242059, 75.9856738242059, -2531.47779912341,
                                                                                                   -2531.47779912341)), class = "data.frame", row.names = c(NA,
                                                                                                                                                            -20L))
library(ggplot2)
library(dplyr)
library(lubridate)

df |>
  mutate(date = lubridate::ymd(date)) |>
  ggplot(aes(x=date,y=exp,color=Series)) +
  geom_line() +
  facet_wrap(~ccy)

请注意,您为A和B指定的值相同,因此这两条线彼此重叠。

相关问题