从数据帧列表中绘制ggplot中的行

时间:2017-02-14 15:16:14

标签: r list plot ggplot2

我有一个data.frames列表:

samplelist = list(a = data.frame(x = c(1:10), y=rnorm(10),
                 b= data.frame(x=c(5:10), y = rnorm(5),
                 c = data.frame(x=c(2:12), y=rnorm(10))

我想构建以下格式的ggplot

ggplot()+ 
    geom_line(data=samplelist[[1]], aes(x,y))+
    geom_line(data=samplelist[[2]], aes(x,y))+
    geom_line(data=samplelist[[3]], aes(x,y))

但这不是超级自动化的。有没有人建议如何解决这个问题?

谢谢!

4 个答案:

答案 0 :(得分:8)

ggplot最有效地处理" long"中的数据格式。在这种情况下,这意味着将三个数据帧堆叠到一个数据帧中,并添加一个额外的列以标识源数据帧。在该格式中,您只需要对geom_line进行一次调用,而标识源数据框的新列可以用作颜色美学,从而为每个源数据框生成不同的行。 dplyr函数bind_rows允许您在ggplot的调用中动态堆叠数据框。

library(dplyr)
library(ggplot2)

samplelist = list(a = data.frame(x=c(1:10), y=rnorm(10)),
                  b = data.frame(x=c(5:10), y=rnorm(6)),
                  c = data.frame(x=c(2:12), y=rnorm(11)))

ggplot(bind_rows(samplelist, .id="df"), aes(x, y, colour=df)) +
  geom_line()

enter image description here

我在上面假设您希望每一行都是不同的颜色,并且有一个显示颜色映射的图例。但是,如果出于某种原因,您只想要三条黑线而没有图例,只需将colour=df更改为group=df即可。

答案 1 :(得分:3)

这将有效 -

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var meals: UILabel!// not really important

@IBOutlet weak var chosenMeal: UILabel!//This  label is used to the page to show detail about what meal that app recommended,
 while I push  func(checkMeals) that the compiler get me an error : "fatal error: unexpectedly found nil while unwrapping an Optional value"



@IBOutlet weak var pickerView: UIPickerView! // used to chose breakfast or lunch


@IBAction func checkMeals(_ sender: UIButton) {

  breakFast = [ "sandwich","Hamburgers", "beef" ,"vegetable", "egg"]

    if meals.text == "breakfast" {


        let breakFast = arc4random_uniform(5) // 

        switch breakFast {
        case 0:
            chosenMeal.text = "sandwich"
        case 1:
             chosenMeal.text = "Hamburgers"
        case 2:
             self.chosenMeal.text = "beef"
        case 3:
             chosenMeal.text = "vegetable"

        case 4:
             chosenMeal.text = "egg"

        default:
            break

        }

    }   else if meals.text == "lunch" {
     let  lunch = (arc4random_uniform(5)+5)

        switch lunch {
        case 6:
            chosenMeal.text = "some1"
        case 7:
            chosenMeal.text = "some2"
        case 8:
            chosenMeal.text = "some3"
        case 9:
          chosenMeal.text = "some4"

        case 10:
            chosenMeal.text = "some5"

        default:
            break

        }



let foods = ["breakfast", "lunch"]

func numberOfComponents(in pickerView: UIPickerView) -> Int
{
    return 1
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
    return foods[row]
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
    return foods.count
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
    meals.text = foods[row]
}



override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

答案 2 :(得分:3)

或者你可以使用lapply。

library(ggplot2)

samplelist = list(a = data.frame(x = c(1:10), y=rnorm(10)),
                  b= data.frame(x=c(5:10), y = rnorm(6)),
                  c = data.frame(x=c(2:12), y=rnorm(11)))

p <- ggplot()

plot <- function(df){
    p <<- p + geom_line(data=df, aes(x,y))
}

lapply(samplelist, plot)

p

答案 3 :(得分:1)

Reduce是迭代添加内容的另一种选择,

library(ggplot2)

samplelist = list(a = data.frame(x = c(1:10), y=rnorm(10)),
                  b= data.frame(x=c(5:10), y = rnorm(6)),
                  c = data.frame(x=c(2:12), y=rnorm(11)))

pl <- Reduce(f = function(p, d) p + geom_line(data=d, aes(x,y)), 
             x = samplelist, init = ggplot(), accumulate = TRUE)

gridExtra::grid.arrange(grobs = pl)

enter image description here