Jo-Achna
Jo-Achna

Reputation: 315

Adjust bean plot and legend in R

I have a question regarding my bean plot and the legend. I cannot find a way to do following things

Here is my code:

beanplot(onset_s~ group*meaning, data=mu3,ll = 0.08,
     main = "Distribution of movement units", side = "both",
     col = list("black", c("grey", "white")),
     axes=T, beanlines = "median")

legend("topleft",fill = c("grey", "black"), legend = c("Non-Performers", "Experts"), cex=0.65)

My data set:

tier            meaning      onset_sgroup   
head_face_mu    self_focused    0   expert  
head_face_mu    self_focused    0   expert  
head_face_mu    context_focused 0   expert  
upper_body_mu   self_focused    0   expert  
upper_body_mu   self_focused    0   expert  
head_face_mu    communication_focused   0   novice  
head_face_mu    context_focused 0   novice  
head_face_mu    context_focused 0   novice  
upper_body_mu   self_focused    0   novice  
upper_body_mu   self_focused    0   novice  
upper_body_mu   self_focused    0   novice  
head_face_mu    self_focused    0.18    novice  
lower_body_mu   self_focused    0.667   novice  
head_face_mu    communication_focused   0.69    novice  
head_face_mu    context_focused 1.139   novice  
head_face_mu    context_focused 1.301   novice  
head_face_mu    context_focused 1.32    novice  
lower_body_mu   self_focused    1.66    novice  
head_face_mu    context_focused 1.98    novice  
lower_body_mu   self_focused    2.205   novice  
head_face_mu    communication_focused   2.297   novice  
head_face_mu    context_focused 2.349   novice  
lower_body_mu   self_focused    2.417   novice  
upper_body_mu   self_focused    2.666   novice  
head_face_mu    self_focused    2.675   expert  
head_face_mu    context_focused 3.218   novice  
head_face_mu    context_focused 3.353   novice  
head_face_mu    context_focused 3.436   expert  
head_face_mu    context_focused 3.588   novice  
head_face_mu    context_focused 3.697   novice  
upper_body_mu   self_focused    4.006   novice  
upper_body_mu   context_focused 4.033   novice  
upper_body_mu   self_focused    4.06    expert  
head_face_mu    context_focused 4.33    novice  
upper_body_mu   self_focused    4.332   novice  
upper_body_mu   self_focused    4.44    novice  
head_face_mu    context_focused 4.738   novice  
lower_body_mu   self_focused    5.395   novice  
head_face_mu    self_focused    5.428   novice  
lower_body_mu   self_focused    5.926   novice  
head_face_mu    context_focused 6.283   novice  
head_face_mu    context_focused 7.002   novice  
head_face_mu    self_focused    7.031   novice  
lower_body_mu   self_focused    7.189   novice  
upper_body_mu   communication_focused   7.45    novice  
lower_body_mu   self_focused    7.632   expert  1.144   
head_face_mu    self_focused    7.739   expert  2.159
lower_body_mu   self_focused    8.943   novice  9.517   
head_face_mu    context_focused 9.002   expert  4.608   

This is my graph:

enter image description here

Any feedback and comments are more than welcome!

Thank you in advance.

Upvotes: 1

Views: 2616

Answers (1)

tbradley
tbradley

Reputation: 2290

Well, I have played with it for a while and I have managed to answer all your questions except changing the color of the mean line. The set up of the col = argument in the beanplot() function seems a bit finicky to me. However, here is what I have for everything else:

par(xpd=F, mai = c(1, 1, 1, 2))
beanplot(onset_s~ group*meaning, data=df,ll = 0.08,
         main = "Distribution of movement units", side = "both",
         col = list("black", c("grey", "white")),
         axes=F, beanlines = "median", ylim = c(-5, 15)) #axes = F let's you define your own axes

#now you can define the x and y axes like this
#note - this also takes care of your renaming issue
#although you could just change the character values using subsetting
#axis 1 is the x axis
axis(1, at = 1:3, labels = c("communication-focused", "context-focused",     "self-focused"))

#axis 2 is the y axis, and you can change the values in the "at =" vector
axis(2, at = c(-5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15))


#this creates the outer box around the plot, which is removed with axes = F
box(which = "plot")

#This now lets you add things to the graphic system outside of the plotting area
par(xpd=TRUE)

#you will need to change the x and y coordinates 
#also, the "cex =' argument of the legend function controls
#the size of the text, so by setting this equal to one, you achieve larger text
legend(3.7, 15.8,fill = c("grey", "black"), legend = c("Non-Performers", "Experts"), cex=1)

Now, I tried multiple different combinations in the col = argument within beanplot to fix your mean line question. However, the closest I got was turning that line red but then getting rid of the split set up of each bean. I think you should just play around with different combinations and read the description of that specific argument in the ?beanplot help page.

Anyway, I hope this helps

UPDATE I have added the mai = c() argument to the first function call of par(). This argument allows you to control the margins around the plot by specifying in inches the margin size of each respective margin (in order from bottom, left, top, right). This should solve your problem. I have added the graph image to show what it looks like currently: enter image description here

Upvotes: 2

Related Questions