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") );
Run Code Online (Sandbox Code Playgroud)
蓝色用于"读/写",红色用于"说".如何在图例中进行修正?(我不想在barplot函数中定义图例)
A5C*_*2T1 24
您可以使用fill
颜色的参数.与David Robinson的回答一样,我也建议在这种情况下将图例放在右上角.
legend("topright",
legend = c("reading/Writing", "Speaking"),
fill = c("darkblue", "red"))
Run Code Online (Sandbox Code Playgroud)
查看您的其他一些问题,您可能还需要花一些时间在绘图之前将数据转换为更合适的形式.
这是一个例子:
这是你的数据:
heights1 = c(5, 5, 4.5, 4, 4, 4, 4.5, 2, 4, 4) # Your data
Run Code Online (Sandbox Code Playgroud)这是matrix
适当的数据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
Run Code Online (Sandbox Code Playgroud)定义您的颜色(可选,但如果您使用的不仅仅是每组的一对条形,则非常有用)
colors <- c("darkblue", "red") # Define the colors you're using
Run Code Online (Sandbox Code Playgroud)绘制数据,在顶部添加一些额外的空间并抑制轴.不确定为什么你不想在这个阶段包含图例,但可以通过添加以下参数轻松完成: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)")
Run Code Online (Sandbox Code Playgroud)重新引入y轴并添加图例
axis(2, at = 0:5, labels = 0:5)
legend("topright", colnames(mydata), fill = colors, bty = "n")
Run Code Online (Sandbox Code Playgroud)