geom_col以错误的方式绘制数据

时间:2017-07-04 11:34:41

标签: r ggplot2

我的ggplot存在问题。 数据:

df <- structure(list(date = structure(c(1499050800, 1499054400, 1499058000, 
1499061600, 1499065200, 1499068800, 1499072400, 1499076000, 1499079600, 
1499083200, 1499086800, 1499090400, 1499094000, 1499097600, 1499101200, 
1499104800, 1499108400, 1499112000, 1499115600, 1499119200, 1499122800, 
1499126400, 1499130000, 1499133600, 1499137200, 1499140800, 1499144400, 
1499144400, 1499148000, 1499151600, 1499155200, 1499158800, 1499162400, 
1499166000), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
    output = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0)), .Names = c("date", 
"output"), row.names = c(NA, -34L), class = c("tbl_df", "tbl", 
"data.frame"))

情节:

ggplot()+
  geom_col(data = df, aes(x = date, y = output))

值5表示为8.

1 个答案:

答案 0 :(得分:2)

因为您同时有两个观察结果(5 + 4 = 9)。试试这个:

ggplot(data = df, aes(x = date, y = output))+
  geom_bar(stat="identity", position = "dodge")

编辑或者根据评论中的@MbrMbr建议:

ggplot(data = df, aes(x = date, y = output))+
  geom_col(position = "dodge")