微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

R ggplot2:如果我使用数据框作为数据输入,如何添加图例?

如何解决R ggplot2:如果我使用数据框作为数据输入,如何添加图例?

我已经在线尝试了任何方法,但是图例不会自动显示。先前的一些问题说在ggplot2中使用数据帧不是可取的,但是我已经尝试过Together()方法,它也不起作用。如何修复错误? 这是数据:


    library(ggplot2)
    library(gtable)
    R = 0.01*c(7.000,6.800,6.620,6.460,6.330,6.250,6.200,6.160,6.125,6.100)
    maturity = c(1,2,3,4,5,6,7,8,9,10)
    c = seq(from=0,to=0,length.out = length(R))
    for (i in 1:length(R)){
      for (j in 1:i){
        c[i] = c[i] + 1/(1+R[j])^maturity[j]
      }
      c[i] = (1-1/(1+R[i])^maturity[i])/c[i]
    }
    Forward = seq(from=0,length.out = length(R))
    for (i in 1:length(R)){
      Forward[i] = (1+R[i+1])^2/(1+R[i])-1
    }

这是RMarkDown中的绘图代码


    ```{R fig.width=2.7559,fig.height=2.0669291}
    df = data.frame(maturity=maturity,ParYield=c,Forwardrate=Forward,R=R)
    p = ggplot(data=df,aes(x=maturity,y=ParYield)) + 
      ggtitle("Curve of Zero-coupon Yield,Par Yield,Forward Yield")+
      labs(x = "maturity (year)",y = "Par Yield") +
      scale_y_continuous(breaks=seq(100*(min(na.omit(Forward),c)-0.01),100*(max(na.omit(Forward),c)+0.01),by=0.1)/100,sec.axis = dup_axis(),labels = scales::number_format(accuracy = 0.005)) +
      scale_x_continuous("maturity",labels=as.character(maturity),breaks=maturity,sec.axis = dup_axis()) +
      theme_classic() +
      geom_line(aes(y=ParYield),size=1) + 
      geom_line(aes(y=R),size=1) + 
      geom_line(aes(y=Forwardrate),size=1) + 
      geom_point(aes(y=ParYield),shape=21,fill=rgb(69/255,117/255,180/255),size=3,stroke=1.5,color="black") +
      geom_point(aes(y=R),shape=22,fill=rgb(145/255,191/255,219/255),color="black") +
      geom_point(aes(y=Forwardrate),shape=23,fill=rgb(224/255,243/255,248/255),color="black") +
      theme(plot.title = element_text(size=14,hjust=0.5),text = element_text(size=15,colour = "black",family = "Calibri"),axis.ticks.length = unit(-0.25,'cm'),axis.line = element_line(size=1),axis.ticks = element_line(size=1),axis.text.x = element_text(margin = margin(t=15)),axis.text.x.top = element_text(margin = margin(b=15)),axis.text.y.right = element_text(margin = margin(l=15,r=5)),axis.text.y = element_text(margin = margin(l=5,r=15)),axis.title.x.top = element_blank(),axis.title.y.right = element_blank())+
            guides(colour=guide_legend(override.aes = list(pch=c(16,21,20),fill=c('r','r','r'))))+
            theme(legend.position = c(0.8,0.8),legend.justification = c("right","top"))
    p

解决方法

检查此草图并进行必要的调整,因为我不清楚必须如何设置颜色。请注意 @stefan 的建议,并修改下一个代码:

library(ggplot2)
#Code
df = data.frame(Maturity=Maturity,ParYield=c,ForwardRate=Forward,R=R)
ggplot(data=df,aes(x=Maturity,y=ParYield)) + 
  ggtitle("Curve of Zero-coupon Yield,Par Yield,Forward Yield")+
  labs(x = "Maturity (year)",y = "Par Yield") +
  scale_y_continuous(breaks=seq(100*(min(na.omit(Forward),c)-0.01),100*(max(na.omit(Forward),c)+0.01),by=0.1)/100,sec.axis = dup_axis(),labels = scales::number_format(accuracy = 0.005)) +
  scale_x_continuous("Maturity",labels=as.character(Maturity),breaks=Maturity,sec.axis = dup_axis()) +
  theme_classic() +
  geom_line(aes(y=ParYield),size=1) + 
  geom_line(aes(y=R),size=1) + 
  geom_line(aes(y=ForwardRate),size=1) + 
  geom_point(aes(y=ParYield,colour='ParYield'),shape=21,fill=rgb(69/255,117/255,180/255),size=3,stroke=1.5,show.legend = T) + #Black
  geom_point(aes(y=R,colour='R'),shape=22,fill=rgb(145/255,191/255,219/255),stroke=1.5) + #Black
  geom_point(aes(y=ForwardRate,colour='ForwardRate'),shape=23,fill=rgb(224/255,243/255,248/255),stroke=1.5) +
  theme(plot.title = element_text(size=14,hjust=0.5),text = element_text(size=15,colour = "black",family = "Calibri"),axis.ticks.length = unit(-0.25,'cm'),axis.line = element_line(size=1),axis.ticks = element_line(size=1),axis.text.x = element_text(margin = margin(t=15)),axis.text.x.top = element_text(margin = margin(b=15)),axis.text.y.right = element_text(margin = margin(l=15,r=5)),axis.text.y = element_text(margin = margin(l=5,r=15)),axis.title.x.top = element_blank(),axis.title.y.right = element_blank())+
  guides(colour=guide_legend(override.aes = list(pch=c(16,21,20))))+
  theme(legend.position = c(0.8,0.8),legend.justification = c("right","top"))+
  labs(color='Variable')

输出:

enter image description here

其他选项是:

#Code 2
df = data.frame(Maturity=Maturity,show.legend = T) +
  geom_point(aes(y=R,stroke=1.5) +
  geom_point(aes(y=ForwardRate,stroke=1.5) +
  scale_color_manual(values = c('black','black','black'))+
  theme(plot.title = element_text(size=14,"top"))+
  labs(color='Variable')

输出:

enter image description here

,

您需要将一些其他美观因素映射到数据中的变量。我通过将数据旋转为更长的格式来实现这一点。

kafka

然后,我只使用了一个library(tidyr) df2 <- df %>% pivot_longer(-Maturity,names_to = "Yield",values_to = "Rate") 和一个geom_line,但是将新的geom_point变量映射到了Yieldgroup和{{1 }}。现在,您可以使用fillshape来设置外观。

scale_shape_manual

enter image description here

我建议您重新考虑您的y轴标签。'

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?