我准备绘制示意图而不是数据可视化。因此,导入数据不是data.frame
。大多数行都是通过手动操作添加的(即geom_segment
,geom_vline
)。我发现很难显示线型图例或颜色图例。也许这与ggplot意识形态不符,但是这是一种在不合并到data.frame
的情况下添加奇异行的简便方法。
x <- 1:10
y <- 2:11
plt <- ggplot()+
geom_point(aes(x=x,y=y),shape=1,color='red',show.legend=TRUE)+
geom_line(aes(x=x,y=y),linetype=2,color='green')+
geom_segment(aes(x=Inf,y=y[3],xend=x[3],yend=y[3]),color='blue',linetype=5)+
geom_vline(xintercept=x[4],linetype=6,size=2)+
scale_linetype_manual(values=c(2,5,6),labels=c('a','b','c'))
答案 0 :(得分:1)
这是你的追求吗
library(ggplot2)
x <- 1:10
y <- 2:11
ggplot()+
geom_point(aes(x=x,y=y, color='red'),shape=1,show.legend=FALSE) +
geom_line(aes(x=x,y=y, linetype= "Line A"), color='green') +
geom_segment(aes(x=Inf,y=y[3],xend=x[3],yend=y[3],
linetype = "Line B"), color='blue')+
geom_segment(aes(x = 4, y= Inf, xend = 4, yend = -Inf,
linetype = "Line C"), size = 2) +
scale_linetype_manual(name = "Legend", values = c(2,5,6),
guide = guide_legend(override.aes =
list(color = c("green", "blue", "black"),
size = c(.5, .5, 2))))
创建:
使用答案<{How to add a legend to hline?>
我看不到不向所有图例行添加点的方法就将红色点添加到图例的方法。所以不是一个完整的答案。
可能不是很优雅的解决方法:
ggplot()+
geom_point(aes(x=x,y=y, linetype='APoints'),
colour = "Red", size = 2, shape=18) +
geom_line(aes(x=x,y=y, linetype= "Line A"), color='green') +
geom_segment(aes(x=Inf,y=y[3],xend=x[3],yend=y[3],
linetype = "Line B"), color='blue') +
geom_segment(aes(x = 4, y= Inf, xend = 4, yend = -Inf,
linetype = "Line C"), size = 2) +
scale_linetype_manual(name = "Legend", values = c(2,5,6,2),
guide = guide_legend(override.aes =
list(color = c("red", "green", "blue", "black"),
size = c(1, .5, .5, 2),
linetype = c("dotted", "dashed",
"dashed", "dotdash"))))
得到这个: