Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you draw a boxplot without specifying x axis?

Tags:

r

ggplot2

The base graphics can nicely plot a boxplot using a simple command

data(mtcars)
boxplot(mtcars$mpg)

enter image description here

But qplot requires y axis. How can I achieve with qplot the same like base graphics boxplot and not get this error?

qplot(mtcars$mpg,geom='boxplot')
Error: stat_boxplot requires the following missing aesthetics: y
like image 818
userJT Avatar asked Feb 22 '13 15:02

userJT


People also ask

Can you make box plot with one variable?

Boxplots can be created for individual variables or for variables by group. The format is boxplot(x, data=), where x is a formula and data= denotes the data frame providing the data.

How do you change the x-axis in a boxplot?

To convert this to Horizontal Boxplot add coord_flip() in the boxplot code, and rest remains the same as above. Example: R.

Do box plots have an x-axis?

Box plots are composed of an x-axis and a y-axis. The x-axis assigns one box for each Category or Numeric field variable. The y-axis is used to measure the minimum, first quartile, median, third quartile, and maximum value in a set of numbers. You can use box plots to visualize one or many distributions.

How do you label X and Y axis in a box plot?

The common way to put labels on the axes of a plot is by using the arguments xlab and ylab. As you can see from the image above, the label on the Y axis is place very well and we can keep it. On the other hand, the label on the X axis is drawn right below the stations names and it does not look good.


1 Answers

You have to provide some dummy value to x. theme() elements are used to remove x axis title and ticks.

ggplot(mtcars,aes(x=factor(0),mpg))+geom_boxplot()+
   theme(axis.title.x=element_blank(),
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank())

Or using qplot() function:

qplot(factor(0),mpg,data=mtcars,geom='boxplot')

enter image description here

like image 56
Didzis Elferts Avatar answered Sep 28 '22 02:09

Didzis Elferts