首页 > 解决方案 > 如何在ggplot中手动创建线型?

问题描述

我有以下数据框,我想绘制它:

df = data.frame(
  a = 1:2,
  b = 1:2)

p = ggplot(df, aes(a,b)) 
p + geom_line()

在此处输入图像描述

这很好,但我希望能够设置不同类型的线型,因为我有几个组。我访问这个网站: http: //www.sthda.com/english/wiki/ggplot2-line-types-how-to-change-line-types-of-a-graph-in-r-software#change-manually -外观线条。我试过了

p + geom_line(linetype = 'dashed')

在此处输入图像描述

然而,我想要一种完全按照我想要的方式构建生产线的方法(即,不是来自现成的模板)。例如,由长行和长空白组成的行。任何想法?

标签: rggplot2graph

解决方案


您可以通过为 geom 函数提供十六进制字符串来指定您希望在线和离线的时间。从文档:

# An example with hex strings, the string "33" specifies three units on followed
# by three off and "3313" specifies three units on followed by three off followed
# by one on and finally three off.
f + geom_line(linetype = "3313")

推荐阅读