我使用ggplot和geom_line创建了一个绘图,并希望在绘图上显示各个数据值(数字)。
到目前为止这是我的代码:
ggplot(errors_prob_3)+
geom_point(AES(…
主要技巧是 将数据从宽到长格式重塑 。然后一切变得更加简单甚至直观。
library(tidyverse) library(ggplot2) errors_prob_3 %>% gather(key = "variable", value = "value", -ID) %>% ggplot(aes(x = ID, y = value, colour = variable)) + geom_line() + geom_point() + geom_text(aes(label = round(value, 1)), vjust = "inward", hjust = "inward", show.legend = FALSE) + labs(x = "Number of predictors", y = "Mean/sd value", title = "Plot of the training and test cv error") + scale_color_discrete(name = "Legend", labels = c("Train mean", "Train sd", "Test mean", "Test sd"))