如何使用ggplot通过一行连接不同组的点

时间:2013-11-01 17:11:27

标签: r ggplot2

df<-data.frame(adjuster=c("Mary","Mary","Bob","Bob"), date=as.Date(c("2012-1-1","2012-2-1","2012-3-1","2012-4-1")), value=c(10,15,25,15))
df
  adjuster       date value
1     Mary 2012-01-01    10
2     Mary 2012-02-01    15
3      Bob 2012-03-01    25
4      Bob 2012-04-01    15

ggplot(df,aes(x=date,y=value,color=adjuster))+geom_line()+geom_point()

enter image description here

在上图中,注意2月和3月点之间的脱节。如何用蓝线连接这些点,使实际的March点变红?换句话说,Bob应该与[Jan-Mar]和[Mar-Apr]的Mary相关联。

编辑:事实证明我的例子过于简单。列出的答案并不适用于调整者不止一次在两个人之间变化的情况。例如,考虑

df<-data.frame(adjuster=c("Mary","Mary","Bob","Bob","Mary"), date=as.Date(c("2012-1-1","2012-2-1","2012-3-1","2012-4-1","2012-5-1")), value=c(10,15,25,15,20))
      adjuster       date value
1     Mary 2012-01-01    10
2     Mary 2012-02-01    15
3      Bob 2012-03-01    25
4      Bob 2012-04-01    15
5     Mary 2012-05-01    20

由于我在原始问题中未提及此问题,因此我将选择一个仅适用于原始数据的答案。

4 个答案:

答案 0 :(得分:5)

更新以最小化对data.frame的修补,添加了group = 1参数

稍微调整一下您的data.frame。我想你应该能够自动修补。如果你不是,请告诉我。此外,您的ggplot命令无法按照您在问题中发布的图表

运行
df<-data.frame(
  adjuster=c("Mary","Mary","Bob","Bob"), 
  date=as.Date(c("2012-1-1","2012-2-1","2012-3-1","2012-4-1")), 
  value=c(10,15,25,15)
)

library(data.table)
library(ggplot2)
dt <- data.table(df)
dt[,adjuster := as.character(adjuster)]
dt[,prevadjuster := c(NA,head(adjuster,-1))]
dt[is.na(prevadjuster),prevadjuster := adjuster]


ggplot(dt) +
geom_line(aes(x=date,y=value, color = prevadjuster, group = 1)) +
geom_line(aes(x=date,y=value, color = adjuster, group = 1)) +
geom_point(aes(x=date,y=value, color = adjuster, group = 1))

答案 1 :(得分:1)

这是一个简单的解决方案。无需更改原始data.frame。

ggplot()+
geom_line(aes_string(x='date',y='value'), data=df, lty=2)+
geom_point(aes_string(x='date',y='value', color='adjuster'), data=df)+
geom_line(aes_string(x='date',y='value', color='adjuster'), data=df)

这是我最喜欢的ggplot功能之一。你可以很好地将你的情节一个在另一个上面。

结果如下: enter image description here

答案 2 :(得分:1)

我想出了一个结合了Codoremifa和JAponte的想法的解决方案。

df<-data.frame(adjuster=c("Mary","Mary","Bob","Bob"), date=as.Date(c("2012-1-1","2012-2-1","2012-3-1","2012-4-1")), value=c(10,15,25,15))
df$AdjusterLine<-df$adjuster
df[2:nrow(df),]$AdjusterLine<-df[1:(nrow(df)-1),]$adjuster
ggplot(df)+geom_line(aes(x=date,y=value, color=AdjusterLine), lty=2)+geom_line(aes(x=date,y=value, color=adjuster))+geom_point(aes(x=date,y=value, color=adjuster))

enter image description here

答案 3 :(得分:1)

我想提出一个不需要修改数据框的解决方案,这是直观的(一旦你考虑如何绘制图层),并且不涉及行相互覆盖。但是,它有一个问题:它不允许您修改线型。我不知道为什么会这样,所以如果有人能够启发我们,那就太好了。

对OP的快速回答:

ggplot(df, aes(x = date, y = value, color = adjuster))+
    geom_line(aes(group = 1, colour = adjuster))+
    geom_point(aes(group = adjuster, color = adjuster, shape = adjuster))

在OP的数据框中,可以使用group=1创建一个跨越整个时期的群组。

用数字说明的一个例子:

# Create data
df <- structure(list(year = c(1990, 2000, 2010, 2020, 2030, 2040), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "Something", class = "factor"), 
    value = c(4, 5, 6, 7, 8, 9), category = structure(c(1L, 1L, 1L, 
    2L, 2L, 2L), .Label = c("Observed", "Projected"), class = "factor")), .Names = c("year", 
"variable", "value", "category"), row.names = c(NA, 6L), class = "data.frame")

# Load library
library(ggplot2)

基本图与OP类似,在category内和geom_point(aes())geom_line(aes())分组数据,在此应用程序中,不良结果表明该行不是& #39;桥&#39;两个类别中的两点。

# Basic ggplot with geom_point() and geom_line()
p <- ggplot(data = df, aes(x = year, y = value, group = category)) + 
    geom_point(aes(colour = category, shape = category), size = 4) +
    geom_line(aes(colour = category), size = 1)
ggsave(p, file = "ggplot-points-connect_p1.png", width = 10, height = 10)

enter image description here

我的解决方案的关键是按variable进行分组,但要category 内的geom_line(aes())颜色

# Modified version to connect the dots "continuously" while preserving color grouping
p <- ggplot(data = df, aes(x = year, y = value)) + 
    geom_point(aes(group = category, colour = category, shape = category), size = 4) +
    geom_line(aes(group = variable, colour = category), size = 1)
ggsave(p, file = "ggplot-points-connect_p2.png", width = 10, height = 10)

enter image description here

然而,遗憾的是,根据这种方法,目前无法控制线型,据我所知:

ggplot(data = df, aes(x = year, y = value)) + 
    geom_point(aes(group = category, colour = category, shape = category), size = 4) +
    geom_line(aes(group = variable, colour = category), linetype = "dotted", size = 1)
## Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line

备注:我正在使用其他数据框,因为我从我正在做的事情中复制粘贴,这让我访问了这个问题 - 这样我就可以上传我的图片了。