When using "geom_histogram" there is error "unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0". Why

RGgplot2

R Problem Overview


When using geom_histogram there is error

unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0. 

Why?

p4<-ggplot(BCIcor,aes(x=cor))+geom_histogram(binwidth = 0.2)    

This showed a black bar chart. However, when I wanted to group the data by p to make the plot colorful, I added fill=p,

p4<-ggplot(BCIcor,aes(x=cor,fill=p))+geom_histogram(binwidth = 0.2)

The I got the following:

error :"unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0".

What's wrong??

The data frame is:

  cor        pvalue   p 

1  0.87882370 0.049710 2       
2 -0.83041880 0.081660 1         
3 -0.12989750 0.835100 1        
4 -0.75309860 0.141700 1        
5 -0.88553450 0.045680 2

R Solutions


Solution 1 - R

You got this error because p values are numeric in your data frame but in this case for the fill= you need discrete values as bars are stacked and will be colored according to p. Just use as.factor() around p.

ggplot(BCIcor,aes(x=cor,fill=as.factor(p)))+geom_histogram(binwidth = 0.2)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJadaView Question on Stackoverflow
Solution 1 - RDidzis ElfertsView Answer on Stackoverflow