首页 > 解决方案 > 使用 R 绘图

问题描述

我想multiplot使用绘制图形,并且我想要每个站点在 X 轴上的值,并且图中的不同线是 M、T、L 和 G,但是在代码的开头,我得到了一个错误,

$ 运算符对原子向量无效

Graphdata <- as.data.frame(multiplot)
par(mfrow=c(4,3))
plot(Graphdata$Sites$A, Graphdata$f, ylim=c(0,16), xlab="Number of years", 
     ylab="Relative density", lwd=2)
lines(Graphdata$Sites$M, type="l", col="blue", lwd=2)
lines(Graphdata$Sites$T, type="l", col="green", lwd=2)
lines(Graphdata$Sites$L, type="l", col="grey", lwd=2)
lines(Graphdata$Sites$G, type="l", col="orange", lwd=2)

我的一些数据是

标签: rplotdata-visualization

解决方案


您需要按如下方式进行子集化:

SiteA<-Graphdata[Graphdata$Sites=="A",]

plot(SiteA$f,SiteA$M)

par(mfrow=c(1,1))

也就是说,我是 的粉丝,tidyverse并认为它会给你一个更简单的解决方案。你需要reshape2你的数据,虽然gather可以做得很好。编辑您还需要决定如何处理缺失值。

library(tidyverse)
Graphdata %>% 
  gather("MySite","MyValue",3:ncol(.)) %>% 
  filter(Sites=="A") %>% 
  ggplot(aes(`f`,MyValue,col=MySite))+geom_point()+geom_line()

这产生: 在此处输入图像描述


推荐阅读