0

My aim is to make a plot like that:

enter image description here

I don't want relative imporantces, I just want to plot each column in my dataframe as a boxplot and sort them by median. I don't have groups or anything else I want to plot by. It works with the normal boxplot() function. But how can I do that with ggplot()? And how is it possible to sort the boxplot by median if I do not have anything else to reorder by?

I can not give you my dataset, because it is not availble to the public. The dataframe is like the iris dataset but without the column "Species" etc. I only have the SepalLegthCm, SepalWidthcm etc.: enter image description here

1 Answer 1

1

Reshape your data to long format and then recode your "grouping" variable to a factor variable based on the median. This second step of fct_reorder could be done either within the ggplot call or before via a mutate:

library(tidyverse) iris |> select(-Species) |> pivot_longer(cols = everything()) |> #mutate(name = fct_reorder(name, value, .fun = median, .desc =TRUE)) |> ggplot() + geom_boxplot(aes(x = fct_reorder(name, value, .fun = median, .desc = TRUE), y = value)) 

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

That worked, thank you so much. Do you know as well how to color now by self defined groups? I have 35 columns and want to make 3 groups and give every group a color. For example with the iris dataset: Sepal Length = orange, Petal Length and Width = green and SepalWidth = red.
Create a new variable in this reshaped dataset where you combine the 35 "names" into 3 colour groups and refer to that color column when adding a colour to your plot. There are countless websites/tutorials on how to colour a ggplot.
Somehow I can't do it. I know how to color the ggplot(), but it does not work to add a variable with the desired groups. I wanted to do it like that: mutate(iris_group = case_when( str_detect(name, "Sepal.Length") ~ "category1", str_detect(name, "Petal.Length", "Sepal.Width") ~ "category2", str_detect(name, "Petal.Width") ~ "category3", TRUE ~ "")) But how do I need to write the value in category 2 - "Petal.Length" and "Sepal.Width", in brackets, with another separator etc.?
str_detect(name, "Petal.Length|Sepal.Width") ~ "category2"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.