如何控制geom_line中的线条粗细

时间:2021-01-27 03:19:42

标签: r

我想控制绘图中线条的粗细,但是遇到了一些困难。似乎如果我在 size=0.06 中添加 size=2geom_line(),它并没有真正将线条粗细更改为不同的大小。它还在输出中添加了奇怪的图例。我该如何解决?

我用来绘图的代码是:

ggplot(data =df)+
       geom_line(aes(x = ADY, y = AVAL, color = PARAMCD, yaxs="d", xaxs="d", size=0.06))+
       geom_point(aes(x = ADY, y = AVAL))+
       scale_color_discrete(breaks=c("SYSBP", "DIABP", "PULSE"),name = "Vital signs", labels = c("Systolic BP", "Diastolic BP",  "Pulse"))+
       scale_colour_manual(values=c(DIABP="#512d69",SYSBP="#007254",PULSE="#fd9300"))

size=0.06size =2 的输出为:

enter image description here

enter image description here

有人可以给我一些指导吗?我不想在图例上显示尺寸,我想控制线条的粗细。谢谢。

可以使用代码构建示例数据:

df<- structure(list(ADY = c(-6, -6, -6, 1, 1, 1, 8, 8, 8, 15, 15, 
15, 22, 22, 22, 29, 29, 29, 43, 43, 43, 57, 57, 57, 64, 87, 87, 
87, 101, 101, 101), AVAL = c(66, 67, 127, 70, 58, 136, 68, 74, 
140, 145, 74, 58, 75, 72, 149, 82, 66, 143, 86, 60, 159, 64, 
87, 136, NA, 73, 58, 135, 141, 74, 74), PARAMCD = structure(c(3L, 
1L, 2L, 1L, 3L, 2L, 3L, 1L, 2L, 2L, 1L, 3L, 1L, 3L, 2L, 1L, 3L, 
2L, 1L, 3L, 2L, 3L, 1L, 2L, NA, 1L, 3L, 2L, 2L, 1L, 3L), .Label = c("DIABP", 
"SYSBP", "PULSE"), class = "factor")), row.names = c(NA, -31L
), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

对于您的情况,

size 应该在 aes 之外:

您可以看到 size = 0.06size = 2 之间的区别。

library(ggplot2)

ggplot(data =df)+
  geom_line(aes(x = ADY, y = AVAL, color = PARAMCD, yaxs="d", xaxs="d"), size=0.06) +
  geom_point(aes(x = ADY, y = AVAL))+
  scale_colour_manual(values=c(DIABP="#512d69",SYSBP="#007254",PULSE="#fd9300"))

enter image description here

ggplot(data =df)+
  geom_line(aes(x = ADY, y = AVAL, color = PARAMCD, yaxs="d", xaxs="d"),  size=2) +
  geom_point(aes(x = ADY, y = AVAL))+
  scale_colour_manual(values=c(DIABP="#512d69",SYSBP="#007254",PULSE="#fd9300"))

enter image description here