0
votes

I have a boxplot showing scores 'before' and 'after' an intervention. I want to a few dots a different color, based on another score.

In other words, say ten subjects had scores of (8,9,10,8,9,10,8,9,10,8) before an intervention, and (12,9,12,8,9,10,10,9,10,10) after an intervention. Right now, using standard ggplot boxplot, I am showing group 1 and group 2 as boxplots with different colors. I would like to color a few subjets with a third colour (seen in the before and after plots), demonstrating the presence of an additional binary variable (like age > 50).

PreopRespondersBoxPlotLDS <- ggboxplot(data=PreopResponders, 
x="Response",y="RespondersVsNonPreopLDS.preop", color = "Response", palette = 
"jco", add = "jitter") + labs(title = "Left Dorsal Striatum Connectivity to the Right Occipital Cortex", y = "Connectivity")
 + theme(axis.title.x = element_blank(), legend.position="none", legend.title = element_blank(), plot.title = element_text(hjust = 0.5))

I'm not getting an error message, I just want to be able to use an extra color to code for a binary variable.

1
Use backticks (`, next to the 1 under the ESC key) not quotes to get code formatting in your question.Gregor Thomas
If you want to add points, add + geom_point() to your graph. Give it the subset of data you want points for, geom_point(data = subset(PreopResponders, ::criteria::)). If you want to add colors for those points, add a mapping for that layer, geom_point(data = subset(PreopResponders, ::criteria::), aes(color = binary_variable_name)). If you need more help, please post a sample of your data. dput() is the nicest way to share data because it is copy/pasteable.Gregor Thomas

1 Answers

0
votes

The following does what the question asks for. It uses geom_boxplot, not ggboxplot. And subsets the data by a binary condition in the call to geom_jitter. This condition can easily become a binary dataframe variable.

Note also that I have included a newline character, "\n", in the graph title.

library(ggplot2)

g <- ggplot(PreopResponders, aes(x = Response, y = RespondersVsNonPreopLDS.preop,
                                 color = Response)) + 
              geom_boxplot() +
              geom_jitter(data = subset(PreopResponders, Age > 50), 
                          color = "black",
                          width = 0.25, height = 0.25) +
              labs(title = "Left Dorsal Striatum Connectivity \nto the Right Occipital Cortex", y = "Connectivity") +
              theme(axis.title.x = element_blank(), 
                    legend.position = "none", 
                    legend.title = element_blank(), 
                    plot.title = element_text(hjust = 0.5))

g

enter image description here

Data creation code.

PreopResponders <- data.frame(Response = factor(rep(c("Before", "After"), each = 10), labels = c("Before", "After")),
                              RespondersVsNonPreopLDS.preop = c(c(8,9,10,8,9,10,8,9,10,8),
                                                                c(12,9,12,8,9,10,10,9,10,10)))
set.seed(1234)
PreopResponders$Age <- sample(18:100, 20, TRUE)