我的代码如下所示,我想更改ggplot的标签,但R总是提醒我:
Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0我该怎么办?
ggplot(mat,aes(x=sales,col=type))+
geom_density()+labels("red_sold","blue_sold","yellow_sold")发布于 2014-04-19 04:55:03
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"))

在此示例中,
ggplot(mtcars, aes(x=hp,color=cyl))+...将导致与您得到的相同的错误,因为mtcars$cyl不是一个因素。
发布于 2020-08-11 01:06:14
作为对@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"))这是更快的,但它取决于因子级别的顺序,这可能会导致不可重复性。您可能希望指定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"))https://stackoverflow.com/questions/23161897
复制相似问题