0

I am trying to create a bar chart for the recovery of a ICP MS machine. So I am having two vectors :

    > number_matches
 [1]  5.0 10.0 12.5 15.0 17.5 20.0 25.0 10.0 25.0 25.0 25.0
> iC_MW
 [1]  4.808625427  9.073759209 11.058866657 12.916878917 14.974790823 16.515987287
 [7] 20.721595708  0.006576491  0.040618454 20.031721743 20.885739448

My aim is to generate a barplot with paired bars from number_matches and iC_MW + the values of the iC_MW as numbes above the bar pair. I approached it this way:

recovery_frame_number<-as.matrix(number_matches)
recovery_frame_iC_MW<-as.matrix(iC_MW)
recovery_frame_merged <-cbind(recovery_frame_number,recovery_frame_iC_MW)
recovery_frame_merged<-t(recovery_frame_merged)
# Bar plot 
ylim_number <- c(0, 1.1*max(number_matches))
ylim_iC_MW <- c(0, 1.1*max(iC_MW))
ylim_max <- as.numeric( max(ylim_iC_MW,ylim_number))
ylim_max <- c(0,ylim_max)
iC_MW<-round(iC_MW, 2)

bplot<-barplot(recovery_frame_merged, beside = T, col=c("red", "darkblue"), names.arg = number_matches, ylim = ylim_max)
text(x = number_matches, y = number_matches, labels = iC_MW)

I get the plot which I want to have, but I am having difficulties with the positioning of the labels. I tried different ways which I found in the net but usually my labels are positioned too near to each other and not at the correct places. For example see picture. If could give me a hint I would be very thankful. Thanks for reading and have a nice day :)

EDIT: Let me clarify that I am not using ggplot2 and I dont get the ggplot2 solution either working

Cooparion
  • 7
  • 3
  • How to display the frequency at the top of each factor in a barplot in R : the $ doesnt work for atomic values. How can I then transform my vector to a non atomic vector ? – Cooparion May 13 '16 at 13:30
  • The `$` operator pulls elements out of lists. Since your material is already extracted, you don't need this. Something like the following will work: `text(x = bplot, y =number_matches, label = iC_MW, pos = 3, cex = 0.8, col = "red")`. – lmo May 13 '16 at 13:53
  • Hi unfortunatly this doesn`t work either, now i get the values twice over my plot – Cooparion May 18 '16 at 11:36
  • ah now it works thanks for the hint my soluition was: `text(x = bplot[1,]+1, y =number_matches+1, label = iC_MW, cex = 0.8, col = "black")` – Cooparion May 18 '16 at 11:44

0 Answers0