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.
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.
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.
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.
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.
You can use stack
to transform the data frame:
library(ggplot2)
ggplot(stack(df), aes(x = ind, y = values)) +
geom_boxplot()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With