1

Solved: I want to color my bars in the plot by which group they are in.

Also: If possible, I would also like to have the colors gradient/fading by the value on the y axis, devided into intervalls of for example [-inf,0], [0,3], [3,10], [10, inf]

My data contains one column with the stop number (which I want to group by), one column with times (positive and negative relative to a time table) and trip ids.

structure(list(stop_sequence = c(1L, 13L, 18L, 6L, 1L, 13L, 18L, 
6L, 1L, 13L, 18L, 6L, 1L, 13L, 18L, 6L, 13L, 18L, 1L, 13L, 18L, 
6L), on_time = c(5, 231, 84, 126, 54, 274, 132, 159, 17, 7, 0, 
6, -4, -20, 0, 28, 110, 0, 17, 199, 177, 197), trip_id = c(55700000048910944, 
55700000048910944, 55700000048910944, 55700000048910944, 55700000048910968, 
55700000048910968, 55700000048910968, 55700000048910968, 55700000048910992, 
55700000048910992, 55700000048910992, 55700000048910992, 55700000048911024, 
55700000048911024, 55700000048911024, 55700000048911024, 55700000048911176, 
55700000048911176, 55700000048911256, 55700000048911256, 55700000048911256, 
55700000048911256)), class = "data.frame", row.names = c(289L, 
290L, 291L, 292L, 293L, 294L, 295L, 296L, 297L, 298L, 299L, 300L, 
301L, 302L, 303L, 304L, 326L, 327L, 337L, 338L, 339L, 340L))

I have tried several thing, for example this below, but it gives me the error "Error: Continuous value supplied to discrete scale"

ggplot(data = tb_dir1, aes(x=stop_sequence, y=on_time, group = stop_sequence, fill=trip_id), names.arg = tb_dir1$stop_sequence) +
  geom_bar(stat="identity", position = "dodge2", width = 5) +
  scale_fill_manual(palette="Drak2")+
  geom_text(aes(label=on_time),  color="black",
           position = position_dodge2(5),vjust = -.25, size=2)+
  scale_x_discrete(name = "Stop sequence",
                   limits=x_labels)+
  theme_minimal()

With this code (without the scale_fill_manual)

ggplot(data = tb_dir1, aes(x=stop_sequence, y=on_time, group = stop_sequence, fill=trip_id), names.arg = tb_dir1$stop_sequence) +
  geom_bar(stat="identity", position = "dodge2", width = 5) +
  geom_text(aes(label=on_time),  color="black",
           position = position_dodge2(5),vjust = -.25, size=2)+
  scale_x_discrete(name = "Stop sequence",
                   limits=x_labels)+
  theme_minimal()

A picture of the plot so far enter image description here

Charlotte
  • 23
  • 5
  • 5
    Use `factor(trip_id)` to transform the numeric variable into a factor and then you can apply a discrete fill scale.I believe you want to use `scale_fill_brewer(palette = "Dark2")` – kath Feb 22 '19 at 10:08
  • @kath, yes it worked! Thank you! – Charlotte Feb 22 '19 at 10:16
  • 2
    Possible duplicate of ["Error: Continuous value supplied to discrete scale" in default data set example mtcars and ggplot2](https://stackoverflow.com/questions/43359050/error-continuous-value-supplied-to-discrete-scale-in-default-data-set-example) – kath Feb 22 '19 at 10:23
  • @kath, I would also like to know if it is possible to have the colors in each group fading by the values in the y-axis, devided into intervalls of for example [-inf,0], [0,3], [3,10], [10, inf] – Charlotte Feb 22 '19 at 10:31
  • 1
    @Charlotte: https://stackoverflow.com/a/52886298/786542 – Tung Feb 22 '19 at 12:13

0 Answers0