2

Is it possible to make this graph with ggplot, ordering the graph by the variable "t" in ascending order, distinguishing each "t" according to status (black or white circle, it can be another marker...) and if possible the variable "id " on the ordinate axis.

Id<- c(1,2,3,4)
t<- c(10,5,20,15)
status<- c(0,1,0,1)
df<- data.frame(Id, t, status)

enter image description here

1
  • It's a little hard to tell exactly what you're looking for, but I think the forcats package can help you with ordering you t variable. You can specify a shape aesthetic in geom_point(aes(shape = status)) to have the shape correspond to the status variable then use scale_shape_manual(values = c(1, 16)) to specify hollow/filled circles as the two shapes to use. Commented Jul 25, 2022 at 18:50

1 Answer 1

2

Maybe you want something like this using geom_segment for the lines with geom_vline for the vertical lines. Using shape and fill aesthetics to fill the points with "black" and "white" per status. You can use the following code:

Id<- c(1,2,3,4)
t<- c(10,5,20,15)
status<- c(0,1,0,1)
df<- data.frame(Id, t, status)

library(ggplot2)
library(dplyr)
library(forcats)
df %>%
  mutate(Id = as.factor(Id),
         status = as.factor(status)) %>%
  ggplot(aes(x = t, y = fct_reorder(Id, t, .desc = TRUE), shape = status, fill = status)) +
  geom_point() +
  geom_segment(aes(x = 0, xend = t, y = Id, yend = Id)) +
  geom_vline(xintercept=c(t),linetype="dotted", alpha = 0.4) +
  scale_shape_manual(values=c(21, 21), name = "shapes!") +
  scale_fill_manual(values=c("black", "white")) +
  scale_x_continuous(expand = c(0, 0), limits = c(0, 25)) + 
  labs(x = "", y = "Id") +
  theme_bw() +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.position = "none",
        axis.title.y = element_text(angle=0)) 

Created on 2022-07-25 by the reprex package (v2.0.1)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. It works perfectly. I want it for putting a dataset in format time at origen (in survival analysis).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.