【问题标题】:Plot multiple columns on the same graph in R [duplicate]在R中的同一张图上绘制多列[重复]
【发布时间】:2012-03-20 20:58:38
【问题描述】:

我有以下数据框:

A       B       C       D       Xax
0.451   0.333   0.034   0.173   0.22        
0.491   0.270   0.033   0.207   0.34    
0.389   0.249   0.084   0.271   0.54    
0.425   0.819   0.077   0.281   0.34
0.457   0.429   0.053   0.386   0.53    
0.436   0.524   0.049   0.249   0.12    
0.423   0.270   0.093   0.279   0.61    
0.463   0.315   0.019   0.204   0.23

我需要在同一个图中绘制所有这些列(在 x 轴上,我想要变量 Xax,y 轴上想要变量 A、B、C 和 D),还需要为每个变量绘制回归线一个人。

我试过这个:

pl<-ggplot(data=df) + geom_point(aes(x=Xax,y=A,size=10)) + 
  geom_point(aes(x=Xax,y=B,size=10)) + 
  geom_point(aes(x=Xax,y=C,size=10)) + 
  geom_point(aes(x=Xax,y=D,size=10)) + 
  geom_smooth(method = "lm", se=FALSE, color="black")

但它只绘制第一个(Xax 和 A)

【问题讨论】:

    标签: r ggplot2 r-faq


    【解决方案1】:

    最简单的方法是将数据转换为“高”格式。

    s <- 
    "A       B        C       G       Xax
    0.451   0.333   0.034   0.173   0.22        
    0.491   0.270   0.033   0.207   0.34    
    0.389   0.249   0.084   0.271   0.54    
    0.425   0.819   0.077   0.281   0.34
    0.457   0.429   0.053   0.386   0.53    
    0.436   0.524   0.049   0.249   0.12    
    0.423   0.270   0.093   0.279   0.61    
    0.463   0.315   0.019   0.204   0.23
    "
    d <- read.delim(textConnection(s), sep="")
    
    library(ggplot2)
    library(reshape2)
    d <- melt(d, id.vars="Xax")
    
    # Everything on the same plot
    ggplot(d, aes(Xax,value, col=variable)) + 
      geom_point() + 
      stat_smooth() 
    
    # Separate plots
    ggplot(d, aes(Xax,value)) + 
      geom_point() + 
      stat_smooth() +
      facet_wrap(~variable)
    

    【讨论】:

    • 我没有得到解决方案??这是数据框的一小部分,它要大得多。您能解释一下答案并将其应用于原始数据框吗??
    • 顺便说一句,这不起作用:/
    • @ifreak 谁能将此代码应用于原始完整数据框,该数据框仅存在于您的计算机上,而您没有提供?并且说“它不起作用”是可以想象到的最没有帮助的评论,因为它没有提供任何关于它如何或为什么不起作用的信息。
    • 我的数据框大约有 500 行。但我试图复制文森特提供的相同代码并在我的脚本之外尝试它也没有工作。这就是我的意思,它没有用..
    • 为了能够提供帮助,我们需要知道您所说的“它不起作用”是什么意思:是否有任何错误消息?
    【解决方案2】:

    一个非常简单的解决方案:

    df <- read.csv("df.csv",sep=",",head=T)
    x <- cbind(df$Xax,df$Xax,df$Xax,df$Xax)
    y <- cbind(df$A,df$B,df$C,df$D)
    matplot(x,y,type="p")
    

    请注意,它只是绘制数据,并没有绘制任何回归线。

    【讨论】:

      【解决方案3】:

      使用tidyverse

      df %>% tidyr::gather("id", "value", 1:4) %>% 
        ggplot(., aes(Xax, value))+
        geom_point()+
        geom_smooth(method = "lm", se=FALSE, color="black")+
        facet_wrap(~id)
      

      数据

      df<- read.table(text =c("
      A       B       C       G       Xax
      0.451   0.333   0.034   0.173   0.22        
      0.491   0.270   0.033   0.207   0.34    
      0.389   0.249   0.084   0.271   0.54    
      0.425   0.819   0.077   0.281   0.34
      0.457   0.429   0.053   0.386   0.53    
      0.436   0.524   0.049   0.249   0.12    
      0.423   0.270   0.093   0.279   0.61    
      0.463   0.315   0.019   0.204   0.23"), header = T)
      

      【讨论】:

      • 感谢tidyverse 选项。效果很好
      【解决方案4】:

      为了选择要绘制的列,我在 Vincent Zoonekynd 的回答中添加了 2 行:

      #convert to tall/long format(from wide format)
      col_plot = c("A","B")
      dlong <- melt(d[,c("Xax", col_plot)], id.vars="Xax")  
      
      #"value" and "variable" are default output column names of melt()
      ggplot(dlong, aes(Xax,value, col=variable)) +
        geom_point() + 
        geom_smooth()
      

      谷歌“整理数据”以了解有关高(或长)/宽格式的更多信息。

      【讨论】:

        猜你喜欢
        • 2015-06-25
        • 1970-01-01
        • 2019-08-05
        • 1970-01-01
        • 1970-01-01
        • 2013-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多