Different font faces and sizes within label text entries in ggplot2

后端 未结 3 438
清歌不尽
清歌不尽 2020-12-14 19:52

I am building charts that have two lines in the axis text. The first line contains the group name, the second line contains that group population. I build my axis labels as

3条回答
  •  余生分开走
    2020-12-14 20:39

    I found another simple solution below:

    require(ggplot2)
    Treatment <- rep(c('T','C'),each=2)
    Gender <- rep(c('Male','Female'),2)
    Response <- sample(1:100,4)
    test_df <- data.frame(Treatment, Gender, Response)
    
    xbreaks <- levels(test_df$Gender)
    xlabels[1] <- expression(atop(bold(Female), scriptstyle("POP1")))
    xlabels[2] <- expression(atop(bold(Male), scriptstyle("POP2")))
    
    hist <- ggplot(test_df, aes(x=Gender, y=Response, fill=Treatment,
    stat="identity"))
    hist +
      geom_bar(position = "dodge") +
      scale_y_continuous(limits = c(0, 100), name = "") +
      scale_x_discrete(label = xlabels, breaks = xbreaks) +
      opts(
        axis.text.x = theme_text(size = 12)
      )
    

    another solution

提交回复
热议问题