使用facet_wrap和ggplot2绘制每个方面内的整个数据 [英] plotting the whole data within each facet using facet_wrap and ggplot2

查看:130
本文介绍了使用facet_wrap和ggplot2绘制每个方面内的整个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为每个数据集绘制线图和 facet_wrap 。我喜欢的是浅灰色,透明或者其他东西,所有数据集都在后台。

  df < - 数据.frame(id = rep(letters [1:5],each = 10),
x = seq(10),
y = runif(50))

ggplot(df ,aes(x,y,group = id))+
geom_line()+
facet_wrap(〜id)



这张图是我得到多少,但我很乐意拥有所有另一个在每个图表中缺少4行...以任何方式我尝试使用 facet_wrap ,我只得到单行的数据。



我期望的是每个方面都是如此。

  ggplot(df, aes(x,y,group = id))+ 
geom_line()+
geom_line(data = df [1:10,],aes(x,y,group = id),size = 5 )

解决方案

以下是另一种方法:

首先添加一个与id相同的新列:

  df $ id2 < -  df $ id 

然后根据没有原始id列的df添加另一个 geom_line

  ggplot(df,aes(x,y,group = id))+ 
geom_line(data = df [ 2:4],aes(x = x,y = y,group = id2),color =gray)+
geom_line()+
facet_wrap(〜id)


I am trying to plot line graphs for and facet_wrap for each dataset. What I would love to have is in light grey, transparent or something, all datasets in the background.

df <- data.frame(id=rep(letters[1:5], each=10),
                 x=seq(10),
                 y=runif(50))

ggplot(df, aes(x,y, group=id)) + 
  geom_line() + 
  facet_wrap(~ id)

This graph is how far I get, but I would love to have all the other missing 4 lines in each graph as well... In any way I try to use facet_wrap, I get only the data of a single line.

What I would expect is something like this for each facet.

ggplot(df, aes(x,y, group=id)) + 
  geom_line() + 
  geom_line(data=df[1:10,], aes(x,y, group=id), size=5)

解决方案

Here's another approach:

First add a new column identical to id:

df$id2 <- df$id

Then add another geom_line based on the df without the original id column:

ggplot(df, aes(x,y, group=id)) + 
  geom_line(data=df[,2:4], aes(x=x, y=y, group=id2), colour="grey") +
  geom_line() + 
  facet_wrap(~ id)

这篇关于使用facet_wrap和ggplot2绘制每个方面内的整个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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