简体   繁体   English

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

[英]ggplot2: Adding another legend to a plot (two times)

I have the following data set that is used for plotting a bubble plot frequencies. 我有以下用于绘制气泡图频率的数据集。

Freq are frequencies at time 1 频率是时间1的频率

Freq1 are frequencies at time 2 频率1是时间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

I am using the following code for plotting, and I do like the plot. 我正在使用以下代码进行绘图,并且我确实喜欢该绘图。 However I am having some troubles with the legend that I explain below: 但是,我在以下说明的传说中遇到了一些麻烦:

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()

在此处输入图片说明

1- I would like to have two legends: one for color, the other one for size, but i can't get the right arguments to do it (I have consult guide and theme documentation and i can't solve my problem with my own ideas) 1-我想有两个图例:一个是颜色,另一个是尺寸,但我无法获得正确的论据(我有参考指南和主题文档,我无法解决我的问题自己的想法)

2- After having the two legends, I would like to increase the size of the legend shape in order to look bigger (not the text, not the background, just the shape (without actually changing the plot)). 2-具有两个图例后,我想增加图例形状的大小以使其看起来更大(不是文本,不是背景,只是形状(实际上没有更改绘图))。 Here and example from what I would have and what i would like (that's an example from my real data). 这里是我所拥有和我想要的东西的例子(这是我的真实数据的例子)。 As you can see is almost impossible to distinguish the color in the first image. 如您所见,几乎不可能在第一张图像中区分颜色。

在此处输入图片说明

Sorry if it's a newbie question, but i can't really get an example of that. 抱歉,如果这是一个新手问题,但是我真的无法得到一个例子。

Thanks, 谢谢,

Angulo 安古洛

Try something like this 试试这个

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)))

The last line will make the circles in the "colour" legend larger. 最后一行将使“颜色”图例中的圆圈变大。

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

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