【问题标题】:Adapt geom_line() order by data to the ggplot function按数据调整 geom_line() 顺序到 ggplot 函数
【发布时间】:2020-01-29 09:38:31
【问题描述】:

我正在尝试使用 ggplot 绘制图表。在这张图表中,我试图给出与价格相关的消费者情绪密度(可分为正面、负面和中性)的概念。现在我想尝试使用命令 geom_line() 按时间顺序在它们之间连接所有这些点(负面感觉为红色,正面感觉为绿色,中性感觉为灰色)。

ggplot(test, aes(x=date, y=price, color=as.factor(tot_sentiment))) +
      geom_point(size= abs(test$Sentiment)) + 
      scale_shape_manual(values=c(19, 19, 19)) + 
      scale_color_manual(values=c('#FF0C00','#999999', '#04C200')) +
      theme(legend.position="top") +
      scale_x_date(date_labels="%d %b %y",date_breaks  ="1 week") +
      xlab("Time") + ylab("Price") + labs(color='Legenda:') + geom_line()

不幸的是,它只连接了相同情绪的点,而不是我希望的按时间顺序排列的所有点。我也试过+geom_line(aes(group = tot_sentiment)) 没有结果

预期输出:

我的数据:

structure(list(date = structure(c(17511, 17512, 17513, 17514, 
17515, 17516, 17517, 17518, 17519, 17520, 17521, 17522, 17523, 
17524, 17525, 17526, 17527, 17528), class = "Date"), price = c(16936.8, 
17415.4, 16408.2, 16564, 17706.9, 19497.4, 19140.8, 19114.2, 
17776.7, 16624.6, 15802.9, 13831.8, 14699.2, 13925.8, 14026.6, 
16099.8, 15838.5, 14606.5), Sentiment = c(0L, -2L, -13L, 4L, 
-6L, 1L, -2L, -1L, -3L, 2L, -4L, -6L, 0L, 4L, 1L, 6L, 5L, 7L), 
    tot_sentiment = c("neutral", "negative", "negative", "positive", 
    "negative", "positive", "negative", "negative", "negative", 
    "positive", "negative", "negative", "neutral", "positive", 
    "positive", "positive", "positive", "positive")), row.names = c(NA, 
-18L), .internal.selfref = <pointer: 0x000002388fc21ef0>, class = c("tbl_df", 
"tbl", "data.frame"))

【问题讨论】:

  • 您能否提供一个可重现的数据集示例?看这里:stackoverflow.com/questions/5963269/…
  • 尝试将group=1 添加到您的aes()
  • @Mosquite group = 1 有效,但线的颜色会根据最后记录的感觉从点到点发生变化。我可以把颜色改成黑色吗?

标签: r ggplot2 plot


【解决方案1】:

ggplot() 调用中设置的美学由后续层继承。如果只需要点图层中的颜色,请在点图层中指定:

ggplot(test, aes(x=date, y=price)) +
      geom_point(aes(color=as.factor(tot_sentiment), size = abs(Sentiment))) + 
      scale_shape_manual(values=c(19, 19, 19)) + 
      scale_color_manual(values=c('#FF0C00','#999999', '#04C200')) +
      theme(legend.position="top") +
      scale_x_date(date_labels="%d %b %y",date_breaks  ="1 week") +
      xlab("Time") + ylab("Price") + labs(color='Legenda:') +
      geom_line()

我将您的 size 规范移至 aes(并删除了 $,这是一种很好的做法)。您可能还想将geom_line放在geom_point 层之前,以便点位于线的顶部。

【讨论】:

    【解决方案2】:

    您可以按照@Mosquite 的建议传递group = 1,并将color = "black" 设置为geom_line

    ggplot(test, aes(x=date, y=price, group = 1, color=as.factor(tot_sentiment))) +
      geom_line(color = "black")+
      geom_point(size= abs(test$Sentiment)) + 
      scale_shape_manual(values=c(19, 19, 19)) + 
      scale_color_manual(values=c('#FF0C00','#999999', '#04C200')) +
      theme(legend.position="top") +
      scale_x_date(date_labels="%d %b %y",date_breaks  ="1 week") +
      xlab("Time") + ylab("Price") + labs(color='Legenda:')
    

    【讨论】:

      猜你喜欢
      • 2022-08-15
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多