简体   繁体   中英

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:

情节实现

I have tried using ggplot but I just cant get what I am looking for.

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.

You can do this by simply specifying color in aes() and using 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).

So, here's how you could produce such a 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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