1

I'm trying to plot a chart with ggplot. In this graph I try to give an idea of ​​the density of consumer sentiment (which can be classified as positive, negative and neutral) in relation to the price. Now I would like to try to connect all these points (red in case of negative feeling, green in case of positive feeling and gray in case of neutral feeling) between them in chronological order with the command 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()

enter image description here

Unfortunately, it connects only the points of the same sentiment and not all the points in chronological order as I wish. I also tried +geom_line(aes(group = tot_sentiment)) with no results

expected output:

enter image description here

MY DATA:

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"))
dc37
  • 15,840
  • 4
  • 15
  • 32
HABLOH
  • 460
  • 2
  • 12
  • Can you provide a reproducible example of your dataset ? see here : https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Jan 29 '20 at 00:41
  • 2
    Try adding `group=1` to your `aes()`. – Mosquite Jan 29 '20 at 00:47
  • @Mosquite group = 1 worked but the color of the line changes from point to point according to the last recorded feeling. Can I change the color to black? – HABLOH Jan 29 '20 at 00:52

2 Answers2

4

Aesthetics set in the ggplot() call are inherited by subsequent layers. If you want the color only in the point layer, specify it in the point layer:

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()

I moved your size specification into aes (and removed the $, as is good practice). You may also want to put the geom_line layer before the geom_point layer so that the points are on top of the line.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
2

You can pass group = 1 as suggested by @Mosquite and set color = "black" to 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:')

enter image description here

dc37
  • 15,840
  • 4
  • 15
  • 32