Home > Blockchain >  How do I fix in ggplot my x and y - axis with all dates and id numbers reflecting correctly?
How do I fix in ggplot my x and y - axis with all dates and id numbers reflecting correctly?

Time:10-29

I would like to visualize the following data in R in the form of a dot plot, with id number on the y-axis and date on the x-axis.

I used the following code:

ggplot(daily_activity_outliers, aes(x=date, y=id)) 
  geom_point()  
  theme(axis.text.x=element_text(angle=90))

and received the following output:

enter image description here

I would like all the dates to appear on the x-axis, and also all 4 ids to appear on the y-axis (as is, and not chronologically) but I cannot find a way. The dataset looks as follows:

enter image description here

CodePudding user response:

Try passing the y axis (ids) as factor (or character).

library(ggplot2)

df <- 
data.frame(id = c(2347167796, 2347167796, 2347167796, 2347167796, 
                  2347167796, 2347167796, 2347167796, 2347167796, 2347167796, 3372868164, 
                  3372868164, 3372868164, 3372868164, 3372868164, 3372868164, 3372868164, 
                  3372868164, 3372868164, 3372868164, 4057192912, 4057192912, 4057192912, 
                  4057192912, 8253242879, 8253242879), 
           date = structure(
                c(16903, 16904, 16905, 16906, 16907, 16908, 16909, 16910, 16911, 16903, 
                  16904, 16905, 16906, 16907, 16908, 16909, 16910, 16911, 16912, 
                  16903, 16904, 16905, 16906, 16903, 16904), 
                class = "Date"))

ggplot(data = df, 
       aes(x = date, 
           y = as.factor(id)))  
  geom_point()  
  labs(y = "id")

enter image description here

Created on 2022-10-28 with reprex v2.0.2

  • Related