m_c
m_c

Reputation: 516

geom_col plots data in a wrong way

I have an issue with ggplot. Data:

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

Plot:

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

Value of 5 is plotted as 8.

Upvotes: 3

Views: 1781

Answers (1)

AK88
AK88

Reputation: 3026

Because you have two observations at the same time (5+4=9). Try this:

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

Edit: Or as suggested by @MbrMbr in the comments:

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

Upvotes: 2

Related Questions