smoothing out lines on ggplot in R

Viewed 35

I am trying to smooth out the line in ggplot. I tried using geom_smooth but that is giving me a fitted line instead. My code is below

#step 1 is to load the csv file
dataset_edited = read.csv("dsccurves.csv", header = TRUE)
str(dataset_edited)

#Check figure of all DSC curves
plt_dsc_1 = ggplot(dataset_edited, aes(x=gt, y=enthalpy, group = location, color = year)) +
 geom_point(size=0.25)+
 scale_x_continuous("Temperature (degree C)",limits = c(50,75), breaks = seq(50,75,1))+
 scale_y_continuous("Heat flow (W/g)",limits = c(-0.37,-0.18), breaks = 
 seq(-0.37,-0.18,0.02))+
 theme_classic()+
 labs(title = "A")+
 guides(guide_legend(title = "Year"))+ 
 theme(text = element_text(size=15))
plt_dsc_1

Currently the image I get looked like this Current image

This is my data file

1 Answers

As per comment you could use geom_line to have line rather than multiple points lineup on your graph, however with your data the plot is looking like this: enter image description here

The reason is that it seems that there is duplicate values for one year at one location: 2016-17 at Davis enter image description here

So you might need to clean the data before plotting

Code

Plot 1:

ggplot(df, aes(x=gt, y=enthalpy, group = location, color = year)) +
  geom_line()+
  scale_x_continuous("Temperature (degree C)",limits = c(50,75), breaks = seq(50,75,1))+
  scale_y_continuous("Heat flow (W/g)",limits = c(-0.37,-0.18), breaks = 
                       seq(-0.37,-0.18,0.02))+
  theme_classic()+
  labs(title = "A")+
  guides(guide_legend(title = "Year"))+ 
  theme(text = element_text(size=15))

Plot 2:

ggplot(df, aes(x=gt, y=enthalpy, color = year)) +
  geom_point()+
  scale_x_continuous("Temperature (degree C)",limits = c(50,75), breaks = seq(50,75,1))+
  scale_y_continuous("Heat flow (W/g)",limits = c(-0.37,-0.18), breaks = 
                       seq(-0.37,-0.18,0.02))+
  facet_grid(year~location)
Related