Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在不影响绘图其他元素的情况下,手动设置ggplot2中geom_路径的颜色_R_Ggplot2 - Fatal编程技术网

R 在不影响绘图其他元素的情况下,手动设置ggplot2中geom_路径的颜色

R 在不影响绘图其他元素的情况下,手动设置ggplot2中geom_路径的颜色,r,ggplot2,R,Ggplot2,鉴于我下面的示例数据new_mtcars,如果能根据变量new_mtcars$group手动设置geom_path的颜色,我将不胜感激,例如black到group1和red到group2 下面,我根据以下内容介绍我的失败尝试。问题是它根本没有应用颜色黑色和红色我想几何路径 #准备样本数据 mtmodel <- lm(mpg ~ wt, data = mtcars) mtcars$Low <- predict(mtmodel, newdata = mtcars, interval =

鉴于我下面的示例数据
new_mtcars
,如果能根据变量
new_mtcars$group
手动设置
geom_path
的颜色,我将不胜感激,例如
black
group1
red
group2

下面,我根据以下内容介绍我的失败尝试。问题是它根本没有应用颜色
黑色
红色
我想
几何路径

#准备样本数据

mtmodel <- lm(mpg ~ wt, data = mtcars)
mtcars$Low <- predict(mtmodel, newdata = mtcars, interval = "confidence")[,2]
mtcars$High <- predict(mtmodel, newdata = mtcars, interval = "confidence")[,3]
mtcars$Mean <- predict(mtmodel, newdata = mtcars, interval = "confidence")[,1]
new_mtcars<-tidyr::gather(mtcars, "Variable", "value", Low:Mean)
#我尝试根据组的级别手动设置geom_路径的颜色

    #does not work

  ggplot(new_mtcars, aes(x=wt, y=value, linetype=Variable)) +
  geom_path(size = 0.71, aes(colour = group)) +
  geom_point(aes(colour = cyl)) 
  scale_color_manual(values = c("black", "red"))
#我的当前输出,它不会将所选颜色应用于geo_路径

ggplot(new_mtcars, aes(x=wt, y=value, linetype=Variable)) +
  geom_path(size = 0.71, aes(colour = group)) +
  geom_point(aes(colour = cyl)) 


提前感谢您的帮助。

这里有一个解决方案,它依赖于手动定义每种颜色。我不知道如何定义其中的一小部分,而让其他部分自动定义

ggplot(new_mtcars, aes(x=wt, y=value, linetype=Variable)) +
  geom_path(size = 0.71, aes(colour = group)) +
  # In the example, cyl was still numeric, creating an error here,
  #   since group is discrete and ggplot doesn't know how to mix
  #   discrete and continuous variables to the same aesthetic.
  geom_point(aes(colour = as.character(cyl)), shape = 21) +
  scale_color_manual(values = c("4" = "blue", "6" = "green", "8" = "purple", 
                                "group1" = "black", 
                                "group2" = "red"))

这里有一个解决方案,它依赖于手动定义每种颜色。我不知道如何定义其中的一小部分,而让其他部分自动定义

ggplot(new_mtcars, aes(x=wt, y=value, linetype=Variable)) +
  geom_path(size = 0.71, aes(colour = group)) +
  # In the example, cyl was still numeric, creating an error here,
  #   since group is discrete and ggplot doesn't know how to mix
  #   discrete and continuous variables to the same aesthetic.
  geom_point(aes(colour = as.character(cyl)), shape = 21) +
  scale_color_manual(values = c("4" = "blue", "6" = "green", "8" = "purple", 
                                "group1" = "black", 
                                "group2" = "red"))

我无法重现这个问题。我只有在运行你的代码时才会得到黑点。谢谢,@markus。只是相应地编辑了代码和文本。您是否将
cyl
重新定义为离散字符或因子?我在给定的代码中得到了一个错误,因为
group
是离散的,但是
cyl
是连续的,并且两者都映射到了颜色。我无法重现该问题。我只有在运行你的代码时才会得到黑点。谢谢,@markus。只是相应地编辑了代码和文本。您是否将
cyl
重新定义为离散字符或因子?我在给定的代码中得到一个错误,因为
group
是离散的,但是
cyl
是连续的,并且两者都映射到颜色。