如何在ggplot中使用多条不同长度的行来格式化行大小?

问题描述

@H_404_0@我能够正确绘制图形,但是我想增加线条尺寸以使图形更易读。当我在geom_line内尝试调整大小时,我的线条变得超级胖。 我在数据框“ data”中有三个时间序列变量(x,y,z),我想在y轴上绘制这些变量,它们的长度不同,这意味着绘制在不同的时间开始。 如何在不增大线条的情况下更改线条的大小?

P_comp <- ggplot(data,aes(x=Date))+
  geom_line(aes(y = x,colour = "green"))+
  geom_line(aes(y = y,colour = "darkred"))+
  geom_line(aes(y = z,colour = "steelblue"))+
  theme_ipsum()+
  theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())+
  theme(text = element_text(family = "serif"))+
  xlab("Time") + ylab("Value") +
  ggtitle("EPU Indices")+
  theme(plot.title = element_text(hjust = 0.5,family = "serif",face = "plain",size = 16))+
  theme(axis.title.x = element_text(hjust = 0.5,size = 12,face = "plain"))+
  theme(axis.title.y = element_text(hjust = 0.5,face = "plain"))
P_comp
@H_404_0@

This is the plot without using size argument

@H_404_0@

This is the plot when inputting size= 1 in one of the geom_lines

解决方法

您的代码段未在此处显示,但这听起来像是您在size = 1语句中设置了aes()。这样会添加一个尺寸美感,称为“ 1”,并自动为其指定尺寸。

请尝试以下操作:geom_line(aes(y = x,colour = "green"),size = 1)

,

可以使用scale_size_*比例尺之一设置线宽。在下面的示例中,我将使用scale_size_manual
每行分类变量"group的行大小将设置为一个值。

在第一个示例中,将行大小设置为值1:3,使行变粗。

library(ggplot2)

ggplot(df1,aes(Date,y,color = group)) +
  geom_line(aes(size = group)) +
  scale_size_manual(values = 1:3) +
  theme_bw()

enter image description here

现在使线条更细。其余的情节是相同的。

ggplot(df1,color = group)) +
  geom_line(aes(size = group)) +
  scale_size_manual(values = (1:3)/5) +
  theme_bw()

enter image description here

数据

df1 <- iris[4:5]
df1$Date <- rep(seq(Sys.Date() - 49,Sys.Date(),by = "day"),3)
names(df1)[1:2] <- c("y","group")