Reputation: 257
I have a boxplot with a group on the left (Baseline.RT) and a group on the right (TBPM.RT). I want to rename Baseline.RT to 1-back and TBPM.RT to TBPM. I also want each group to have a different square frame. I would also like to try black and white options to fill in the values for Neutral, Positive, and Negative.
I have tried several options but did not get the result I wanted. Below is my code. Any help is welcome.
DataFrame:
data.frame(
stringsAsFactors = FALSE,
Participant = c(1, 2, 3, 4, 5, 6),
Sex = c("m", "m", "m", "f", "f", "m"),
Age = c(29, 21, 29, 22, 25, 31),
Stress = c(14, 26, 11, 19, 15, 15),
Dass21.total = c(6, 43, 4, 10, 12, 8),
Dass21Ansie = c(0, 12, 1, 3, 2, 2),
Dass.Depre = c(1, 11, 0, 1, 3, 0),
Dass.Stress = c(5, 20, 3, 6, 7, 6),
Valence = c(0, 1, 2, 0, 1, 2),
Baseline.RT = c(1.17657473346937,
0.656485061072056,0.617504973518475,0.552112912223171,
0.587283706967395,0.569011248952529),
TBPM.RT = c(1.16126499995575,
0.682658424923267,0.643632301167193,0.589782671563839,
0.705303832011063,0.691478784144668),
TotalClockChecks = c(44, 97, 44, 93, 32, 90),
TotalChecks5060 = c(13, 22, 17, 23, 10, 27),
TotalClockResets = c(18, 20, 19, 19, 18, 19),
Correct.Resets = c(16, 16, 18, 18, 12, 19),
Before.55.Resets = c(0, 2, 0, 1, 0, 0),
After.65.Resets = c(2, 2, 1, 0, 6, 0),
MeanResetTime = c(63.0026438647087,
58.9040712799639,60.9928466685597,60.4603108544334,
65.859630879724,60.5522703813385),
Accuracy.Baseline = c(0.987179487179487,
0.991489361702128,0.97907949790795,0.987234042553191,1,
0.987234042553191),
Accuracy.TBPM = c(0.968619246861925,
0.972746331236897,0.989626556016598,0.972515856236786,
0.974736842105263,0.991786447638604),
rau.Baseline = c(112.453264487601,
114.413187265486,109.508971532343,112.475825131896,
122.999999959683,112.475825131896),
rau.TBPM = c(106.447535249234,
107.58519024216,113.516946707831,107.519541719961,
108.163803190644,114.564811317506)
)
Plot:
my44 %>%
select(Participant, Valence, Baseline.RT,TBPM.RT) %>% #Select interest variables
gather(Task,RT, -Valence, -Participant) %>%
ggplot(., aes(factor(Valence), RT)) + #plot
geom_boxplot() + facet_wrap(~ Task) +
scale_x_discrete(name = element_blank(), labels=c("0" = "Neutral", "1" = "Positive", "2" = "Negative")) +
scale_fill_discrete(name="Valence",
breaks=c("0", "1", "2"),
labels=c("Neutral", "Positive",
"Negative"))
Created on 2020-09-02 by the reprex package (v0.3.0)
Upvotes: 1
Views: 146
Reputation: 174641
It sounds like you're looking for something like this (although your question's input data doesn't produce the values displayed in your plot, and you seem to have a default theme set somewhere).
Your fill colours can be chosen by scale_fill_manual
, but you need to map the Valence
variable to the fill scale if you want the different boxes to have different colours.
If you want a frame around each facet, theme_bw
does this by default, or you can use theme(panel.border = element_rect(colour = "black"))
.
To re-name facets, I would normally just re-name the faceting variables to the desired names in the input, but here I have shown an alternative method using the labeller
parameter in facet_wrap
.
my44 %>%
select(Participant, Valence, Baseline.RT,TBPM.RT) %>% #Select interest variables
gather(Task,RT, -Valence, -Participant) %>%
ggplot(., aes(factor(Valence), RT)) +
geom_boxplot(aes(fill = factor(Valence))) +
facet_wrap(~ Task,
labeller = function(x) data.frame(Task = c("1-back", "TBPM"))) +
scale_x_discrete(name = element_blank(),
labels=c("0" = "Neutral", "1" = "Positive", "2" = "Negative")) +
scale_fill_manual(name="Valence",
breaks=c("0", "1", "2"),
labels=c("Neutral", "Positive","Negative"),
values = c("gray50", "gray75", "gray95")) +
theme_bw() +
theme(legend.position = "none",
strip.background = element_blank())
Upvotes: 4