Home > OS >  How to discard the x-axis and place manually factor axis in specified position ggplot2 in R
How to discard the x-axis and place manually factor axis in specified position ggplot2 in R

Time:10-11

I created the following plot in R with ggplot2

KL_tot_RM = c(19.95814, 19.82527, 19.59050, 19.54873, 19.52221, 19.51316, 20.69058,
  20.40126, 19.85393, 19.70881, 19.59630, 19.55224, 21.07645, 20.75875,
  20.09839, 19.88498, 19.69226, 19.60695, 21.87141, 21.53600, 20.77132,
  20.46374, 20.10521, 19.89015, 22.15567, 21.82590, 21.05127, 20.72808,
  20.33150, 20.07257, 22.43661, 22.12112, 21.33892, 21.00751, 20.58678,
  20.29487, 22.66943, 22.37502, 21.66856, 21.33899, 20.90636, 20.59057,
  22.58487, 22.11511, 21.80200, 21.36871, 21.03423, 22.33055, 22.06044,
  21.75075, 22.45573, 22.17089, 20.63523)
KL_tot_BM = c(19.39840, 19.44582, 19.50268, 19.50384, 19.50405, 19.50406, 19.39609,
  19.51611, 19.51505, 19.51001, 19.50653, 19.50530, 19.73257, 19.69439,
 19.55838, 19.53254, 19.51575, 19.50994, 21.00110, 20.71141, 20.07691,
 19.87026, 19.68416, 19.60225, 21.48362, 21.18844, 20.47549, 20.19863,
 19.90049, 19.74088, 21.92624, 21.65443, 20.92355, 20.61059, 20.23113,
 19.99025, 22.26873, 22.11128, 21.42642, 21.10224, 20.68051, 20.38133,
 22.31634, 22.00143, 21.69530, 21.26701, 20.94143, 22.37260, 22.04097,
 21.73265, 19.70630, 22.23376, 19.32922)
df_plot = data.frame(KL = c(KL_tot_RM,KL_tot_BM), x= 1:53, 
                     data_set = c(rep('RM',53),rep('BM',53)) )
ggplot(df_plot, aes(x = x, y = KL, colour = data_set))  
  geom_point(size = 1.8)

enter image description here

what I would like to do it to discard the x-axis, i.e. remove the value 0, 20, 40 and let it blank. And then somehow create the following plot

enter image description here

The dotted vertical lines indicate the groups. What I would like to do is at the middle of each group to place a factor p_{1}, p_{2}, p_{3},....,p_{11} is there a way to do that?

CodePudding user response:

If you just want to remove the axis ticks and text below, just add the element_blank argument to the theme:

ggplot(df_plot, aes(x = x, 
                    y = KL, 
                    colour = data_set))  
  geom_point(size = 1.8) 
  theme(axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

Which gives you this:

enter image description here

To add arbitrary breaks, you can do so with this function:

ggplot(df_plot,
       aes(x = x,
           y = KL, 
           colour = data_set))  
  geom_point(size = 1.8) 
  scale_x_continuous(breaks = c(0,10,20,30,40,50),
                     labels=c("P1",
                            "P2",
                            "P3",
                            "P4",
                            "P5",
                            "P6"))

enter image description here

There is also probably a more nice way of coding this, but you can just add geom_vline for all of these breaks too. If you wanna change the aesthetics, you can do so within the geom_vline function:

ggplot(df_plot,
       aes(x = x,
           y = KL, 
           colour = data_set))  
  geom_point(size = 1.8) 
  scale_x_continuous(breaks = c(0,10,20,30,40,50),
                     labels=c("P1",
                            "P2",
                            "P3",
                            "P4",
                            "P5",
                            "P6")) 
  geom_vline(xintercept = 0) 
  geom_vline(xintercept = 10) 
  geom_vline(xintercept = 20) 
  geom_vline(xintercept = 30) 
  geom_vline(xintercept = 40) 
  geom_vline(xintercept = 50)

Like so:

enter image description here

Edit

Just add the last argument here to throw away the x axis label:

ggplot(df_plot,
       aes(x = x,
           y = KL, 
           colour = data_set))  
  geom_point(size = 1.8) 
  scale_x_continuous(breaks = c(0,10,20,30,40,50),
                     labels=c("P1",
                              "P2",
                              "P3",
                              "P4",
                              "P5",
                              "P6")) 
  geom_vline(xintercept = 0) 
  geom_vline(xintercept = 10) 
  geom_vline(xintercept = 20) 
  geom_vline(xintercept = 30) 
  geom_vline(xintercept = 40) 
  geom_vline(xintercept = 50) 
  labs(x="")

enter image description here

CodePudding user response:

Improved solution which easily allows you to change the interval, and positions the labels in the centre of each bin.

my_interval <- 5
breaks <- seq(0, max(df_plot$x), my_interval)
labels <- breaks   my_interval/2

ggplot(df_plot, aes(x = x, y = KL, colour = data_set))  
  geom_point(size = 1.8)  
  geom_vline(xintercept = breaks, linetype = "dashed")  
  scale_x_continuous(breaks = labels,
                     labels = paste0("P", seq_along(labels)))   
  theme(axis.ticks.x = element_blank(),
        axis.title.x = element_blank())

ggplot2 plot with continuous x axis converted into groups

  •  Tags:  
  • r
  • Related