Tom Enns
Tom Enns

Reputation: 182

Box Plot In R Shows No Data

I'm trying to create a boxplot in R that shows the number of organic visitors to a website (data pulled from analytics API).

Each

The data is fine, but when I go to create the boxplot, it appears empty with no data.

The data frame is called 'myData'

Here is the code:

 names(myData)
[1] "date"   "visits"
> myData$yearmo <- year(myData$date)*100 + month(myData$date)
>
> boxplot(visits ~ yearmo, data=myData, main="Organic Visits",
+         xlab="Year-Month", ylab="Visits", col= "orange", ylim=c(0,.8), yaxt="n")
>  

Here is the boxplot I'm getting:

enter image description here

Upvotes: 0

Views: 1136

Answers (2)

zero323
zero323

Reputation: 330113

I would guess that your ylim is wrong. You have visits on y-scale and maximum plotted value is set to 0.8 so it is probably outside 1.5 IQR.

Try to remove ylim:

boxplot(
    visits ~ yearmo, data=myData, main="Organic Visits",
    xlab="Year-Month", ylab="Visits", col= "orange", yaxt="n"
)

If problem persists check if your dataframe really contains expected values.

BTW Always try to provide reproducible example. Without it is really really hard to solve your problem.

Upvotes: 1

Jesse Pfammatter
Jesse Pfammatter

Reputation: 11

Boxplots require a categorical variable on the x axis. Your myData$yearmo variable is continuous. Changing the code to something like...

    boxplot(visits ~ as.factor(year), data=myData)

This wont give you exactly the x axis you are looking for, but it will generate a plot and you can figure out how to make the proper categorical variable once you get the boxplot working. Ultimately, the following line of code is suspect for generating the incorrect type of data for boxplots:

    myData$yearmo <- year(myData$date)*100 + month(myData$date)

I'm not sure using year() and month() as functions is correct, or actually what package they are from. What I am sure about is that the resultant object is not categorical. Also, there is no need to put in an argument for ylim at this point, it will just cause problems.

Upvotes: 0

Related Questions