【问题标题】:Facet plots and line graphs: how to plot and disconnect gaps in lines with groups刻面图和折线图:如何绘制和断开与组的线中的间隙
【发布时间】:2018-08-08 14:14:51
【问题描述】:

更新以解释这不是重复的问题。

我想删除连接图 1 中各组线之间的“数据间隙”的线:要修复的刻面图,如下。我正在尝试重新创建图片 2。(图片 2 中连接原始数据集和校正数据集的线条很好,但不需要)。

分面图显示了从同一数据记录器记录的两个参数的值。每个参数都有一个原始值数据集和一个校正数据集,其中错误值被删除(因此总共 4 个数据集)。我将它们全部与gather() 结合以制作刻面图并添加列类型(原始或更正)。

我已经读到保持 NA 会阻止折线图绘制一个点。为此,我似乎需要 4 种参数类型(参数 1 原始,参数 1 已校正,参数 2 原始,参数 2 已校正)。但是,一旦我这样做了,我不知道如何制作一个刻面图,其中参数 1 原始和校正参数 1 一起绘制在顶部图上,参数 2 原始和参数 2 校正在刻面的底部图上。

链接1:Connecting across missing values with geom_line

绘图组:https://www3.nd.edu/~steve/computing_with_data/13_Facets/facets.html

链接 2:geom_line - different colour in the same line 我了解如何将不同的颜色应用到同一行(至少一种方式 - 请参见下面的示例代码)。 这个答案没有解释如何在分面图中做到这一点。我已经尝试了 aes() 放置和组,但还没有找到一个解决方案,它允许两个平面图,每个平面图都有两个不连接组之间的线的线分组。我需要为每个构面图单独调用 geom_line() 吗(这可能吗)?

要修复的分面图:线具有分组值,并且组之间的间隙是连接的。

我希望我的情节看起来像的示例

这是一个示例数据集:

df<- data.frame(DateTime=c("08/29/2011 00:00", "08/29/2011 01:00", "08/29/2011 02:00", "08/29/2011 03:00", "08/29/2011 04:00", "08/29/2011 05:00", 
                        "08/29/2011 06:00", "08/29/2011 07:00", "08/29/2011 08:00", "08/29/2011 09:00", "08/29/2011 10:00", "08/29/2011 11:00",
                        "08/29/2011 12:00", "08/29/2011 13:00", "08/29/2011 14:00", "08/29/2011 15:00", "08/29/2011 16:00", "08/29/2011 17:00",
                        "08/29/2011 18:00", "08/29/2011 19:00", "08/29/2011 20:00", "08/29/2011 21:00", "08/29/2011 22:00", "08/29/2011 23:00", 
                        "08/30/2011 00:00", "08/29/2011 00:00", "08/29/2011 01:00", "08/29/2011 02:00", "08/29/2011 03:00", "08/29/2011 04:00", "08/29/2011 05:00", 
                        "08/29/2011 06:00", "08/29/2011 07:00", "08/29/2011 08:00", "08/29/2011 09:00", "08/29/2011 10:00", "08/29/2011 11:00",
                        "08/29/2011 12:00", "08/29/2011 13:00", "08/29/2011 14:00", "08/29/2011 15:00", "08/29/2011 16:00", "08/29/2011 17:00",
                        "08/29/2011 18:00", "08/29/2011 19:00", "08/29/2011 20:00", "08/29/2011 21:00", "08/29/2011 22:00", "08/29/2011 23:00", 
                        "08/30/2011 00:00"),
             Type=c("Corrected", "Corrected", "Raw", "Raw", "Corrected", "Corrected", "Corrected", "Corrected",
                    "Raw", "Raw","Raw","Raw","Raw","Raw", "Corrected", "Corrected",
                    "Corrected", "Corrected","Raw","Raw","Corrected","Corrected","Corrected", "Corrected",
                    "Raw", "Corrected", "Corrected", "Raw", "Raw", "Corrected", "Corrected", "Corrected", "Corrected",
                    "Raw", "Raw","Raw","Raw","Raw","Raw", "Corrected", "Corrected",
                    "Corrected", "Corrected","Raw","Raw","Corrected","Corrected","Corrected", "Corrected",
                    "Raw"))
df$DateTime<-strptime(df$DateTime,"%m/%d/%Y %H:%M")
df$DateTime<-as.POSIXct(df$DateTime, tz="EST") 
df$Parameter[1:25]<-"Par1"
df$Parameter[26:50]<-"Par2"
df$Value<-sample(c(11:60))
df$Value<-ifelse(df$Type=="Raw", 1, df$Value)

这是我的图表:

df %>% ggplot(aes(DateTime, Value, color=Type))+
 geom_point()+
 geom_line()+
 theme_bw()+
 facet_grid(Parameter ~., scales="free")+ 
 scale_color_manual(values=c("#CC79A7","#000000")) + 
 labs(x="Date-Time")+
 theme(text=element_text(family="serif"), 
    strip.text.y=element_text(face="bold"), strip.background = element_rect(fill=NA, colour="black"),
    axis.text=element_text(color="#000000"), axis.title=element_text(face="bold"))

【问题讨论】:

  • 感谢您的链接,已编辑以解释它如何不是重复的问题。
  • 试试这个:df %>% ggplot(aes(DateTime, Value, color=(Type == "Corrected")))+ geom_point()+ geom_line(aes(group = Parameter))+ theme_bw()+ facet_grid(Parameter ~., scales="free")+ scale_color_manual(values=c("#CC79A7","#000000")) + labs(x="Date-Time")+ theme(text= element_text(family="serif"), strip.text.y=element_text(face="bold"), strip.background = element_rect(fill=NA, colour="black"), axis.text=element_text(color=" #000000"),axis.title=element_text(face="bold"))
  • 谢谢!!这正是我一直在寻找的。之后只需要手动修复图例。你能解释一下颜色 = (Type=="Corrected"))。 color=Type 是如何导致数据间隙连接的?

标签: r ggplot2 facet-grid aesthetics


【解决方案1】:

我的评论实际上比它需要的更复杂。您所要做的就是将group 美学添加到geom_line。通过不添加Type == Corrected,您不必手动更改您的图例。

作为answer quotes,Hadley 解释了原因:

重要的是[对于水平因子的折线图 axis]是手动指定分组。默认情况下 ggplot2 使用 图中所有分类变量的组合以对几何图形进行分组 - 这对这个情节不起作用,因为你得到了一条单独的线 每个点。手动指定 group = 1 表示你想要一个 连接所有点的线。

这是你的例子:

df %>% ggplot(aes(DateTime, Value, color=Type))+
        geom_point()+
        geom_line(aes(group = Parameter))+
        theme_bw()+
        facet_grid(Parameter ~., scales="free")+ 
        scale_color_manual(values=c("#CC79A7","#000000")) + 
        labs(x="Date-Time")+
        theme(text=element_text(family="serif"), 
              strip.text.y=element_text(face="bold"), strip.background = element_rect(fill=NA, colour="black"),
              axis.text=element_text(color="#000000"), axis.title=element_text(face="bold"))

【讨论】:

    猜你喜欢
    • 2020-06-08
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    相关资源
    最近更新 更多