Adding more x-axix labels to ggplot

I have a function like this:

plotMeansDouble <- function(data, labX)
{
  #xlabs <- paste(levels(stats::reorder(data$type, data$score,mean)),"\n(N=",levels(stats::reorder(data$N, data$score,mean)),"/",levels(stats::reorder(data$TN, data$score,mean)),")",sep="")
  ggplot(data, aes(x=microstyle, y=difficulty, ymax = Upper.CI, group= course, color=course)) + 
  geom_errorbar(aes(ymin=Lower.CI, ymax = Upper.CI ), width=.1, position=position_dodge(.2)) + 
  geom_line(, position=position_dodge(.2)) + 
  geom_text(aes(y=Upper.CI,label = pointlabel, vjust=-1),position=position_dodge(.2)) + 
  geom_point(size=3, shape=21, position=position_dodge(.2))+
  labs(x = labX, y = "Score") + 
  theme_bw()+ 
  theme(panel.grid.major = element_blank(), panel.border = element_blank(),axis.text=element_text(size=14), axis.title=element_text(size=18),axis.text.x=element_text(size=16, angle=40, vjust=.8, hjust=1.01)) #+ scale_x_discrete(labels=xlabs)
}

      

This code draws my graph like this: enter image description here

In this plot, I want to build a connection between type and score for two courses that are so good. But now I would like to add a second X-axis table below A, B and C respectively to show the number of observations for each type. Please note that in the code I have commented out scale_x_discrete

. I know this feature allows me to add something under each level. But the problem is that I have two courses DSP and RP. So I would like to add the number of observations for both two courses under the x labels A, B, C, preferably colored green and yellow to represent the two courses, which is not possible with scale_x_discrete

.

I think the solution would be to add two additional X-axes below the current one, each with two course labels. Can this be achieved with ggplot2

?

0


source to share


1 answer


You can use geom_text

to achieve this. The following code is highly dependent on this question . Please note that since there is no sample data in your question, I made my own reproducible example .

# load ggplot
require(ggplot2)
require(grid)
# creating sample data
set.seed(42)
df <- data.frame(Type = LETTERS[1:3], 
                 Score = runif(6), 
                 course = letters[1:2])
# data for text labels
text.a <- data.frame(Type = LETTERS[1:3], 
                      Score = -Inf,
                      course = 'a',
                      text = paste0('N=', 1:3))
text.b <- data.frame(Type = LETTERS[1:3], 
                     Score = -Inf,
                     course = 'b',
                     text = paste0('N=', 2:4))
# plotting commands
p <- ggplot(df, aes(Type, Score, color=course, group=course)) + 
  geom_point() +
  geom_line() +
  geom_text(data=text.a, aes(label = text), vjust=3, show_guide  = FALSE) +  # adding text for first course
  geom_text(data=text.b, aes(label = text), vjust=4.5, show_guide = FALSE) + # adding text for second course
  theme(plot.margin = unit(c(1,1,2,1), "lines")) + # making enough room 
  scale_x_discrete(name='\n\n\nType') # pushing down the legend

# turns clipping off
gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name == "panel"] <- "off"
grid.draw(gt)

      



enter image description here

+3


source







All Articles