【问题标题】:R: changing the color of lines in RR:改变R中线条的颜色
【发布时间】:2021-04-15 21:48:29
【问题描述】:

我正在使用 R。我正在尝试更改图表上不同线条的颜色。我想出了如何在下面的代码中使每一行变成不同的颜色:

library(xts)
library(ggplot2)
library(dplyr)
library(plotly)
library(lubridate)

#time series 1
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")

property_damages_in_dollars <- rnorm(731,100,10)

final_data <- data.frame(date_decision_made, property_damages_in_dollars)

final_data %>%
  mutate(date_decision_made = as.Date(date_decision_made)) %>%
  add_count(week = format(date_decision_made, "%W-%y"))

final_data$class = "time_series_1"


#time series 2
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")

date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d")

property_damages_in_dollars <- rnorm(731,10,10)

final_data_2 <- data.frame(date_decision_made, property_damages_in_dollars)

final_data_2 %>%
  mutate(date_decision_made = as.Date(date_decision_made)) %>%
  add_count(week = format(date_decision_made, "%W-%y"))

final_data_2$class = "time_series_2"


#time series 3
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")

date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d")

property_damages_in_dollars <- rnorm(731,50,50)

final_data_3<- data.frame(date_decision_made, property_damages_in_dollars)

final_data_3 %>%
  mutate(date_decision_made = as.Date(date_decision_made)) %>%
  add_count(week = format(date_decision_made, "%W-%y"))

final_data_3$class = "time_series_3"



#time series 4
date_decision_made = seq(as.Date("2014/1/1"), as.Date("2016/1/1"),by="day")

date_decision_made <- format(as.Date(date_decision_made), "%Y/%m/%d")

property_damages_in_dollars <- rnorm(731,50,50)

final_data_4<- data.frame(date_decision_made, property_damages_in_dollars)

final_data_4 %>%
  mutate(date_decision_made = as.Date(date_decision_made)) %>%
  add_count(week = format(date_decision_made, "%W-%y"))

final_data_4$class = "time_series_4"

data = rbind(final_data, final_data_2, final_data_3, final_data_4)

a = data  %>% 
    
    group_by(class) %>% 
    plot_ly(x = ~ (date_decision_made)) %>% 
    add_lines(y = ~ property_damages_in_dollars, 
              color = ~ factor(class)
    ) 

有没有办法让 (time series 1, time series 2) 颜色和 (time series 3, time series 4) 颜色一样?

我尝试了以下代码,但现在只有 2 个时间序列而不是 4 个:

final_data_4$class = "red"
final_data_3$class = "red"
final_data_2$class = "blue"
final_data$class = "blue"

data = rbind(final_data, final_data_2, final_data_3, final_data_4)

a = data  %>% 
    
    group_by(class) %>% 
    plot_ly(x = ~ (date_decision_made)) %>% 
    add_lines(y = ~ property_damages_in_dollars, 
              color = ~ factor(class)
    ) 

谁能告诉我我做错了什么?我希望有 4 个时间序列,前两个是一种颜色,另外两个是相同的颜色。

谢谢

【问题讨论】:

    标签: r dplyr time-series plotly data-visualization


    【解决方案1】:
    library(xts)
    
    dat <- matrix(rnorm(400), ncol = 4)
    tindex <- seq.Date(from = Sys.Date()-99,
                       to = Sys.Date(),
                       by = 'days')
    
    dat_xts <- xts(dat, order.by = tindex)
    names(dat_xts) <- paste0('ts', 1:4)
    
    plot.xts(dat_xts, col = c('red', 'red', 'blue', 'blue'),
             legend.loc = 'bottomright')
    
    

    使用ggplot:

    library(ggplot2)
    
    autoplot(dat_xts, facets = FALSE) + scale_color_manual(values= c('red', 'red', 'blue', 'blue'))
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-29
    • 2018-06-11
    • 2014-11-17
    • 1970-01-01
    • 2017-09-29
    • 2014-05-11
    • 1970-01-01
    • 2021-11-23
    相关资源
    最近更新 更多