在R中再次绘制两组数据

时间:2018-07-24 15:08:07

标签: r

嗨,我正在尝试绘制两组相互关联的数据,

一个在Y上,另一个在X

例如,这是我的数据集:

import UIKit

@IBDesignable class SeparatorStackView: UIStackView {

    @IBInspectable var separatorColor: UIColor? = .black {
        didSet {
            invalidateSeparators()
        }
    }
    @IBInspectable var separatorWidth: CGFloat = 0.5 {
        didSet {
            invalidateSeparators()
        }
    }
    @IBInspectable private var separatorTopPadding: CGFloat = 0 {
        didSet {
            separatorInsets.top = separatorTopPadding
        }
    }
    @IBInspectable private var separatorBottomPadding: CGFloat = 0 {
        didSet {
            separatorInsets.bottom = separatorBottomPadding
        }
    }
    @IBInspectable private var separatorLeftPadding: CGFloat = 0 {
        didSet {
            separatorInsets.left = separatorLeftPadding
        }
    }
    @IBInspectable private var separatorRightPadding: CGFloat = 0 {
        didSet {
            separatorInsets.right = separatorRightPadding
        }
    }

    var separatorInsets: UIEdgeInsets = .zero {
        didSet {
            invalidateSeparators()
        }
    }

    private var separators: [UIView] = []

    override func layoutSubviews() {
        super.layoutSubviews()

        invalidateSeparators()
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        invalidateSeparators()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()

        invalidateSeparators()
    }


    private func invalidateSeparators() {
        guard arrangedSubviews.count > 1 else {
            separators.forEach({$0.removeFromSuperview()})
            separators.removeAll()
            return
        }

        if separators.count > arrangedSubviews.count {
            separators.removeLast(separators.count - arrangedSubviews.count)
        } else if separators.count < arrangedSubviews.count {
            separators += Array<UIView>(repeating: UIView(), count: arrangedSubviews.count - separators.count)
        }

        separators.forEach({$0.backgroundColor = self.separatorColor; self.addSubview($0)})

        for (index, subview) in arrangedSubviews.enumerated() where arrangedSubviews.count >= index + 2 {
            let nextSubview = arrangedSubviews[index + 1]
            let separator = separators[index]

            let origin: CGPoint
            let size: CGSize

            if axis == .horizontal {
                let originX = (nextSubview.frame.maxX - subview.frame.minX)/2 + separatorInsets.left - separatorInsets.right
                origin = CGPoint(x: originX, y: separatorInsets.top)
                let height = frame.height - separatorInsets.bottom - separatorInsets.top
                size = CGSize(width: separatorWidth, height: height)
        } else {
                let originY = (nextSubview.frame.maxY - subview.frame.minY)/2 + separatorInsets.top - separatorInsets.bottom
                origin = CGPoint(x: separatorInsets.left, y: originY)
                let width = frame.width - separatorInsets.left - separatorInsets.right
                size = CGSize(width: width, height: separatorWidth)
            }

            separator.frame = CGRect(origin: origin, size: size)
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您可以尝试使用tidyverse。数据集保存在df1df2中。

df1 %>% 
  rownames_to_column() %>% 
  bind_rows( rownames_to_column(df2), .id="set") %>% 
  gather(k,v,-set, -rowname) %>%   
  mutate(set = paste0("set",set)) %>% 
  spread(set, v) %>% 
  ggplot(aes(set1, set2, color=k)) + 
    geom_point() + 
    # add regression lines?!
    geom_smooth(method="lm", se=F,show.legend = F, size=0.5) 

enter image description here

当然,您可以按照评论中的建议使用构面面板。

df1 %>% 
  rownames_to_column() %>% 
  bind_rows( rownames_to_column(df2), .id="set") %>% 
  gather(k,v,-set, -rowname) %>%   
  mutate(set = paste0("set",set)) %>% 
  spread(set, v) %>% 
  ggplot(aes(set1, set2, color=k)) + 
    geom_point(show.legend = F) + 
    geom_smooth(method="lm", se=F,show.legend = F) + 
    facet_wrap(~k, scales = "free")

enter image description here

答案 1 :(得分:0)

第一个结构-> x,第二个结构-> y,然后

plot(unlist(x), unlist(y))