Che*_*hen 1 math statistics plot r ggplot2
我的代码如下,我想更改ggplot的标签,但R总是提醒我:
Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
ggplot(mat,aes(x=sales,col=type))+
geom_density()+labels("red_sold","blue_sold","yellow_sold")
Run Code Online (Sandbox Code Playgroud)
Dan*_*iel 11
作为对@jlhoward答案的补充,scale_color_manual()
更倾向于自定义色阶(将显示的实际颜色)。
对于您的情况,您可能更愿意使用scale_color_discrete()
:
ggplot(mtcars, aes(x=hp,color=factor(cyl)))+
geom_density()+
scale_color_discrete(name="Cylinders",
labels=c("4 Cylinder","6 Cylinder","8- Cylinder"))
Run Code Online (Sandbox Code Playgroud)
这更快,但它取决于因子水平顺序,这可能会导致不可重复性。您可能需要指定breaks
参数以最大限度地降低错误风险(并自定义图例中的顺序):
ggplot(mtcars, aes(x=hp,color=factor(cyl)))+
geom_density()+
scale_color_discrete(name="Cylinders",
breaks=c(8,6,4),
labels=c("8 Cylinder","6 Cylinder","4 Cylinder"))
Run Code Online (Sandbox Code Playgroud)
更多信息请访问https://ggplot2-book.org/scales.html。
是mat$type
一个因素?如果没有,那将导致错误.此外,你不能这样使用labels(...)
.
由于您未提供任何数据,因此这里是使用内置mtcars
数据集的示例.
ggplot(mtcars, aes(x=hp,color=factor(cyl)))+
geom_density()+
scale_color_manual(name="Cylinders",
labels=c("4 Cylinder","6 Cylinder","8- Cylinder"),
values=c("red","green","blue"))
Run Code Online (Sandbox Code Playgroud)
在这个例子中,
ggplot(mtcars, aes(x=hp,color=cyl))+...
Run Code Online (Sandbox Code Playgroud)
会导致你得到的同样的错误,因为mtcars$cyl
这不是一个因素.
归档时间: |
|
查看次数: |
5770 次 |
最近记录: |