在直方图(格子)中添加图例
我是 R 编程新手,我想制作一个关于性别和相应工作的直方图。
我遇到的问题是我无法在图表中添加图例。
另外,我想在每个栏的顶部添加频率数字。
你能帮我一下吗?
library(lattice)
histogram(~ job | sex, col=rainbow(7), main="", xlab = "", ylab="(%)")
PS 我正在尝试使用图例功能,但收到此错误:
strwidth(图例,单位 =“用户”,cex = cex)中的错误: plot.new尚未被调用
I am new to R programming and I would like to make a histogram about sex and the correspondent jobs.
The problem I have is that I can't add a legend in the diagram.
Also, I would like on the top of every bar to add the frequency number.
Could you help me please ?
library(lattice)
histogram(~ job | sex, col=rainbow(7), main="", xlab = "", ylab="(%)")
P.S. I am trying with legend function but I get this error:
Error in strwidth(legend, units = "user", cex = cex) :
plot.new has not been called yet
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜您一直在阅读教您使用
attach
的介绍材料。尝试忘记这一点。如果你想使用lattice,这是一个坏习惯。 (这对于编写代码来说是一个普遍的坏习惯。)假设您在数据帧中有这两个变量,dfrm
采用长格式,然后尝试以下操作:legend
函数将与点阵不能很好地混合,因为它是基础图形。你可以尝试一下,但是放置的坐标系非常不同。我使用
singer
数据集测试了上述内容的变体,但没有成功。此示例在向歌手数据集添加Freq
列后起作用:I am guessing that you have been reading intro material that taught you to use
attach
. Try to unlearn that. It's a bad habit if you want to use lattice. (And a generally bad habit all around for writing code.) Assuming you have these two variables in a dataframe,dfrm
in long format, then try this:The
legend
function will not mix well with lattice, since it is base graphics. You could try, but the coordinate system for placement is very different.I tested a variant of the above with the
singer
dataset and it did not succeed. This example works after adding aFreq
column to the singer dataset: