gpuguy
gpuguy

Reputation: 4585

Legends in barplot not appearing properly

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

Upvotes: 23

Views: 97809

Answers (2)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193637

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

Upvotes: 31

David Robinson
David Robinson

Reputation: 78610

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

Upvotes: 3

Related Questions