首页 > 解决方案 > 如何将 matplot 转换为 ggplot2?

问题描述

我正在学习matplotggplot2

我在尝试将 matplot 转换为 ggplot2 时遇到问题

matplot(iris[,1:4],type="l")

legend("topleft",names(iris)[1:4],lty=c(1,2,3,4),col=c(1,2,3,4))

我真的不能把这Species部分带到ggplot2

我怎样才能转换这个?

标签: rggplot2

解决方案


大概是这样的

library(tidyverse)

iris %>%
  select(1:4) %>%
  mutate(row = row_number()) %>%
  pivot_longer(cols = -row) %>%
  ggplot() + aes(row, value, color = name) + geom_line()

enter image description here


推荐阅读