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)\",
nam
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.