gpt4 book ai didi

r - 条形图中的图例无法正确显示

转载 作者:行者123 更新时间:2023-12-03 14:42:27 25 4
gpt4 key购买 nike

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") );


蓝色表示“读/写”,红色表示“说”。如何在图例中进行更正? (我不想在barplot函数中定义图例)

最佳答案

您可以为颜色使用fill参数。与David Robinson的答案一样,在这种情况下,我也建议将图例放在右上角。

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






查看其他一些问题,您可能还需要花一些时间在绘图之前将数据转换为更合适的形式。

这是一个例子:


这是您的数据:

heights1 = c(5, 5, 4.5, 4, 4, 4, 4.5, 2, 4, 4) # Your data

这是带有 matrixdimnames数据

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

定义您的颜色(可选,但如果每个组中使用的条数不止一对,则很有用)

colors <- c("darkblue", "red") # Define the colors you're using

绘制数据,在顶部添加一些额外的空间并压下轴。不知道为什么在此阶段不希望包含图例,但是可以通过添加以下参数轻松完成: 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)")

重新引入y轴并添加图例

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

关于r - 条形图中的图例无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13936223/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com