This is my dataset
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)
)
and I created a line graph.
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)
I aim to create a smoother line graph. In Excel, achieving this is straightforward. However, in R, many posts suggest using geom_smooth()
However, when I use geom_smooth()
, the data becomes distorted because it's fitting the data. What I want is to display a smooth line graph like in Excel.
Could you please let me know how I can make the line graph smoother?
Thanks,
You could also use a gam
in geom_smooth()
with k
equal to the number of points in each group (13 in this case).
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"))
Created on 2024-02-29 with reprex v2.0.2