How to differ color based on rise or fall of values in boxplot ?
2
0
Entering edit mode
6.1 years ago
Björn ▴ 110

In the attached example figure, the values from condition one to condition two either increased or decreased. How to show in different line style and color for those which increased or decreased. ![paired_boxplot][1] [1]: https://ibb.co/nBo827

rstudio boxplot ggpaired ggbarplot • 7.7k views
ADD COMMENT
0
Entering edit mode

post your code and I'll tweak it

ADD REPLY
0
Entering edit mode

For example

         ggpaired(file, x = "Condition", y = "Accuracy",
     color = "Time", line.color = "grey",line.size = 0.6, point.size = 2,
     xlab="Condition", ylab = "Accuracy",title = "",
     palette = c("green", "red"))+
     stat_compare_means(method = "t.test", paired = TRUE)
ADD REPLY
1
Entering edit mode
6.1 years ago
russhh 5.7k

Can I suggest you dump ggpaired and do it in ggplot2 (I can't quite understand why ggpaired wasn't written as a geom_* function).

I can't quite divine how your data is structured (is Time a duplication of Condition and if so, why?) so I'm going to assume it looks like this:

library(tidyverse)
library(magrittr)

set.seed(1)

my_data <- data.frame(
  Accuracy = rnorm(10),
  Condition = rep(c("condition1", "condition2"), each = 5),
  Patient = rep(paste0("P", 1:5), times = 2)
)

> my_data
     Accuracy  Condition Patient
1  -0.6264538 condition1      P1
2   0.1836433 condition1      P2
3  -0.8356286 condition1      P3
4   1.5952808 condition1      P4
5   0.3295078 condition1      P5
6  -0.8204684 condition2      P1
7   0.4874291 condition2      P2
8   0.7383247 condition2      P3
9   0.5757814 condition2      P4
10 -0.3053884 condition2      P5

In ggplot2 you've got more control:

 # lines all the same colour
ggplot(my_data, aes(x = Condition, y = Accuracy)) +
    geom_boxplot(aes(fill = Condition), alpha = 0.2, col = "grey") +
    geom_point(aes(col = Condition)) +
    geom_line(aes(group = Patient))

same_color_lines

With differentially coloured lines, you have to work out whether there's an increase or decrease before you do your plotting call:

# black = increased, red = decreased
my_data %>%
    tidyr::spread(Condition, Accuracy) %>%
    dplyr::mutate(is_increasing = condition2 > condition1) %>%
    tidyr::gather("Condition", "Accuracy", 2:3) %>%
    ggplot(aes(x = Condition, y = Accuracy)) +
    geom_boxplot(aes(fill = Condition), alpha = 0.2, col = "grey") +
    geom_point() +
    geom_line(aes(group = Patient, col = is_increasing)) +
    scale_colour_manual(values = c("red", "black"))

different_colour_lines

BTW, the data passed to ggplot now looks like this:

   Patient is_increasing  Condition   Accuracy
1       P1         FALSE condition1 -0.6264538
2       P2          TRUE condition1  0.1836433
3       P3          TRUE condition1 -0.8356286
4       P4         FALSE condition1  1.5952808
5       P5         FALSE condition1  0.3295078
6       P1         FALSE condition2 -0.8204684
7       P2          TRUE condition2  0.4874291
8       P3          TRUE condition2  0.7383247
9       P4         FALSE condition2  0.5757814
10      P5         FALSE condition2 -0.3053884
ADD COMMENT
0
Entering edit mode

@russhh Thanks I get an error message "Error: Aesthetics must be either length 1 or the same as the data (44): group, colour, x, y" Note: The data has 11 samples as condition1 and 11 samples as condition2

ADD REPLY
0
Entering edit mode

Be aware that you haven't posted your own data, or a template that represents it, or a minimal reproducible example. So I can't debug the code for your specific problem. Can you confirm that the code I posted works with the example data that I posted, please

ADD REPLY
0
Entering edit mode

Sorry, I can't share data. The example you posted works. With my data, only the second part of code don't work . what does 2:3 meant in the code tidyr::gather("Condition", "Accuracy", 2:3) %>% and I have only the positive values in Accuracy. Does it matter ?

ADD REPLY
0
Entering edit mode

Then make-up some data that conforms to the data structure that you are working with

See the help for tidyr::gather and look what happens after each step of data-manipulation that I gave you. Those are the columns that are gathered into the Accuracy column (of the output data) from the spread version of the data.

ADD REPLY
0
Entering edit mode

@russhh, Below is fraction of my data

condition,Accuracy,Patient

A,9510000,a A,762000,b A,630000,c A,98800,d A,6120000,e A,1.01E+06,f A,91200,g A,13700,h A,1.24E+05,i A,14500,j A,3680000,k B,3300,a B,1.10E+04,b B,59200,c B,57200,d B,3080,e B,75800,f B,2.28E+05,g B,6930,h B,54000,i B,35600,j B,9630,k I having problem to get the graph as you have nicely shown. Could you please try with the subset of data. Sorry, I don't know how to upload the data as csv file. Thanks

ADD REPLY
0
Entering edit mode

post it in your original question; and, rather than posting it in a non-copyable way, could you read this and post the output of using dput.

ADD REPLY
0
Entering edit mode
6.1 years ago

I suggest you to create two different dataframes, one with increased values, another with decreased values.

Then with the documentation ( https://cran.r-project.org/web/packages/ggpubr/ggpubr.pdf )

ggpaired(data, cond1, cond2, x = NULL, y = NULL, id = NULL,
color = "black", fill = "white", palette = NULL, width = 0.5,
point.size = 1.2, line.size = 0.5, line.color = "black", title = NULL,
xlab = "Condition", ylab = "Value", facet.by = NULL,
panel.labs = NULL, short.panel.labs = TRUE, label = NULL,
font.label = list(size = 11, color = "black"), label.select = NULL,
repel = FALSE, label.rectangle = FALSE, ggtheme = theme_pubr(), ...)

You can change the color of your lines with the line.color option

ADD COMMENT
0
Entering edit mode

line.color has to be two colored. one for increased value , another for decreased.

ADD REPLY
0
Entering edit mode

If you have to do it in the same plot that will be tricky. My solution was to create two ggpaired plots with different color line and to "grid" them :

grid.arrange(ggpaired1, ggpaired2)
ADD REPLY

Login before adding your answer.

Traffic: 2439 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6