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)
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).
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.
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"))
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:
Here is your data:
heights1 = c(5, 5, 4.5, 4, 4, 4, 4.5, 2, 4, 4) # Your data
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
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
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)")
Reintroduce your y-axis and add your legend
axis(2, at = 0:5, labels = 0:5) legend("topright", colnames(mydata), fill = colors, bty = "n")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With