这是我的数据集

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()

enter image description here

然而,当我使用geom_smooth()时,数据变得扭曲,因为它拟合数据.我想要的是显示一个像Excel中一样的平滑线条图.

你能告诉我怎样才能使折线图更流畅吗?

谢谢,

enter image description here

推荐答案

您也可以使用geom_smooth()中的gam,其中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

R相关问答推荐

在特定列上滞后n行,同时扩展框架的长度

geom_Ribbon条件填充创建与数据不匹配的形状(ggplot 2 r)

R Markdown中的交叉引用表

名称输出pmap on tible

当两个图层映射到相同的美学时,隐藏一个图层的图例值

如何写一个R函数来旋转最后n分钟?

将向量组合到一个数据集中,并相应地命名行

如何得到每四个元素向量R?

通过在colname中查找其相应值来创建列

找出二叉树中每个 node 在R中的深度?

使用for循环和粘贴创建多个变量

使用geom_iles在一个切片中包含多个值

为什么我对圆周率图的蒙特卡罗估计是空的?

仅当后续值与特定值匹配时,才在列中回填Nas

R -基线图-图形周围的阴影区域

有没有办法将不等长的列表转换为R中的数据帧

为什么不能使用lApply在包装函数中调用子集

分隔日期格式为2020年7月1日

使用一个标签共享多个组图图例符号

将每晚的平均值与每晚的值进行比较,统计是否有效?