0

这是我的 DF:

> head(xgb_1_plot)
   week PRICE id_item food_cat_id test_label xgb_1
2     5    18      60           7          2     2
7     5    21       9           6          5     8
12    5    14      31           4          4     6
21    5    15      25           7         12    12
31    5    14      76           3          4     2
36    5     7      48           8          2     4

其中 test_label 是测试值,“xgb_1”是具有预测值的列,id_items 是项目。我想绘制图表,其中我可以并排查看某些 id_items 的预测值 VS 真实值。有超过 100 个,所以我只需要情节的一个子集(否则会一团糟)。让我知道!

PS最好的办法是将test_label和xgb1转换成行并添加一个虚拟变量“Predicted/True value”,但我不知道该怎么做。

4

2 回答 2

0

我会建议这种方法,重塑数据然后绘图。拥有更多数据,它看起来会更好:

library(tidyverse)
#Data
dfa <- structure(list(id_item = c(60L, 9L, 31L, 25L, 76L, 48L), test_label = c(2L, 
5L, 4L, 12L, 4L, 2L), xgb_1 = c(2L, 8L, 6L, 12L, 2L, 4L)), class = "data.frame", row.names = c("2", 
"7", "12", "21", "31", "36"))

代码:

#Reshape
dfa %>% pivot_longer(cols = -id_item) %>%
  ggplot(aes(x=value,fill=name))+
  geom_histogram(position = position_dodge())+
  facet_wrap(.~id_item)

输出:

在此处输入图像描述

于 2020-08-31T22:17:45.570 回答
0

这是使用geom_errorbar. 也许颜色的东西有点太多了,但是今天是下雨天……所以需要一些品种

"%>%" <- magrittr::"%>%"

dat <- dplyr::tibble(id_item=c(69,9,31,25,76,48),
              test_label=c(2,5,4,12,4,2),
              xgb_1=c(2,8,6,21,2,4))

dat %>%
  dplyr::mutate(diff=abs(test_label-xgb_1)) %>%
  ggplot2::ggplot(ggplot2::aes(x=id_item,ymin=test_label,ymax=xgb_1,color=diff)) + 
  ggplot2::geom_errorbar()
于 2020-09-01T06:54:09.810 回答