ggplot更改图例标题和标签

时间:2018-02-06 04:50:06

标签: r ggplot2

我正在尝试更改图例标题和键标签以显示自定义信息。这就是我所拥有的。

enter image description here

这是我尝试的代码。

g + scale_fill_discrete(name="Experimental\nCondition",          
      breaks=c("IL,1","IN,1","MI,1","OH,1","WI,1","IL","IN","MI","OH","WI"),
      labels=c("1","2","3","4","5","6","7","8","9","10"))

谢谢

1 个答案:

答案 0 :(得分:0)

似乎OP正在寻找一种方法,可以将colourfill用于数据点,然后自定义自己选择的legend

在作为OP的一部分提供的示例中,最后“5”点使用与white相同的颜色。在定义自定义fill颜色时应该牢记这一点。

另一点是,只有shape值大于20的点才允许同时使用colorfill。这就是为什么shape在下面的示例中被视为“22”的原因。

样本数据: 库(GGPLOT2)

df< - data.frame(A = 1:20,B =样本(30:40,20,TRUE),C =样本(1:10,20,TRUE))

ggplot(df, aes(A, B, colour = factor(C), fill = factor(C))) + 
  geom_point(alpha = 0.6, shape = 22, size = 4) +
  scale_fill_manual(name="Experimental\nCondition", 
    values = c("gray","pink","coral","seagreen","ivory","white","white","white","white","white"),
    labels=c("IL,1","IN,1","MI,1","OH,1","WI,1","IL","IN","MI","OH","WI"),
    breaks=c("1","2","3","4","5","6","7","8","9","10")) +
  scale_colour_manual(name="Experimental\nCondition", 
    values = c("red","green","yellow","blue","pink","ivory","gray","coral","seagreen","orange"),
    labels=c("IL,1","IN,1","MI,1","OH,1","WI,1","IL","IN","MI","OH","WI"),
                    breaks=c("1","2","3","4","5","6","7","8","9","10"))

enter image description here