ggplot等效于matplot [英] ggplot equivalent for matplot

查看:84
本文介绍了ggplot等效于matplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ggplot2中是否有等效图可以绘制此数据集?我使用了matplot,并阅读了可以使用qplot的信息,但是它确实不起作用. ggplot/matplot

Is there an equivalent in ggplot2 to plot this dataset? I use matplot, and read that qplot could be used, but it really does not work. ggplot/matplot

data<-rbind(c(6,16,25), c(1,4,7), c(NA, 1,2), c(NA, NA, 1))
as.data.frame(data)
matplot(data, log="y",type='b', pch=1)

推荐答案

您可以在ggplot中创建一个类似的图,但是首先需要对数据进行一些重塑.

You can create a similar plot in ggplot, but you will need to do some reshaping of the data first.

library(reshape2)

#ggplot needs a dataframe
data <- as.data.frame(data)
#id variable for position in matrix 
data$id <- 1:nrow(data) 
#reshape to long format
plot_data <- melt(data,id.var="id")


#plot
ggplot(plot_data, aes(x=id,y=value,group=variable,colour=variable)) +
  geom_point()+
  geom_line(aes(lty=variable)) +
  scale_y_log10(breaks=c(1,2,5,10,25))

我的输出:

这篇关于ggplot等效于matplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆