【发布时间】: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 有效,但线的颜色会根据最后记录的感觉从点到点发生变化。我可以把颜色改成黑色吗?