Home > Software design >  How to get this plot in R
How to get this plot in R

Time:01-02

I have similar data like this :

Dat<-read.table (text=" Class Value
    0   8
    1   10
    0   9
    2   12
    2   13
    1   10
    3   15
    2   12
    3   12
    4   17
    4   19
    1   13
    2   12
    4   10

    ", header=TRUE)

A= 0, B=1, C=2, D=3, E=4

I have tried to sketch my plot as follows, but I failed to plot it using geom_line.

enter image description here

The sketch is hypothetical and may not reflect the hypothetical data. The X-axis shows a range of the values according to the groups, i.e., A, B, C, D, E. For example, in the data, E has a range from 10 and 19.

CodePudding user response:

Another option using geom_linerange like this:

library(dplyr)
library(ggplot2)
Dat %>%
  mutate(Class = as.factor(Class)) %>%
  group_by(Class) %>%
  summarise(x_min = min(Value),
            x_max = max(Value)) %>%
  ggplot(aes(y = Class, colour = Class, group = Class))  
  geom_linerange(aes(xmin = x_min, xmax = x_max))  
  scale_y_discrete(labels=c("A","B","C","D", "E"))  
  theme_classic()  
  labs(x = "Value", y = "Class", color = "")  
  theme(legend.position = "none")

Created on 2023-01-01 with enter image description here

  • Related