Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a box plot from all columns of data frame with column names on x in ggplot2 [duplicate]

Tags:

r

ggplot2

I have a data frame my.df of the following structure:

   A B  C
1  1 1  2
2  2 3  4
3  3 5  6
4 NA 7  8
5 NA 9 NA

How to build a box plot from it with column names on x axis and all the values on y?

There are many answers like:

ggplot(melt(my.df), aes(variable, value)) + geom_boxplot()

But I don't understand, what I actually should pass as "variable" and "value". I tried x=colnames(my.df)) and this partially works, however I still have no idea what to do with y.

like image 916
Roman Avatar asked Nov 24 '14 16:11

Roman


People also ask

How do you make a boxplot for multiple columns in R?

In case you need to plot a different boxplot for each column of your R dataframe you can use the lapply function and iterate over each column. In this case, we will divide the graphics par in one row and as many columns as the dataset has, but you could plot individual graphs.

How do you make a boxplot from a DataFrame in R?

Data Visualization using R Programming First of all, create a data frame with single numerical column and create the boxplot for that column using boxplot function. Then, create the same boxplot with show. names argument set to TRUE.

What is the X in a box plot?

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 make a boxplot for each feature in the dataset?

boxplot() function. The boxplot() function is used to make a box plot from DataFrame columns. Make a box-and-whisker plot from DataFrame columns, optionally grouped by some other columns. A box plot is a method for graphically depicting groups of numerical data through their quartiles.


1 Answers

You can use stack to transform the data frame:

library(ggplot2)
ggplot(stack(df), aes(x = ind, y = values)) +
  geom_boxplot()

enter image description here

like image 119
Sven Hohenstein Avatar answered Oct 20 '22 12:10

Sven Hohenstein