简体   繁体   English

如何在 R 中的一个 plot 中打印不同的变量

[英]How to print different variables in one plot in R

So I have some 2-factor-variables (1,0) and an age variable (numeric), I have done a cluster analysis and I want to plot the different variables in X axis, something like this:所以我有一些 2 因子变量(1,0)和一个年龄变量(数字),我做了一个聚类分析,我想 plot X 轴上的不同变量,如下所示:

情节实现

I have tried using ggplot but I just cant get what I am looking for.我曾尝试使用 ggplot 但我无法得到我正在寻找的东西。

So I have the following:所以我有以下内容:

visit <- rbinom(20, 1, 0.5)
death <- rbinom(20, 1, 0.5)
gender <- rbinom(20, 1, 0.5)
age <- sample(1:50,20,replace=F)
cluster <- letters[1:5]
df <- data.frame(visit,death,gender,age,cluster)

And I want to plot a graph in which in colored line represents the cluster based on the above variables.我想 plot 一个图表,其中彩色线代表基于上述变量的集群。

You can do this by simply specifying color in aes() and using geom_line() .您可以通过简单地在aes()中指定color并使用geom_line()来做到这一点。 I initially thought this might be a duplicate, but didn't see any real candidates after a quick search.我最初认为这可能是重复的,但在快速搜索后没有看到任何真正的候选人。 I will say that I think you would benefit by reading up on some ggplot2 basics, such as by going through this guide (for example).我会说,我认为您会受益于阅读一些ggplot2基础知识,例如阅读本指南(例如)。

So, here's how you could produce such a plot:所以,这里是你如何生产这样一个 plot:

library(ggplot2)
ggplot(data = df, mapping = aes(x = death, y = age, color = cluster)) +
    geom_line()

在此处输入图像描述

Data数据

set.seed(42) ## Set seed for reproducibility
visit <- rbinom(20, 1, 0.5)
death <- rbinom(20, 1, 0.5)
gender <- rbinom(20, 1, 0.5)
age <- sample(1:50,20,replace=F)
cluster <- letters[1:5]
df <- data.frame(visit,death,gender,age,cluster)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM