如何在ggplot中手动创建线型?

问题描述 投票:0回答:1

我有以下数据框,我想绘制。

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

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

enter image description here

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

p + geom_line(linetype = 'dashed')

enter image description here

然而,我希望有一种方法可以完全按照我的要求来构建线条(即,不是从现成的模板中构建)。例如,由长线和长空组成的线。有什么办法吗?

r ggplot2 graph
1个回答
1
投票

你可以通过给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")

0
投票

我认为你最好的选择是使用美学上的 geom_line.

你可以随心所欲地定制它们。看一下文件 . 选项包括 x,y,alpha,colour,group,linetype,size.

您还可以 自制 geom如果你真的想自己建立它们。


0
投票

解决方法是在geom_line()的参数linetype中提供一个数字对的序列,具体来说,在每一个数字对中,第一个数字是实线部分的长度,第二个数字是空白部分的长度。具体来说,在每一对数字中,第一个数字指定线的实线部分的长度,第二个数字指定线的空白部分的长度。因此,例如,'11'意味着一个单位的线和一个单位的空白。同样,'22'意味着2个单位的实线和2个单位的空白。或者,'48'意味着4个单位的线条和8个单位的空白。

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

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

enter image description here

但线型可以更详细。比如说

p + geom_line(linetype = '48')

这里,我们做了一条由4个单位的线,8个空白,2个单位的实线和2个单位的空白组成的线。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.