在ggplot中添加更多的x-axix标签 [英] Adding more x-axix labels in ggplot

查看:146
本文介绍了在ggplot中添加更多的x-axix标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  plotMeansDouble<  -  function(data,labX)
{$ (数据$类型,数据$分数,平均数)),\ n(N =,levels(stats :: reorder(data $ N,data $ data,mean)),/,levels(stats :: reorder(data $ TN,data $ score,mean)),),sep =)
ggplot(data,aes(x = measyle,y =难度,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)
}

这段代码如下绘制我的图:

在这幅图中我想绘制两种课程的类型和分数之间的关系,迄今为止都非常好。但现在我想分别在A,B和C以下添加第二个x轴标签,以显示每种类型的观察数量。请注意,在代码中,我评论了 scale_x_discrete 。我知道这个函数允许我在每个级别下添加一些东西。但问题是我有两门课程DSP和RP。因此,我想在x标签A,B,C下为两个课程添加观察次数,最好用绿色和黄色着色以表示两个课程,这对于 scale_x_discrete



我认为一个解决方案可以在当前的一个下添加两个额外的x轴,每个都有两个过程的标签。是否可以通过 ggplot2

解决方案

来实现。 geom_text 来实现这一点。以下代码受到此问题的强烈影响。请注意,因为您的问题中没有示例数据,所以我制作了自己的重现性例子

 #load ggplot 
require(ggplot2)
require(grid)
#创建示例数据
set.seed(42)
df < - data.frame(Type = LETTERS [1:3],
Score = runif(6),$
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))
#绘制命令
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)+#为第一课添加文本
geom_text(data = text.b, aes(label = text),vjust = 4.5,show_guide = FALSE)+#为第二课程添加文本
theme(plot.margin = unit(c(1,1,2,1),lines)) +#制作足够的空间
scale_x_discrete(name ='\\\
\\\
\\\
Type')#按下图例

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


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 plot my graph like this:

In this plot I want to plot the relationship between Type and Score for two courses, so far so good. But now I would like to add a second x-axis lables below A, B and C respectively to show the number of observations for each type. Note that in the code I commented the scale_x_discrete. I know this function 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 x labels A,B,C, preferably colored with green and yellow to represent two courses, which does not seem to be possible with scale_x_discrete.

I think a solution could be add two additional x-axis under the current one, each with labels of the two course. Is it possible to achieve this with ggplot2?

解决方案

You can use geom_text to achieve this. The following code is strongly influenced by this question. Note that because there 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)

这篇关于在ggplot中添加更多的x-axix标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆