Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Legends in barplot not appearing properly

Tags:

heights1=c(5,5,4.5,4,4,4,4.5,2,4,4)  opar <- par(lwd = 0.3)  barplot(heights1,xlim=c(0,3), ylim=c(0,5),  width=0.1,  main="Langauges(Verbal & Non-verbal)",  names.arg=c("Spanish", "Speak" , "English","Speak", "Hindi",  "Speak", "Arabic", "Speak",  "Body Lang", "Speak"), ylab="Skill level ",  xlab="Language starting with mostly used", col=c("darkblue","red"), cex.names=0.7,space=c(2,0,2,0,2,0,2,0,2,0))  legend("top",  c("darkblue","red"), c("reading/Writing", "Speaking")  ); 

Blue is for "reading/writing" and red is for "speaking". How do I make correction in legend? (I do not want to define legend inside barplot function)

enter image description here

like image 373
gpuguy Avatar asked Dec 18 '12 15:12

gpuguy


People also ask

How do I add a legend to a Barchart in R?

In R you can add a legend to any plot using the legend() command. You can also use the legend = TRUE parameter in the barplot() command. The barplot() command is the only general plot type that has a legend parameter (the others need a separate legend).

How do you add a legend to a bar graph in Matlab?

Set the DisplayName property as a name-value pair when calling the plotting functions. Then, call the legend command to create the legend. Legends automatically update when you add or delete a data series. If you add more data to the axes, use the DisplayName property to specify the labels.


2 Answers

You can use the fill argument for your colors. As with David Robinson's answer, I would also suggest placing the legend in the top right in this case.

legend("topright",         legend = c("reading/Writing", "Speaking"),         fill = c("darkblue", "red")) 

enter image description here


Looking at some of your other questions, you might also want to spend some time getting your data into a more appropriate form before plotting.

Here's an example:

  1. Here is your data:

    heights1 = c(5, 5, 4.5, 4, 4, 4, 4.5, 2, 4, 4) # Your data 
  2. Here is your data as a matrix with appropriate dimnames

    mydata <- matrix(heights1, ncol = 2, byrow = TRUE,                  dimnames = list(c("Spanish", "English", "Hindi",                                     "Arabic", "Body Lang"),                                  c("Reading/Writing", "Speaking"))) mydata # Much more meaningful to look at than a simple vector #           Reading/Writing Speaking # Spanish               5.0        5 # English               4.5        4 # Hindi                 4.0        4 # Arabic                4.5        2 # Body Lang             4.0        4 
  3. Define your colors (optional, but useful if you are working with more than just a pair of bars per group)

    colors <- c("darkblue", "red") # Define the colors you're using 
  4. Plot your data adding a little bit of extra space at the top and suppressing your axes. Not sure why you don't want to include the legend at this stage, but it could easily be done by adding the following arguments: legend.text = TRUE, args.legend = list(x = "topright", bty = "n")

    barplot(t(mydata), beside = TRUE, col = colors,          ylim = c(0, 6), axes = FALSE,         xlab = "Language starting with mostly used",         main = "Languages (Verbal & Non-verbal)") 
  5. Reintroduce your y-axis and add your legend

    axis(2, at = 0:5, labels = 0:5) legend("topright", colnames(mydata), fill = colors, bty = "n") 

    enter image description here

like image 124
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 22 '22 08:09

A5C1D2H2I1M1N2O1R2T1


Change your legend line to

legend("topright", c("reading/Writing", "Speaking"), col=c("darkblue","red"), lwd=10); 

The lwd argument says the legend should have lines of 10 pixel thickness of each of the corresponding colors. It's a good idea to use "topright" rather than "top" in your case so that the legend doesn't appear under the bars.

enter image description here

like image 27
David Robinson Avatar answered Sep 18 '22 08:09

David Robinson