使用ggplot向多个折线图添加图例 [英] Adding legends to multiple line plots with ggplot

查看:214
本文介绍了使用ggplot向多个折线图添加图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图例添加到使用ggplot创建的绘图中.我从两个csv文件中加载数据,每个文件都有两行8列(不包括标题).

I'm trying to add a legend to a plot that I've created using ggplot. I load the data in from two csv files, each of which has two columns of 8 rows (not including the header).

我从每个文件构造一个包含累积总数的数据框,因此该数据框具有三列数据( bv bin_count bin_cumulative ),每列8行,每个值都是整数.

I construct a data frame from each file which include a cumulative total, so the dataframe has three columns of data (bv, bin_count and bin_cumulative), 8 rows in each column and every value is an integer.

然后按如下所示绘制两个数据集.显示效果很好,但是我无法弄清楚如何在结果图中添加图例,因为ggplot对象本身应该具有数据源,但是我不确定如何在有多个相同列的情况下构建一个图例名称.

The two data sets are then plotted as follows. The display is fine but I can't figure out how to add a legend to the resulting plot as it seems the ggplot object itself should have a data source but I'm not sure how to build one where there are multiple columns with the same name.

library(ggplot2)

i2d <- data.frame(bv=c(0,1,2,3,4,5,6,7), bin_count=c(0,0,0,2,1,2,2,3), bin_cumulative=cumsum(c(0,0,0,2,1,2,2,3)))
i1d <- data.frame(bv=c(0,1,2,3,4,5,6,7), bin_count=c(0,1,1,2,3,2,0,1), bin_cumulative=cumsum(c(0,1,1,2,3,2,0,1)))


c_data_plot <- ggplot() + 
  geom_line(data = i1d, aes(x=i1d$bv,  y=i1d$bin_cumulative), size=2, color="turquoise") +
  geom_point(data = i1d, aes(x=i1d$bv, y=i1d$bin_cumulative), color="royalblue1", size=3) + 
  geom_line(data = i2d, aes(x=i2d$bv,  y=i2d$bin_cumulative), size=2, color="tan1") +  
  geom_point(data = i2d, aes(x=i2d$bv, y=i2d$bin_cumulative), color="royalblue3", size=3) +
  scale_x_continuous(name="Brightness", breaks=seq(0,8,1)) +
  scale_y_continuous(name="Count", breaks=seq(0,12,1)) + 
  ggtitle("Combine plot of BV cumulative counts")

c_data_plot

我对R还是很陌生,非常感谢您的帮助.

I'm fairly new to R and would much appreciate any help.

对于每个注释,我已经编辑了代码以在将数据集加载到数据框中后重现该数据集.

Per comments, I've edited the code to reproduce the dataset after it's loaded into the dataframes.

关于生成单个数据帧,我将欢迎有关如何实现这一点的建议-我仍在努力解决数据帧的工作方式.

Regarding producing a single data frames, I'd welcome advice on how to achieve that - I'm still struggling with how data frames work.

推荐答案

首先,我们通过组合 i1d i2d 来组织数据.我添加了 data 列,用于存储原始数据集的名称.

First, we organize the data by combining i1d and i2d. I've added a column data which stores the name of the original dataset.

i1d$data <- 'i1d'
i2d$data <- 'i2d'
i12d <- rbind.data.frame(i1d, i2d)

然后,我们使用 ggplot2 更常见的语法创建图:

Then, we create the plot, using syntax that is more common to ggplot2:

ggplot(i12d, aes(x = bv, y = bin_cumulative))+
    geom_line(aes(colour = data), size = 2)+
    geom_point(colour = 'royalblue', size = 3)+
    scale_x_continuous(name="Brightness", breaks=seq(0,8,1)) +
    scale_y_continuous(name="Count", breaks=seq(0,12,1)) + 
    ggtitle("Combine plot of BV cumulative counts")+
    theme_bw()

如果我们在 ggplot 函数中指定 x y ,则无需继续在各种 geom中重写它我们要添加到绘图中.在前三行之后,我复制并粘贴了您的内容,以便格式符合您的期望.我还添加了 theme_bw ,因为我认为它在视觉上更具吸引力.我们还使用来自 data.frame

If we specify x and y within the ggplot function, we do not need to keep rewriting it in the various geoms we want to add to the plot. After the first three lines I copied and pasted what you had so that the formatting would match your expectation. I also added theme_bw, because I think it's more visually appealing. We also specify colour in aes using a variable (data) from our data.frame

如果要更进一步,可以使用 scale_colour_manual 函数指定归因于数据中 data 列的不同值的颜色.框架 i12d :

If we want to take this a step further, we can use the scale_colour_manual function to specify the colors attributed to the different values of the data column in the data.frame i12d:

ggplot(i12d, aes(x = bv, y = bin_cumulative))+
    geom_line(aes(colour = data), size = 2)+
    geom_point(colour = 'royalblue', size = 3)+
    scale_x_continuous(name="Brightness", breaks=seq(0,8,1)) +
    scale_y_continuous(name="Count", breaks=seq(0,12,1)) + 
    ggtitle("Combine plot of BV cumulative counts")+
    theme_bw()+
    scale_colour_manual(values = c('i1d' = 'turquoise',
                                   'i2d' = 'tan1'))

这篇关于使用ggplot向多个折线图添加图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆