1

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

  • change the x-axis labels (from communication_focused to communication-focused)
  • show the y-axis in more fine-grained way (e.g. breaks should be from 0, 25,50,75,100,125...300)
  • put the legend outside the plot
  • keep the font of the text in legend bigger than what I get now
  • mark the mean (black solid line in each category) in each category in "red" color

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.

Jo-Achna
  • 315
  • 1
  • 3
  • 14
  • Firstly, it is always helpful when you add your data set, which can be done using `dput()`. Second, you should look into using `geom_violin` in the `ggplot2` package as that will solve your legend issues. Thirdly, in order to split your plot by category you can see the answer in this question [split violin plot](http://stackoverflow.com/questions/35717353/split-violin-plot-with-ggplot2). Finally, you can just use `geom_line` to add your mean value and set its color to red. – tbradley Mar 01 '17 at 23:34
  • @tbradley: The 1st point: I added the 50 points of my data subset. If there is an option to add the whole ".csv" file to my question (my subset contains almost 1000 data points) and you would like me to do that, please let me know. The 2nd and 3rd point. The `geom_violin` plot is not an option for me because a.) you can't see individual outliers with it, b.) only minimum and maximum are visible on the graph, and c.) no indication of the number of observations in a group can be observed in the graphic representation. – Jo-Achna Mar 02 '17 at 08:17

1 Answers1

2

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
tbradley
  • 2,210
  • 11
  • 20
  • @ tbradley: Thank you very much for the solution. Everything works fine but now I have a new problem with the legend that's outside of the plot's space. After changing my `x` and `y` coordinates to `legend(3.7, 365)`, I can see only a bit of the legend on the right upper side of the plot. It looks like the plot did not shrink to make "space" for the legend. Do you have any suggestions how to fix it? – Jo-Achna Mar 03 '17 at 13:04
  • @user3832272 I have made some edits that should solve that problem for you. – tbradley Mar 03 '17 at 13:45
  • Also, as it seems you are new to SO, if this answer solved your problem, please mark it as correct! Thanks! – tbradley Mar 03 '17 at 13:47
  • I was about to mark it as CORRECT after your last suggestion, so no worries :) Many thanks for your time and help. – Jo-Achna Mar 03 '17 at 14:00
  • I think you should add `xaxt = "n"` to your beanplot function. Otherwise, there will be an overlap between old labels and new labels. – Hadij Mar 18 '19 at 17:45