简体   繁体   English

R plot:colors 不对应,图例不是预期的

[英]R plot : colors not corresponding and legend not what expected

I have simplified my original code to explain my problem.我已经简化了我的原始代码来解释我的问题。 My goal is to plot the values from the column "Value" of my data frame enter image description here .我的目标是 plot 我的数据框的“值”列中的值在此处输入图像描述 Each dot value would have the color mentionned in column "Color".每个点值将具有“颜色”列中提到的颜色。 The plot result is not what I want because: plot 结果不是我想要的,因为:

  • the colors don't correspond (is that a problem of "factor"?) colors 不对应(这是“因素”的问题吗?)
  • I would like to have only one legend that displays: Blue color dot T1, Red color dot T2, green color dot T3我只想显示一个图例:蓝色点 T1、红色点 T2、绿色点 T3

Would anyone know how to get that result?有人知道如何得到这个结果吗? Thank you for your help !谢谢您的帮助 !

DF <- DF_Total_Tidy[1:9,1:4]
colnames(DF)<-c("Time","Name","Value","Color")
DF[1:9,4]                                                                                  
<-c("blue","blue","blue","red","red","red","green","green","green")
DF$Name<- as.character(DF$Name)
DF[1:9,2]<-c("T1","T1","T1","T2","T2","T2","T3","T3","T3")
DF

Graph<- DF %>%
  ggplot(aes(x=Time, y=Value,fill=Name,color=Color)) +
  geom_point( size=4)
plot(Graph)

enter image description here在此处输入图像描述

enter image description here在此处输入图像描述

  1. If you want to provide the color names via a column of your dataset you could use scale_color_identity(guide = "legend") where the guide argument is needed to add a legend (As a general rule I would recommend to not use something like DF$color .).如果您想通过数据集的一列提供颜色名称,您可以使用scale_color_identity(guide = "legend")需要guide参数来添加图例(作为一般规则,我建议不要使用类似DF$color 。)。 One drawback of providing the colors is that you end up with the color names in the legend.提供 colors 的一个缺点是您最终会得到图例中的颜色名称。 To fix that you have to provide your desired labels via the labels argument, for which I use a named vector in my code below.要解决此问题,您必须通过labels参数提供所需的标签,为此我在下面的代码中使用了命名向量。 Additionally your categories are ordered according to the color names.此外,您的类别是根据颜色名称排序的。 Hence, you probably have to use eg the breaks argument to set the right order of the categories in the legend.因此,您可能必须使用例如breaks参数来设置图例中类别的正确顺序。

  2. To fix your second issue remove the fill aesthetic.要解决您的第二个问题,请删除fill美学。

Using a minimal reprex based on mtcars I first reproduce your issue:使用基于mtcars的最小表示我首先重现您的问题:

library(ggplot2)

mtcars$color <- with(mtcars, dplyr::case_when(cyl == 4 ~ "blue", cyl == 6 ~ "red", TRUE ~ "green"))
                     
ggplot(mtcars, aes(hp, mpg, color = color, fill = factor(cyl))) +
  geom_point()

Now, following the instructions outlined above we get:现在,按照上面概述的说明,我们得到:

ggplot(mtcars, aes(hp, mpg, color = color)) +
  geom_point() +
  scale_color_identity(
    guide = "legend",
    labels = setNames(unique(mtcars$cyl), unique(mtcars$color)),
    breaks = c("blue", "red", "green")
  )

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM