分面图和折线图:如何绘制和断开与组之间的线间隙
Facet plots and line graphs: how to plot and disconnect gaps in lines with groups
已更新以解释为什么这不是一个重复的问题。
我想删除连接线组之间 "data gaps" 的线,如下图 1:要修复的小平面图。我正在尝试重新创建图片 2。(图片 2 中连接原始数据集和更正数据集的线条很好,但不需要)。
小平面图显示了从同一数据记录器记录的两个参数的值。每个参数都有一个原始值数据集和一个校正后的数据集,其中删除了错误值(因此总共有 4 个数据集)。我将它们全部与 gather() 结合起来以制作构面图并添加了列类型(原始或更正)。
我读到保留 NA 会阻止折线图绘制点。为此,我似乎需要 4 种参数类型(原始参数 1、已更正参数 1、原始参数 2、已更正参数 2)。但是,一旦我这样做了,我就不知道如何制作构面图,其中参数 1 raw 和参数 1 corrected 一起绘制在顶部图表上,参数 2 raw 和参数 2 corrected 位于 facet 的底部图表上。
Link1: Connecting across missing values with geom_line
绘图组:https://www3.nd.edu/~steve/computing_with_data/13_Facets/facets.html
Link 2: geom_line - different colour in the same line
我了解如何将不同的颜色应用于同一行(至少一种方式——参见下面的示例代码)。
这个答案没有解释如何在 facet plot 中做到这一点。我已经尝试过 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"))
我的评论实际上比需要的更复杂。您所要做的就是为 geom_line
添加 group
美学。通过不添加 Type == Corrected
,您将不必手动更改图例。
如此 answer quotes,Hadley 解释原因:
The important thing [for a line graph with a factor on the horizontal
axis] is to manually specify the grouping. By default ggplot2 uses the
combination of all categorical variables in the plot to group geoms -
that doesn't work for this plot because you get an individual line for
each point. Manually specify group = 1 indicates you want a single
line connecting all the points.
这是你的例子:
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"))
已更新以解释为什么这不是一个重复的问题。
我想删除连接线组之间 "data gaps" 的线,如下图 1:要修复的小平面图。我正在尝试重新创建图片 2。(图片 2 中连接原始数据集和更正数据集的线条很好,但不需要)。
小平面图显示了从同一数据记录器记录的两个参数的值。每个参数都有一个原始值数据集和一个校正后的数据集,其中删除了错误值(因此总共有 4 个数据集)。我将它们全部与 gather() 结合起来以制作构面图并添加了列类型(原始或更正)。
我读到保留 NA 会阻止折线图绘制点。为此,我似乎需要 4 种参数类型(原始参数 1、已更正参数 1、原始参数 2、已更正参数 2)。但是,一旦我这样做了,我就不知道如何制作构面图,其中参数 1 raw 和参数 1 corrected 一起绘制在顶部图表上,参数 2 raw 和参数 2 corrected 位于 facet 的底部图表上。
Link1: Connecting across missing values with geom_line
绘图组:https://www3.nd.edu/~steve/computing_with_data/13_Facets/facets.html
Link 2: geom_line - different colour in the same line 我了解如何将不同的颜色应用于同一行(至少一种方式——参见下面的示例代码)。 这个答案没有解释如何在 facet plot 中做到这一点。我已经尝试过 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"))
我的评论实际上比需要的更复杂。您所要做的就是为 geom_line
添加 group
美学。通过不添加 Type == Corrected
,您将不必手动更改图例。
如此 answer quotes,Hadley 解释原因:
The important thing [for a line graph with a factor on the horizontal axis] is to manually specify the grouping. By default ggplot2 uses the combination of all categorical variables in the plot to group geoms - that doesn't work for this plot because you get an individual line for each point. Manually specify group = 1 indicates you want a single line connecting all the points.
这是你的例子:
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"))