【问题标题】:Line thickness ggplot - looks discrete线条粗细 ggplot - 看起来离散
【发布时间】:2020-03-24 15:25:21
【问题描述】:

我想绘制两条不同粗细的线。 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"] &lt;- 2 不会明显影响任何事情。

【问题讨论】:

  • 那么这里想要的结果到底是什么?我不确定我是否理解您的问题。

标签: r ggplot2 line thickness


【解决方案1】:

一种方法是使用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"))

【讨论】:

  • 谢谢,它可以工作,但会生成消息:Scale for 'size' is already present. Adding another scale for 'size', which will replace the existing scale.
  • @user1700890 你的情节中可能有两个不同的scale_size_... 调用
  • @Tjebo,谢谢,是的,我有两个scale_size_continuous(guide = FALSE) 来摆脱传说(在我的原始帖子中)和scale_size_identity(guide=FALSE) 来控制厚度(来自答案)。我需要他们两个。我该怎么办?
  • @user1700890 去掉连续的,看看会发生什么
  • @Tjebo,你是对的。我删除了scale_size_continuous(guide = FALSE),没有任何改变。我有厚度控制,没有图例
猜你喜欢
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 2022-12-16
相关资源
最近更新 更多