R Ggplot2中包含两个变量的两个直方图
这是我的DF:R Ggplot2中包含两个变量的两个直方图,r,ggplot2,data-visualization,histogram,R,Ggplot2,Data Visualization,Histogram,这是我的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
> 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_项目是项目。
我想绘制一个图表,在这个图表中,我可以看到一些id_项目的预测值和真实值并排。
有超过100个,所以我只需要一个子集的情节(否则它将是一个混乱)。
让我知道
另外,最好的办法是将测试标签和xgb1转换成行,并添加一个虚拟变量“预测值/真值”,但我不知道该怎么做。我建议采用这种方法,重塑数据,然后绘制。有了更多的数据,它看起来会更好:
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"))
输出:
这里有一种不同的方法,使用
geom\u 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()
“%%>%”
ggplot2::ggplot(ggplot2::aes(x=id_项,ymin=test_标签,ymax=xgb_1,color=diff))+
ggplot2::geom_errorbar()
通过阅读问题,我不确定您想要哪种图形。我也不确定(在P.S.中)理想的东西是预测等于真的虚拟物还是不同的东西。如果您以可以轻松读入R或具有类似功能的玩具示例的格式向我们提供数据,我们也可能能够提供更好的答案。
"%>%" <- 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()