【问题标题】:Adding legends to multiple line plots with ggplot使用 ggplot 向多条线图添加图例
【发布时间】:2016-08-29 08:37:17
【问题描述】:

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

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

然后将这两个数据集绘制如下。显示很好,但我不知道如何在结果图中添加图例,因为 ggplot 对象本身似乎应该有一个数据源,但我不确定如何构建一个有多列相同的列名字。

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 的新手,非常感谢任何帮助。

根据 cmets,我已编辑代码以在数据集加载到数据帧后重现数据集。

关于生成单个数据框,我欢迎就如何实现这一点提出建议 - 我仍在为数据框的工作原理而苦苦挣扎。

【问题讨论】:

  • 请提供一个可重现的例子。
  • 乍一看,请考虑在美学调用中包含您的size 和/或color 选项。
  • @Cyrus Mohammadian 没错!您目前的“滥用” ggplot 因为您分开很多。尝试制作一个包含所有信息的数据框,然后在 ggplot() 语句中添加大部分信息,作为开始。
  • 这能回答你的问题吗? Add legend to ggplot2 line plot

标签: r ggplot2


【解决方案1】:

首先,我们通过组合i1di2d 来组织数据。我添加了一个列data,它存储了原始数据集的名称。

重组数据

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

然后,我们使用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 函数中指定xy,我们不需要在我们想要添加到绘图中的各种geoms 中不断重写它。在前三行之后,我复制并粘贴了您所拥有的内容,以便格式符合您的期望。我还添加了theme_bw,因为我认为它更具视觉吸引力。我们还使用来自data.frame 的变量(data)在aes 中指定colour

如果我们想更进一步,我们可以使用scale_colour_manual 函数来指定归因于data.frame i12ddata 列的不同值的颜色:

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() aes 调用 ( aes(x = bv, y = bin_cumulative) ) 中,x=bv 和 y=bin_cumulative 是否指示 R 在数据框中查找这些列?
  • @Sanger99 从某种意义上说,是的。查看this cheatsheet 了解更多ggplot2 信息
  • 好的,我会检查一下 - 再次感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2019-06-21
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-27
  • 1970-01-01
相关资源
最近更新 更多