ggplot颜色不会根据组自动着色 [英] ggplot color is not automatically coloring based on group
问题描述
我正在尝试按组'candidate'在图形上创建线.
I'm trying to create to lines on a graph by group 'candidate'
我的代码是 ggplot(grouped_covid,aes(x = newDate,y = positiveIncrease,group = candidate,color =候选人))+ geom_line()
当我设置 color =候选人
时,出现错误:未知颜色名称:Clinton,Hillary
.我的理解是,它应该只是根据候选项下的分组自动设置颜色,但似乎认为我正在尝试定义颜色.
When I set color = candidate
, I get Error: Unknown colour name: Clinton, Hillary
. My understanding is that it should just automatically set the color based on the grouping under candidate but it seems think I am trying to define the colors.
推荐答案
@teunbrand被发现.有趣的.您可能以某种方式自愿或多或少地使用了 I()
,这使R可以按原样"解释对象.另请参见?I
@teunbrand was spot on.
Interesting. You may have somehow more or less voluntarily used I()
, which lets R interpret an object "as is". See also ?I
这里是如何转换回普通字符:
Here how to convert back to plain character:
您可以在对ggplot本身的调用中临时执行此操作,或者通过分配(我认为您想这样做)更永久地执行此操作.
You can do that either temporarily in the call to ggplot itself, or more permanently, by assignment (which I think you want to do).
更新在评论中,用户teunbrand指出了S3方法 scale_type.AsIs
,这就是为什么使用"asIs"的原因.对象的工作原理与使用 scale ... identity
update
in the comments, user teunbrand pointed to the S3 Method scale_type.AsIs
, which is why using an "asIs" object works just like using scale...identity
## this is to reproduce your data structure
iris2 <- iris
iris2$Species <- I(as.character(iris2$Species))
library(ggplot2)
ggplot(iris2, aes(x=Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point()
#> Error: Unknown colour name: setosa
#convert withing ggplot
ggplot(iris2, aes(x=Sepal.Length, y = Sepal.Width, color = as.character(Species))) +
geom_point()
## convert by assignment
iris2$Species <- as.character(iris2$Species)
ggplot(iris2, aes(x=Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point()
由 reprex软件包(v0.3.0)创建于2020-07-01 sup>
Created on 2020-07-01 by the reprex package (v0.3.0)
这篇关于ggplot颜色不会根据组自动着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!