使用facet_wrap绘制融化变量 [英] Ploting melted variable using facet_wrap

查看:44
本文介绍了使用facet_wrap绘制融化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个具有6个方面的图.

我正在使用熔化的数据集.

使用时:

  ggplot(g,aes(x = x,y =值,color =变量,linetype =变量,size =变量))+geom_line()+ facet_wrap(.〜condition) 

我得到了错误:

  layout_base中的错误(数据,vars,drop = drop):至少一层必须包含用于构面的所有变量 

鉴于我在变量"condition"中具有用于刻面的变量,因此我不明白这意味着什么

此处是创建融化变量的原始数据./p>

这是我用来制作情节的代码:

  ggplot(g,aes(x = x,y = as.numeric(value),color = variable,linetype = variable,size = variable))+geom_line()+scale_x_continuous(breaks = seq(1,10,1))+scale_y_continuous(breaks = seq(0,1,0.1))+scale_colour_manual(values = c("red3","red3","red3","red3","red3","red3","red3","red3","red3","blue3","blue3","blue3","blue3","blue3","blue3","blue3","blue3","blue3")))+scale_linetype_manual(值= c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2))+scale_size_manual(值= c(0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4))+xlab("\ nTime-steps")+ylab(比例正确\ n")+theme_bw()+主题(axis.text = element_text(size = 5),axis.title = element_text(size = 5),axis.line = element_line(大小= 0.25),axis.ticks = element_line(size = 0.25),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.border = element_blank(),panel.background = element_blank(),legend.position ="none",legend.direction ="vertical",legend.title = element_blank(),legend.text = element_text(size = 6),legend.background = element_blank(),图例.key = element_blank())+ facet_wrap(〜condition) 

解决方案

您有两个主要问题-y被编码为一个因子,应该是数字,更重要的是,您的facet_wrap词中有一个小的错字,应该是没有.:

  p<-ggplot(g,aes(x = as.numeric(x),y​​ = as.numeric(value),group = variable))p<-p + geom_line()p<-p + facet_wrap(〜condition)p 

这给了我

我注意到变量中嵌入了一个大小编码.可以很好地将其包括在图中:

  g $ size< -substring(g $ variable,1,5) 

然后在美学中添加 colour = size ,您将获得:

I would like to create a plot with 6 facets.

I am using a melted dataset.

When using :

ggplot(g,aes(x = x, y = value, colour = variable, linetype = variable,size = variable)) + 
    geom_line() + facet_wrap(.~condition)

I get the error:

Error in layout_base(data, vars, drop = drop) : 
  At least one layer must contain all variables used for facetting

I don't understand what this means given that I have the variable used for faceting in the variable "condition"

Here is the original data from which the melted variable was created.

This is the code I'm using to produce the plot:

ggplot(g,aes(x = x, y = as.numeric(value), colour = varible, linetype = variable,size = variable)) + 
    geom_line() + 
    scale_x_continuous(breaks=seq(1,10,1)) +
    scale_y_continuous(breaks=seq(0,1, 0.1))+
    scale_colour_manual(values=c("red3","red3","red3","red3", "red3","red3","red3", "red3","red3",   
    "blue3","blue3","blue3","blue3","blue3","blue3","blue3","blue3","blue3")) + 
    scale_linetype_manual(values = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2)) + 
    scale_size_manual(values = c(0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4,0.4)) + 
    xlab("\nTime-steps") + 
    ylab("Proportion correct\n") +
    theme_bw() +
    theme(axis.text=element_text(size=5),
          axis.title=element_text(size=5),
          axis.line = element_line(size=0.25),
          axis.ticks=element_line(size=0.25),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.border = element_blank(),
          panel.background = element_blank(),
          legend.position="none" ,
          legend.direction="vertical", 
          legend.title=element_blank(),
          legend.text=element_text(size=6), 
          legend.background=element_blank(), 
          legend.key=element_blank())+facet_wrap(~condition)

解决方案

You have two main problems - y is coded as a factor and should be numeric, and more importantly there is a tiny typo in your facet_wrap term, which should be without .:

p <- ggplot(g,aes(x = as.numeric(x), y = as.numeric(value), group = variable)) 
p <- p + geom_line()
p <- p + facet_wrap(~condition)
p

This gives me

EDIT: I noted that variable has a size coding embedded. This works nicely to include it in the graphs:

g$size<-substring(g$variable,1,5)

Then add colour = size in the aesthetics and you obtain:

这篇关于使用facet_wrap绘制融化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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