ggplot non-integer x-axis behaviour with geom_col baffles me


df0 <- data.frame(index=as.numeric(1:10),count = round(runif(10,1,10)))
ggplot(df0,aes(index,count)) + geom_col()

Okay that is what I expect. Now, let's add 0.5 to each x-axis value so we have a non-integer scale (note I used as.numeric() above to the x-axis is not an integer type.)

df0$index <- df0$index + 0.5
ggplot(df0,aes(index,count)) + geom_col()

Can someone tell me what is going on here. This plot is a mutant! Thanks.

Are you running ggplot 3.3.0? If so, ggplot now tries to determine the orientation from the aesthetic mapping (see "Bi-directional geoms and stats" here) and it looks like it is guessing wrong (or at least differently than you intended) here.

Yes, 3.3.0 with R 3.6.3

Being explicit with x and y `ggplot(df0,aes(x=index,y=count)) + geom_col()' makes no difference.

I guess it must be choosing count as the categories since the values are whole numbers.

I checked the ggplot2 development site and there's already an open issue about this.

Thanks! I looked at the issue and this fixes it.

ggplot(df0,aes(x=index,y=count)) + geom_col(orientation = "x")

I thought it might be a conspiracy to drive me nuts since I spent hours working around it in a way more complicated fashion.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.