简体   繁体   English

如何使用来自多个数据帧的数据将图例添加到 plot

[英]How to add legend to plot with data from multiple data frames

I have scripted a ggplot compiled from two separate data frames, but as it stands there is no legend as the colours aren't included in aes.我编写了一个从两个单独的数据帧编译的 ggplot 脚本,但就目前而言,没有图例,因为颜色不包含在 aes 中。 I'd prefer to keep the two datasets separate if possible, but can't figure out how to add the legend.如果可能,我希望将两个数据集分开,但不知道如何添加图例。 Any thoughts?有什么想法吗?

I've tried adding the colours directly to the aes function, but then colours are just added as variables and listed in the legend instead of colouring the actual data.我尝试将颜色直接添加到 aes function,但随后颜色只是作为变量添加并列在图例中,而不是为实际数据着色。

Plotting this with base r, after creating the plot I would've used:在创建 plot 之后,我将使用基础 r 绘制此图:

legend("top",c("Delta 18O","Delta 13C"),fill=c("red","blue")

and gotten what I needed, but I'm not sure how to replicate this in ggplot.并得到了我需要的东西,但我不确定如何在 ggplot 中复制它。

The following code currently plots exactly what I want, it's just missing the legend... which ideally should match what the above line would produce, except the "18" and "13" need superscripted.以下代码当前准确地绘制了我想要的图,它只是缺少图例......理想情况下应该与上面的行将产生的内容相匹配,除了“18”和“13”需要上标。

Examples of an old plot using base r (with a correct legend, except lacking superscripted 13 and 18) and the current plot missing the legend can be found here: Old: https://imgur.com/xgd9e9C New, missing legend: https://imgur.com/eGRhUzf Examples of an old plot using base r (with a correct legend, except lacking superscripted 13 and 18) and the current plot missing the legend can be found here: Old: https://imgur.com/xgd9e9C New, missing legend: https ://imgur.com/eGRhUzf

Background data后台数据

head(avar.data.x)
      time          av       error
1 1.015223 0.030233604 0.003726832
2 2.030445 0.014819145 0.005270609
3 3.045668 0.010054801 0.006455241
4 4.060891 0.007477541 0.007453974
5 5.076113 0.006178282 0.008333912
6 6.091336 0.004949045 0.009129470
head(avar.data.y)
      time         av       error
1 1.015223 0.06810001 0.003726832
2 2.030445 0.03408136 0.005270609
3 3.045668 0.02313839 0.006455241
4 4.060891 0.01737148 0.007453974
5 5.076113 0.01405144 0.008333912
6 6.091336 0.01172788 0.009129470

The following avarn function produces a data frame with three columns and several thousand rows (see header above).下面的 avarn function 生成一个三列几千行的数据帧(参见上面的 header)。 These are then graphed over time on a log/log plot.然后将这些随时间绘制在日志/日志 plot 上。

avar.data.x <- avarn(data3$"d Intl. Std:d 13C VPDB - Value",frequency)

avar.data.y <- avarn(data3$"d Intl. Std:d 18O VPDB-CO2 - Value",frequency)

Create allan deviation plot创建艾伦偏差 plot

ggplot()+
      geom_line(data=avar.data.y,aes(x=time,y=sqrt(av)),color="red")+
      geom_line(data=avar.data.x,aes(x=time,y=sqrt(av)),color="blue")+
      scale_x_log10()+
      scale_y_log10()+
      labs(x=expression(paste("Averaging Time ",tau," (seconds)")),y="Allan Deviation (per mil)")

The above plot is only missing a legend to show the name of the two plotted datasets and their respective colours.上面的 plot 只缺少一个图例来显示两个绘制数据集的名称及其各自的颜色。 I would like the legend in the top centre of the graph.我想要图表顶部中心的图例。

How to superscript legend titles?:如何上标图例标题?:

ggplot()+
  geom_line(data=avar.data.y,aes(x=time,y=sqrt(av), 
color =expression(paste("Delta ",18^,"O"))))+
  geom_line(data=avar.data.xmod,aes(x=time,y=sqrt(av), 
color=expression(paste("Delta ",13^,"C"))))+
  scale_color_manual(values = c("blue", "red"),name=NULL) +
  scale_x_log10()+
  scale_y_log10()+
  labs(
    x=expression(paste("Averaging Time ",tau," (seconds)")),
    y="Allan Deviation (per mil)") + 
  theme(legend.position = c(0.5, 0.9))

Set color inside the aes and add a scale_color_ function to your plot should do the trick.aes中设置color并将scale_color_ function 添加到您的 plot 应该可以解决问题。

ggplot()+
  geom_line(data=avar.data.y,aes(x=time,y=sqrt(av), color = "a"))+
  geom_line(data=avar.data.x,aes(x=time,y=sqrt(av), color="b"))+
  scale_color_manual(
    values = c("red", "blue"),
    labels = expression(avar.data.x^2, "b")
  ) +
  scale_x_log10()+
  scale_y_log10()+
  labs(
    x=expression(paste("Averaging^2 Time ",tau," (seconds)")),
    y="Allan Deviation (per mil)") + 
  theme(legend.position = c(0.5, 0.9))

@z-cool merits the accepted answer. @z-cool 值得接受的答案。 However, the current approaches do not use ggplots amazing aesthetics.然而,目前的做法并没有使用 ggplots 惊人的美学。 Your data frames seem all to have the same structure.您的数据框似乎都具有相同的结构。 Now, this in mind, a more ggplot like way would be to make one single long data frame and use color (or any aesthetic) like so:现在,考虑到这一点,一种更像 ggplot 的方式是制作一个长数据框并使用color (或任何美学),如下所示:

avar.data.x <- readr::read_table("0 time          av       error
1 1.015223 0.030233604 0.003726832
2 2.030445 0.014819145 0.005270609
3 3.045668 0.010054801 0.006455241
4 4.060891 0.007477541 0.007453974
5 5.076113 0.006178282 0.008333912
6 6.091336 0.004949045 0.009129470") 
avar.data.y <- readr::read_table("0 time         av       error
1 1.015223 0.06810001 0.003726832
2 2.030445 0.03408136 0.005270609
3 3.045668 0.02313839 0.006455241
4 4.060891 0.01737148 0.007453974
5 5.076113 0.01405144 0.008333912
6 6.091336 0.01172788 0.009129470")

library(tidyverse)

combine_df <- bind_rows(list(a = avar.data.x, b = avar.data.y), .id = 'ID')

ggplot(combine_df)+
  geom_line(aes(x = time, y = sqrt(av), color = ID))+
  scale_color_manual(values = c("red", "blue"),
    labels = c(expression("Delta 18"^"O"), expression("Delta 13"^"C"))) 

Created on 2019-11-11 by the reprex package (v0.2.1)代表 package (v0.2.1) 于 2019 年 11 月 11 日创建

This gives only one call to geom_line and easier and better control of the legend(s).这只需要一次调用geom_line并且更容易更好地控制图例。 You could even make some fancy function to automate your labels.您甚至可以制作一些精美的 function 来自动化您的标签。 etc.等等

Also note that white spaces in column names are not great (you're making your own life very difficult) and that you may want to think about automating your avarn calls, eg with lapply , which would result in a list of data frames and makes the binding of the data frames even easier.另请注意,列名中的空格不是很好(您让自己的生活变得非常困难),并且您可能想要考虑自动化您的avarn调用,例如使用lapply ,这将导致数据框列表并使数据帧的绑定更加容易。

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

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