Make a distribution plot with more than one values in the same graph

Viewed 120

Having a dataframe like this:

structure(list(google_before = c(0.26981640312419, 0.302252978236613, 
0.27519244423907, 0.278573602172958), amazon_before = c(0.165541492443112, 
0.162543532408399, 0.150484069110868, 0.212810080358854), ebay_before = c(0.698096408083222, 
0.625412783031095, 0.699099484936941, 0.610794910230257), yahoo_before = c(0.156164414439798, 
0.189265950612553, 0.151656203861282, 0.211930979296043), so_before = c(0.384820854982136, 
0.364443743167243, 0.352744936715994, 0.397252245652394), google_after = c(0.290892287578753, 
0.279948606399405, 0.262591995672118, 0.327138300630022), amazon_after = c(0.170072244074521, 
0.190821283262141, 0.136632592108377, 0.185400160041476), ebay_after = c(0.637122860008791, 
0.595805110056691, 0.713976579846045, 0.594306130039334), yahoo_after = c(0.154789410213351, 
0.185512865305938, 0.136271935262096, 0.18347290001916), so_after = c(0.359935532588727, 
0.391256325582968, 0.352913994612688, 0.312475345723399)), row.names = c(NA, 
-4L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000000003d1ef0>)

How is it possible to create into one plot the distributions of the ten variables but without filling inside the distribution but only have different color in the line like this:

library(tidyverse)

# Build Poisson distributions

p_dat <- map_df(1:10, ~ tibble(
  l = paste(.),
  x = 0:20,
  y = dpois(0:20, .)
))

# Build Normal distributions

n_dat <- map_df(1:10, ~ tibble(
  l = paste(.),
  x = seq(0, 20, by = 0.001),
  y = dnorm(seq(0, 20, by = 0.001), ., sqrt(.))
))

# Use ggplot2 to plot

ggplot(n_dat, aes(x, y, color = factor(l, levels = 1:10))) +
  geom_line() +
  geom_point(data = p_dat, aes(x, y, color = factor(l, levels = 1:10))) +
  labs(color = "Lambda:") +
  theme_minimal()
1 Answers
library(tidyverse)
df <- structure(list(google_before = c(0.26981640312419, 0.302252978236613, 
                                 0.27519244423907, 0.278573602172958), amazon_before = c(0.165541492443112, 
                                                                                         0.162543532408399, 0.150484069110868, 0.212810080358854), ebay_before = c(0.698096408083222, 
                                                                                                                                                                   0.625412783031095, 0.699099484936941, 0.610794910230257), yahoo_before = c(0.156164414439798, 
                                                                                                                                                                                                                                              0.189265950612553, 0.151656203861282, 0.211930979296043), so_before = c(0.384820854982136, 
                                                                                                                                                                                                                                                                                                                      0.364443743167243, 0.352744936715994, 0.397252245652394), google_after = c(0.290892287578753, 
                                                                                                                                                                                                                                                                                                                                                                                                 0.279948606399405, 0.262591995672118, 0.327138300630022), amazon_after = c(0.170072244074521, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            0.190821283262141, 0.136632592108377, 0.185400160041476), ebay_after = c(0.637122860008791, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     0.595805110056691, 0.713976579846045, 0.594306130039334), yahoo_after = c(0.154789410213351, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               0.185512865305938, 0.136271935262096, 0.18347290001916), so_after = c(0.359935532588727, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     0.391256325582968, 0.352913994612688, 0.312475345723399)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              -4L), class = c("data.table", "data.frame"))


df_long <- df %>%
  pivot_longer(cols = everything(), names_to = "company_period", 
               values_to = "val") %>%
  separate(col = company_period, into = c("company", "period"), sep = "_") %>%
  mutate_at(vars(company, period), as.factor)


df_long %>%
  ggplot(aes(x = val, color = company, linetype = period)) +
  geom_density() +
  theme_bw()


df_long %>%
  ggplot(aes(x = val, color = company)) +
  geom_density() +
  facet_grid(period ~ .) +
  theme_bw()

Created on 2020-04-10 by the reprex package (v0.3.0)

Related