按组ggplot条形图颜色

时间:2019-02-22 09:48:23

标签: r ggplot2 colors bar-chart

已解决:我想按其所属的组为图中的条形着色。

也:如果可能的话,我还想通过y轴上的值来使颜色渐变/褪色,划分为例如[-inf,0],[0,3],[ 3,10],[10,inf]

我的数据包含一列,其中包含停靠站编号(我想对其进行分组),一列中包含时间(相对于时间表为正数和负数)和旅行ID。

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

我已经尝试了几件事,例如下面的例子,但是它给我的错误是“错误:连续值提供给离散刻度”

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

使用此代码(无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()

到目前为止的情节图片 enter image description here

0 个答案:

没有答案