[英]How to connect points of different groups by a line using 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))
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()
在上图中,注意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
由于我在原始问题中没有提到这一点,我会选择一个对原始数据有效的答案。
更新以最小化对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))
这是一个简单的解决方案。 无需更改原始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功能之一。 你可以将你的情节一个在另一个上面。
结果如下:
我提出了一个结合了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))
我想提出一个不需要修改数据帧的解决方案,这是直观的(一旦你考虑如何绘制图层),并且不涉及行相互覆盖。 但是,它有一个问题:它不允许您修改线型。 我不知道为什么会这样,所以如果有人能够启发我们,那就太好了。
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的基本绘图在geom_point(aes())
和geom_line(aes())
内按category
对数据进行分组,在此应用程序中,不希望的结果是该行不“桥接”这两个点跨越这两个类别。
# 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)
我的解决方案的关键是按variable
分组,但在geom_line(aes())
内按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)
然而,遗憾的是,根据这种方法,目前我无法控制线型,据我所知:
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
备注:我正在使用另一个数据帧,因为我正在从我正在做的事情中复制粘贴,这让我访问了这个问题 - 这样我就可以上传我的图像了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.