简体   繁体   English

将图例添加到由两列组成的 ggplot

[英]Adding legend to a ggplot made from two columns

I am plotting residuals from two different methods with the following code:我正在使用以下代码绘制两种不同方法的残差:

ggplot(df_index, aes(cases100ppl, lm_errors)) +
  geom_point(alpha=1) +
  geom_point(data=df_index, aes(cases100ppl, error), col="red", alpha=0.2) 

How can I add a legend to this?我如何为此添加图例?

the data has a structure like this:数据具有如下结构:

code       cases100ppl   error         lm_errors
E02000001  0.05575558    0.2228769     0.1554760                
E02000002  0.11299289    0.3680860     0.4357544            
E02000003  0.11938429    0.4785204     0.3163543            
E02000004  0.10767160    0.1978992     0.3909933            
E02000005  0.11138804    0.3544542     0.3370886            
E02000007  0.09484474    0.3447380     0.3881657

Output looks something like this:输出看起来像这样: 在此处输入图片说明 Thanks!谢谢!

You will need to change a little your data so you can use aes() to set the color and the alpha.您将需要稍微更改您的数据,以便您可以使用aes()来设置颜色和 alpha。 This is a very usefull trick with ggplot (you can find ways to do it, including the one presented here, on SO posts like this one ).这是 ggplot 的一个非常有用的技巧(您可以在像这样的SO 帖子上找到方法,包括此处介绍的方法)。 You can find more general informations about pivoting here in the book R for data science, chapter 12 Tidy data .你可以在R for data science 一书中找到更多关于数据透视的一般信息,第 12 章整理数据

Accordingly, I pivot your dataframe to make a new variable called error_type .因此,我将您的数据error_type旋转以创建一个名为error_type的新变量。 This new variable is then used inside aes() so the legend is created accordingly.然后在aes()使用这个新变量,以便相应地创建图例。 Note that, using, dplyr pipe symbol %>% I pivot your dataframe just before entering ggplot world, without changing the original df_index object.请注意,使用dplyr管道符号%>%我在进入 ggplot 世界之前旋转您的数据框,而不更改原始df_index对象。

Then you can use scale_alpha_manual() and scale_colour_manual() to custom the color and the alpha the way you want it to be.然后您可以使用scale_alpha_manual()scale_colour_manual()按照您希望的方式自定义颜色和 alpha。

Here is a start:这是一个开始:

library(dplyr)
library(tidyr)
library(ggplot2)

df_index %>% 
  pivot_longer(cols = c("error", "lm_errors"), names_to = "error_type", values_to = "error_value") %>% 
  ggplot(data = ., aes(x = cases100ppl, 
                       y = error_value, 
                       color = error_type, 
                       alpha = error_type)) + # do not forget to put alpha inside aes()!
  scale_alpha_manual(values = c("error" = 0.3, "lm_errors" = 1)) +
  geom_point()

ex_plot

Data:数据:

df_index <- structure(list(code = c("E02000001", "E02000002", "E02000003", 
                                    "E02000004", "E02000005", "E02000007"), cases100ppl = c(0.05575558, 
                                                                                            0.11299289, 0.11938429, 0.1076716, 0.11138804, 0.09484474), error = c(0.2228769, 
                                                                                                                                                                  0.368086, 0.4785204, 0.1978992, 0.3544542, 0.344738), lm_errors = c(0.155476, 
                                                                                                                                                                                                                                      0.4357544, 0.3163543, 0.3909933, 0.3370886, 0.3881657)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                   -6L))

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

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