1

我想绘制两条不同粗细的线。size论点有效,但只是部分有效。

library(ggplot2)

forecast <- c(2,2,1,2,2,3,2,3,3,3,3)
actual <- c(2,2,1,2,2,3,2,3,2,2,1)

my_df <- data.frame(forecast = forecast, actual = actual)
my_df$seq_order <- as.factor(1:NROW(my_df))
my_df <-gather(my_df, "line_type", "value", -seq_order)

my_df$line_size <- 1
my_df[my_df$line_type =="actual", "line_size"] <- 5

ggplot(data=my_df, aes(x=seq_order, y = value,
        colour = line_type, group=line_type, size= line_size))+geom_line()+
  scale_size_continuous(guide = FALSE)+
  scale_color_manual(values = c("forecast" = "blue" ,"actual" = "green"))

这是它生成的内容:

阴谋

但是,更改my_df[my_df$line_type =="actual", "line_size"] <- 2不会明显影响任何事情。

4

1 回答 1

2

一种方法是使用scale_size_identity.

ggplot(data=my_df, aes(x=seq_order, y = value,
        colour = line_type, group=line_type, size= line_size))+geom_line()+ 
  scale_size_identity(guide=FALSE) +
  scale_color_manual(values = c("forecast" = "blue" ,"actual" = "green"))
于 2020-03-24T15:39:25.840 回答