如何用geom_ribbon绘图 [英] How to graph with geom_ribbon

查看:51
本文介绍了如何用geom_ribbon绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表:

Upper  Bound  
    Q      C   
    1     30  
    2     50  
    3     40

Lower  Bound      
    Q      C       
    1     10   
    2     15     
    3     20 

错误数据:

Q      C      Name  
1      50     Sample 1  
2      40     Sample 1  
3      30     Sample 1  
1      0      Sample 2  
2      60     Sample 2  
3      5      Sample 2

我想要一个图形,该图形以灰色绘制上下限,并填充其间的所有内容,并以不同的颜色和图例在顶部绘制不良样本的图形:

I want a graph that graphs the lower and upper bounds in gray and fills everything in between and graph the bad samples on top with different colors and a legend:

plot <- ggplot(Bad_Data, aes(x = Bad_Data$Q, y = Bad_Data$C, group = 1))
plot + geom_line(aes(color = N)) + geom_ribbon(aes(ymin = Lower_Bound$C, ymax = Upper_Bound$C))  

我尝试过,但是它给了我这个错误:

I tried that but it gave me this error:

错误:美学的长度必须为1或与数据相同(624):ymin,ymax,x,y,group

Error: Aesthetics must be either length 1 or the same as the data (624): ymin, ymax, x, y, group

有人可以帮助我吗?

推荐答案

这里是一个开始,您可以调整颜色和其他参数以完全根据需要获得动态效果.我添加了一些美学,并描述了每种美学的作用:

Here is a start, you can tweak the colors and other parameters to get the dynamics exactly as you like. I have added a few aesthetics with description of what each does:

#Prepare
Bad_Data$Lower_Bound <- Lower_Bound$C
Bad_Data$Upper_Bound <- Upper_Bound$C

#Plot
library(ggplot2)
p <- ggplot(Bad_Data, aes(x = Q, y = C, color=Name, group=Name))
p <- p + geom_line()
p + geom_ribbon(aes(ymin=Lower_Bound, ymax=Upper_Bound), 
                alpha=0.1,       #transparency
                linetype=1,      #solid, dashed or other line types
                colour="grey70", #border line color
                size=1,          #border line size
                fill="green")    #fill color

数据

以下是数据:

Upper_Bound <- read.table(text="Q      C   
1     30  
                          2     50  
                          3     40", header=T)

Lower_Bound <- read.table(text="Q      C       
                          1     10   
                          2     15     
                          3     20", header=T) 

Bad_Data <- read.table(text="Q      C      Name  
                       1      50     Sample1  
                       2      40     Sample1  
                       3      30     Sample1  
                       1      0      Sample2  
                       2      60     Sample2  
                       3      5      Sample2", header=T)

修改

更安全的准备:

Bad_Data$Lower_Bound <- Lower_Bound$C[match(Bad_Data$Q, Lower_Bound$Q)]
Bad_Data$Upper_Bound <- Upper_Bound$C[match(Bad_Data$Q, Upper_Bound$Q)]

这篇关于如何用geom_ribbon绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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