如何在R中平滑地(不拟合)绘制折线图?

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

这是我的数据集

df=data.frame(
  treatment = rep(c("B", "A"), each = 13L),
  time = as.POSIXct(
    rep(
      c(
        "1899-12-31 06:00:00", "1899-12-31 07:00:00", "1899-12-31 08:00:00",
        "1899-12-31 09:00:00", "1899-12-31 10:00:00", "1899-12-31 11:00:00",
        "1899-12-31 12:00:00", "1899-12-31 13:00:00", "1899-12-31 14:00:00",
        "1899-12-31 15:00:00", "1899-12-31 16:00:00", "1899-12-31 17:00:00",
        "1899-12-31 18:00:00"
      ),
      2
    ),
    tz = "UTC"
  ),
  Radiation = c(
    56.52811508317025, 127.6913397994129, 249.13531630136984, 232.7319272847358,
    282.9493191340509, 700.568959549902, 1227.3102986741683, 1481.2186411399216,
    1388.3260398336595, 1126.6007556702543, 598.2448670694715, 234.7725780919765,
    135.41278232387475, 57.28412474559686, 143.20141084148727, 219.71977855675146,
    635.4653254109588, 1313.8880365215264, 1463.729484349315, 1508.7094646183953,
    1183.3423383219176, 653.6103462181995, 318.73400056262227, 284.35463106164383,
    238.0325500440313, 120.99518652641878
  ),
  row.names = c(14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 
                26L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 
                52L)
)

我创建了一个折线图。

ggplot(data=df, aes(x=time, y=Radiation)) + 
  geom_line (aes(color=treatment, group=treatment)) + 
  geom_point(aes(shape=treatment, fill=treatment), color="black", size=2) +
  scale_shape_manual(values= c(21,22)) +
  scale_fill_manual(values=c("coral4","grey65"))+
  scale_color_manual(values=c("coral4","grey65"))+
  scale_x_datetime(date_breaks = "2 hour", date_labels = "%H:%M") +
  scale_y_continuous(breaks=seq(0,2500,500), limits = c(0,2500)) +
  theme_classic(base_size=18, base_family="serif")+
  theme(legend.position=c(0.5,0.9),
        legend.title=element_blank(),
        legend.key=element_rect(color="white", fill="white"),
        legend.text=element_text(family="serif", face="plain",
                                 size=15, color= "Black"),
        legend.background=element_rect(fill="white"),
        axis.line=element_line(linewidth=0.5, colour="black")) +
windows(width=5.5, height=5)

我的目标是创建一个更平滑的线图。在 Excel 中,实现这一点非常简单。然而,在 R 中,许多帖子建议使用

geom_smooth()

但是,当我使用

geom_smooth()
时,数据会变得扭曲,因为它正在拟合数据。我想要的是像 Excel 一样显示平滑的折线图。

您能让我知道如何使折线图更平滑吗?

谢谢,

r ggplot2 line smoothing
2个回答
1
投票

ggalt
有一个专门用于此的功能(
ggalt::geom_xspline
):

ggplot(data=df, aes(x=time, y=Radiation)) + 
  ggalt::geom_xspline(aes(color=treatment, group=treatment), size = 0.5) + 
  geom_point(aes(shape=treatment, fill=treatment), color="black", size=2) +
  scale_shape_manual(values= c(21,22)) +
  scale_fill_manual(values=c("coral4","grey65"))+
  scale_color_manual(values=c("coral4","grey65"))+
  scale_x_datetime(date_breaks = "2 hour", date_labels = "%H:%M") +
  scale_y_continuous(breaks=seq(0,2500,500), limits = c(0,2500)) +
  theme_classic(base_size=18, base_family="serif")


1
投票

您还可以在

gam
中使用
geom_smooth()
,其中
k
等于每组中的点数(本例中为 13)。

library(ggplot2)
ggplot(data=df, aes(x=time, y=Radiation)) + 
  geom_smooth(aes(color=treatment, group=treatment), method="gam", formula = y~ s(x, k=13)) + 
  geom_point(aes(shape=treatment, fill=treatment), color="black", size=2) +
  scale_shape_manual(values= c(21,22)) +
  scale_fill_manual(values=c("coral4","grey65"))+
  scale_color_manual(values=c("coral4","grey65"))+
  scale_x_datetime(date_breaks = "2 hour", date_labels = "%H:%M") +
  scale_y_continuous(breaks=seq(0,2500,500), limits = c(0,2500)) +
  theme_classic(base_size=18, base_family="serif")+
  theme(legend.position=c(0.5,0.9),
        legend.title=element_blank(),
        legend.key=element_rect(color="white", fill="white"),
        legend.text=element_text(family="serif", face="plain",
                                 size=15, color= "Black"),
        legend.background=element_rect(fill="white"),
        axis.line=element_line(linewidth=0.5, colour="black"))

创建于 2024-02-29,使用 reprex v2.0.2

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