如何添加图例以使用多个数据框中的数据进行绘图 [英] How to add legend to plot with data from multiple data frames

查看:138
本文介绍了如何添加图例以使用多个数据框中的数据进行绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个由两个单独的数据帧编译的ggplot脚本,但是就目前而言,由于aes中不包含颜色,因此没有图例.如果可能的话,我宁愿将两个数据集分开,但无法弄清楚如何添加图例.有什么想法吗?

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. I'd prefer to keep the two datasets separate if possible, but can't figure out how to add the legend. Any thoughts?

我尝试过将颜色直接添加到es函数中,但是然后颜色只是作为变量添加并在图例中列出,而不是对实际数据进行着色.

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.

在创建绘图之后,用基数r对此进行绘图:

Plotting this with base r, after creating the plot I would've used:

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

得到了我所需要的,但是我不确定如何在ggplot中复制它.

and gotten what I needed, but I'm not sure how to replicate this in ggplot.

下面的代码当前正好绘制了我想要的内容,只是缺少了图例...理想情况下,该图应该与上面的代码行匹配,除了"18"和"13"需要上标.

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.

使用基数r(具有正确的图例,但没有上标13和18的情况下)的旧图的示例,以及缺少图例的当前图的示例可在此处找到: 旧: https://imgur.com/xgd9e9C 新的缺少的图例: 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

背景数据

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

以下avarn函数产生一个具有三列和数千行的数据帧(请参见上面的标题).然后将它们随时间绘制在对数/对数图上.

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)

创建Allan偏差图

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

上面的图仅缺少图例以显示两个绘制的数据集的名称及其各自的颜色.我想在图的顶部居中放置图例.

如何为图例标题加上上标?:

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

推荐答案

aes内设置color并在绘图中添加scale_color_函数即可解决问题.

Set color inside the aes and add a scale_color_ function to your plot should do the trick.

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

这篇关于如何添加图例以使用多个数据框中的数据进行绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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