r - 根据 R 中的数据框绘制合适的图形并将其转换为 HTML

标签 r dataframe ggplot2 html-table plotly

我有下面提到的数据框:

DF_1>

    Month  # of A  M  Sum of A  M Med A  Mean A   # of B  M  Sum of B M  Median B  Mean B

    Mar-17 10      -  100000    -        -        6       -  15000    -  -         -      
    Feb-17 22      -  150000    -        -        8       -  22000    -  -         -
    Jan-17 25      -  200000    -        -        3       -  23000    -  -         -

将其他字段标记为 - 因为我不使用这些值来绘制图表,下面是 html 格式的相同 Dataframe 的屏幕截图。 (不包括上面的标题和数据框的其余 2 行相同)

enter image description here

底部增加一行,提供总计,如何使用 A 的状态B 的状态 绘制一张图表A 的编号A 的总和B 的编号B 的总和 数据点。

月份应位于 Y 轴上,其他数据点应位于 X 轴上,图表标题如 ABC

此外,我想将该图表转换为 Html 格式,可以通过 mailR 库轻松邮寄。

我已经有 2 个 html 表格覆盖了我的邮件正文,如下所示:

AAAA CCCC

BBBBBBBBB

其中 AAAA 是一张表,BBBBBBBBB 是第二张表,CCCC 是我想要排列此图表的空白区域。

最佳答案

我已经重新创建了表格,因为您没有提供它,这就是我正在使用的(注意:我为 sum of B 添加了一些假数字,因为它在文本中丢失):

status <- tibble::tibble(Month = factor(c('Jan-17', 'Feb-17', 'Mar-17'),
                                        levels = c('Jan-17', 'Feb-17', 'Mar-17')),
                         '# of A' = c(100000, 150000, 200000),
                         'Sum of A' = c(6, 8, 3),
                         '# of B' = c(150000, 22000, 23000),
                         'Sum of B' = c(2, 4, 6))

然后我使用 data.table::melt() 将其从宽格式转换为长格式:

status_m <- data.table::melt(status, 
                             id.vars = "Month")

然后我使用ggplot2创建折线图(注意:# of A# of B 的比例相似,Sum of ASum of B 的比例相似,但 #Sum 之间的区别是太大):

p <- ggplot(data = status_m) +
  geom_line(aes(x = Month,
                y = value,
                group = variable,
                color = variable)) +
  theme(legend.title=element_blank())

使用 ggsave() 保存该图:

ggsave("plot.png",
       plot = p,
       width = 10,
       height = 10)

接下来,我创建一个“虚拟”tableHTML :

th <- status %>% 
  tableHTML(rownames = FALSE)

然后我创建一些 HTML重新创建您指定的布局,其中有 2 行,其中第一行有 2 列(注意: <div> s 和 CSS flex ,请参阅 here )。

mail_html <-
  htmltools::HTML(paste("<!DOCTYPE html>\n<html>\n<body>", 
                        '<div style="display:flex;">',
                        '<div style="flex:50%;">',
                        th,
                        '</div>',
                        '<div style="flex:50%;alig">',
                        # change this path to the plot you saved using ggsave()
                        '<img src="/path/to/plot.png" height = "800" width = "800";">',
                        '</div>',
                        '</div>',
                        '<div>',
                        th,
                        '</div>',
                        "</body>\n</html>", sep = "\n"))

最后一步是使用 mailR 发送电子邮件(注意:相应地更改邮件设置):

library(mailR)

send.mail(from = "someone@gmail.com",
          to = "someone.elser@mail.com",
          subject = "test report",
          body = mail_html,
          html = TRUE,
          inline = TRUE,
          smtp = list(host.name = "smtp.gmail.com", 
                      port = 465, 
                      user.name = "...", 
                      passwd = "...", 
                      ssl = TRUE),
          authenticate = TRUE,
          send = TRUE)

测试电子邮件如下所示:

mail

关于r - 根据 R 中的数据框绘制合适的图形并将其转换为 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49143779/

相关文章:

r - 为什么逻辑回归中存在异方差稳健标准误?

python - pandas - 仅更新基于 'key' 值的特定 DataFrame 列值

r - 在 `geom_hline` 图中设置 `geom_bar` 的长度

python - 尝试使用 sheet_name=None 为文档中的每个工作表运行 python pandas 脚本但不工作

python - Python 中索引的工作日数据框行选择

r - 使用 gghighlight 时为多个 geom_hlines 创建图例

r - 将多个 ggplot 图表放在同一个文件中时不需要的粗体

r - 如何根据位数将数值转换为小时和分钟

r - 如何创建正负序列

用 R 中的子集均值替换 NA 值?