ggplot2:在绘图中添加另一个图例(两次)

时间:2014-11-20 10:45:59

标签: r plot ggplot2 legend

我有以下用于绘制气泡图频率的数据集。

频率是时间1的频率

Freq1是时间2的频率

id names  variable value Freq Freq.1

1   1   item1   1   13  11
2   2   item2   1   9   96
3   3   item1   2   10  28
4   4   item2   2   15  8
5   5   item1   3   9   80
6   6   item2   3   9   10
7   7   item1   4   11  89
8   8   item2   4   14  8
9   9   item1   5   3   97
10  10  item2   5   25  82

我使用以下代码进行绘图,我喜欢这个情节。但是我在下面解释的传说中遇到了一些麻烦:

theme_nogrid <- function (base_size = 12, base_family = "") {
  theme_bw(base_size = base_size, base_family = base_family) %+replace% 
    theme(panel.grid = element_blank())   
}
plot1<- ggplot(Data, aes(x = variable, y = value, size = Freq, color=Freq.1))+
  geom_point( aes(size = Freq,  stat = "identity", position = "identity"),
              shape = 19, color="black", alpha=0.5) +
  geom_point( aes(size = Freq.1,  stat = "identity", position = "identity"),
              shape = 19, color="red", alpha=0.5) +
  scale_size_continuous(name= "Frequencies ", range = c(2,30))+
  theme_nogrid()

enter image description here

1-我想有两个传说:一个用于颜色,另一个用于大小,但我无法得到正确的参数(我有咨询指南和主题文档,我无法解决我的问题)我自己想法的问题)

2-在有了两个传说之后,我想增加图例形状的大小以便看起来更大(不是文字,不是背景,只是形状(没有实际改变图))。 这里和我将拥有的例子和我想要的例子(这是我真实数据的一个例子)。如您所见,几乎无法区分第一张图像中的颜色。

enter image description here

很抱歉,如果这是一个新手问题,但我无法得到一个例子。

谢谢,

Angulo

1 个答案:

答案 0 :(得分:1)

尝试这样的事情

library(ggplot2) 
library(tidyr)
d <- gather(Data, type, freq, Freq, Freq.1)
ggplot(d, aes(x = variable, y = value))+
    geom_point(aes(size = freq, colour = type), shape = 19, alpha = 0.5) +
    scale_size_continuous(name = "Frequencies ", range = c(2, 30)) +
    scale_colour_manual(values = c("red", "blue")) +
    theme_nogrid() +
    guides(colour = guide_legend(override.aes = list(size = 10)))

最后一行会使“颜色”图例中的圆圈变大。