在融化的时间序列中没有得到 jco 调色板

问题描述 投票:0回答:1

我正在尝试绘制一个包含 3 个变量的时间序列。为了绘制它,我正在使用 melt 函数并使用

ggsci
的 jco 调色板。还从图例中删除可变标题部分。我正在使用以下代码来绘制它,但是,我仍然获得默认颜色。

    mdf<-melt(df,id.vars="DATE")

g <- ggplot(data=mdf, aes(x=DATE, y=value, group = variable,colour=variable))+
  geom_line(linewidth=0.05)+
  scale_fill_jco(name="", #keeping title of legend blank
  breaks=c("V1", "V2","V3"),
  labels=c("First", "Second", "third"))+
  scale_x_date(breaks = date_breaks("1 years"), labels = date_format("%Y"),expand = c(0, 0)) +
  
  labs(y= "Mean precipitation(mm)", x = "Year")
g

数据:

df <- data.frame(
  V1 = c(
    0.299709245928855, 0.264898804581009, 0.0514762211257873, 0.172528797343094,
    0.286692151182817, 0.381306320393445, 0.209173997269107, 0.0271853802231936,
    0.0773644637950056, 0.100304007028023
  ),
  V2 = c(
    0.0656779733249077, 0.252630359185351, 0.35086728952018, 0.337186818513385,
    0.495255439470686, 0.913847169790918, 0.358945167549019, 0.849737426926668,
    1.0342232107347, 1.2839730181513
  ),
  V3 = c(
    0.11712306002031, 0.326976704286807, 0.44465221799287, 0.428882415544564,
    0.606684040009608, 1.13251218973981, 0.454452665889777, 1.05901363917576,
    1.29908926601187, 1.63183343004054
  ),
  DATE = as.Date(c(
    "1971-01-01", "1971-01-02", "1971-01-03", "1971-01-04", "1971-01-05",
    "1971-01-06", "1971-01-07", "1971-01-08", "1971-01-09", "1971-01-10"
  ))
)
r color-palette
1个回答
0
投票

您应该使用

scale_color_jco
,因为您正在将
color
映射到您的线条美学中。您也可以使用
pivot_longer
tidyr
到更长的格式,如下所示:

library(tidyr)
library(ggplot2)
library(ggsci)
library(scales)
df |>
  pivot_longer(cols = V1:V3) %>%
  ggplot(aes(x=DATE, y=value, group = name, colour=name))+
  geom_line(linewidth=0.05)+
  scale_color_jco(name="", #keeping title of legend blank
                 breaks=c("V1", "V2","V3"),
                 labels=c("First", "Second", "third"))+
  scale_x_date(breaks = date_breaks("1 years"), labels = date_format("%Y"),expand = c(0, 0)) +
  labs(y= "Mean precipitation(mm)", x = "Year")


不变

linewidth

library(tidyr)
library(ggplot2)
library(ggsci)
library(scales)
df |>
  pivot_longer(cols = V1:V3) %>%
  ggplot(aes(x=DATE, y=value, group = name, colour=name))+
  geom_line()+
  scale_color_jco(name="", #keeping title of legend blank
                 breaks=c("V1", "V2","V3"),
                 labels=c("First", "Second", "third"))+
  scale_x_date(breaks = date_breaks("1 years"), labels = date_format("%Y"),expand = c(0, 0)) +
  labs(y= "Mean precipitation(mm)", x = "Year")

创建于 2023-03-22 与 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.