1 Kernel

df_bw <- df_box %>% 
  pivot_longer(cols = !c(1:2),
               names_to = "conditions",
               values_to = "abs")

df_bw_1 <- df_bw %>% 
  head(8)

##Simple Kernel The next portion of our guide is going to cover how to create a kernel density plot. In this case, we will be using the geom_density function to graph the densities of our data. Below is an example of the kernel density graph for the df_bw data set we used earlier.

df_bw %>% 
  ggplot(
    aes(x=abs, color=conditions)
  )+
  geom_density()

1.1 Kernel Customization

1.1.1 Log 10 Kernel

When creating a kernel density plot, you might want to use the log10 of the absorbance. To do this, we will use similar functions as we did in the Boxplot sections.

df_bw %>% 
  ggplot(
    aes(x=abs, color=conditions)
  )+
  geom_density()+
  scale_x_continuous(trans = "log10")

1.1.2 Line Thinkness

The line thickness for this density plot might be harder to see, especially on larger presentation surfaces. Therefore, we need to use the argument linewidth in the geom_density function. You can change the thickness for your presentation needs. An example is shown below:

df_bw %>% 
  ggplot(
    aes(x=abs, color=conditions)
  )+
  geom_density(linewidth = 1)

1.1.3 Filtering

The next step we could use is to examine only a singular condition for the kernel density. To do so, we will use the filter argument.

df_bw %>% 
  filter(conditions == "SD30") %>% 
  ggplot(
    aes(x=abs))+
  geom_density()+
  geom_density()

1.1.4 Adjustment

The next available step is to reduce the smoothing of our line, to do this, we will use the adjust function in geom_density. The lower the number from 1, the further it is from the initial smoothed line. In this instance, an adjust of 0.5 would half the bandwidth. Bandwidth is referring to the generality of the kernel plot. The higher the number from 1, the more smoothed the line becomes.

Below is an example of a density plot with no smoothing adjustment Red, and the smoothing adjustment of 0.5, Blue.

df_bw %>% 
  filter(conditions == "SD30") %>% 
  ggplot(
    aes(x=abs)
  )+
  geom_density(color = "red")+
  geom_density (adjust = 0.5, color = "blue")

The adjusted 0.5 density plot looks to be taller than that of the initial plot. This is due to the density curves trying to retain the same area under the graph before and after the changes. Therefore, the density graph heights increase.

1.1.5 Kernel Line Type

Another way to change your density plot is to change the line type for your graph. For this case, I will use the code above and change the Blue line to be a dashed line.

df_bw %>% 
  filter(conditions == "SD30") %>% 
  ggplot(
    aes(x=abs)
  )+
  geom_density(color = "red")+
  geom_density (adjust = 0.5, color = "blue", linetype = "dashed")

1.1.6 Kernel Faceting

Another way that we can separate our kernel plot for our data set is to facet wrap. To do so, we will use the facet_wrap function and facet around our conditions. An example is provided below:

df_bw %>% 
  ggplot(
    aes(x = abs, color = conditions)
  )+
  geom_density(linewidth = 1)+
  facet_wrap(~conditions, nrow = 3, ncol = 2)+
  theme(strip.text = element_blank())