【问题标题】:Make a distribution plot with more than one values in the same graph在同一张图中制作一个包含多个值的分布图
【发布时间】:2020-04-10 14:40:51
【问题描述】:

有这样的数据框:

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

如何在一个图中创建十个变量的分布,但没有填充分布内部,而只有在线条中具有不同的颜色,如下所示:

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

【问题讨论】:

  • 您是在问如何模拟分布(类似于n_dat)?看起来您已经拥有的数据将形成 ggplot 示例中的 p_dat。
  • @TimAssal 不,我只有 n_dat 和 p_data 的代码作为示例来显示我想要的情节,只有线条颜色,每个分布内部没有填充,右侧是名称具有线条颜色的所有变量

标签: r ggplot2


【解决方案1】:
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()

reprex package (v0.3.0) 于 2020-04-10 创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    相关资源
    最近更新 更多