本文介绍了使用一个X轴将辅助X轴标签添加到ggplot中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 **编辑,这里有两个很好的解决方案,一个被标记为答案,但是@ hrbrmstr提供了一个很好的解决方案,它结合了两个ggplots,这对于这个简单的情节非常有效。* 以下是代码 breaks.major breaks.minor< - c(30,45,60,75,90)#定义类别的边界(我需要的第二个标签集合) labels.minor lims = c(0,100)g geom_bar()+ coord_flip()+ scale_y_continuous (面板= lims,minor_breaks = breaks.minor,breaks = breaks.major,labels = labels.minor)+ 主题(panel.grid.major.x = element_blank())+ 主题(面板.grid.major.y = element_blank())+ theme(axis.ticks.x = element_blank())+ theme(axis .title = element_blank()) 它会产生这样的情节: 我需要两组X轴标签,一组显示类别名称(即通过 labels.minor )已经存在的满意等等,还有一个显示了 breaks.minor 位置(对应于类别限制,即垂直面板网格线)。我需要当前 labels.minor 标签低于所需的额外标签。 我现在用线条做这个以便数字和类别都在一个长字符串中,但间距会随着剧情大小变得有趣。我可以用文本框(我假设)来做到这一点,在ggplot中有没有办法? 如果您将我当前的标签放在他们部分的中心(例如非常满意不在中心),则可获得额外分数 这是我想要的输出(原谅我的'mspaint') 解决方案也许是这样的。注意的设置扩展了,以处理两个坐标轴的正确间距和类别名称的位置。 图中的标签并非真正偏离中心,而是位于其类别边界的中心。只是在默认情况下,这些轴会进一步扩展。 如果您想要更加美观,您也可以在绘图区域之外进行绘制,但它需要有点烦躁。 **Edit, there are two great solutions here, one is marked as the answer, but @hrbrmstr provides a great solution combining two ggplots which works well for this simple plot.*Here's the codebreaks.major <- c(0,15,37.5,52.5,67.5,82.5,95,100) #defines the midpoints of the categories (label locations)breaks.minor <- c(30,45,60,75,90) #defines the edges of the categories (second label set I need)labels.minor <- c("","Extremely \nDissatisfied","Dissatisfied","Uncertain","Satisfied","Very \nSatisfied","Extremely \nSatisfied","")lims =c(0,100)g <- ggplot(mpg, aes(class))+ geom_bar()+ coord_flip()+ scale_y_continuous(limit = lims, minor_breaks = breaks.minor, breaks = breaks.major, labels = labels.minor) + theme(panel.grid.major.x = element_blank()) + theme(panel.grid.major.y = element_blank()) + theme(axis.ticks.x=element_blank()) + theme(axis.title= element_blank())It produces this plot:I need to have two sets of X-axis labels, one showing the category names (i.e. the "satisfied" etc. that are already there via labels.minor), and one showing the values at the breaks.minor locations (corresponding to the category limits, i.e. the vertical panel grid lines). I need the current labels.minor labels to be below the required additional labels.I currently do this with line breaks so that the numbers and categories are all in one long string, but the spacing gets funny with plot resizes.I could do this with text boxes (I assume), is there a way within ggplot?Extra points if you get my current labels in the centre of their sections (e.g. "Extremely Satisfied" is off-centre)This is my desired output (pardon my 'mspaint') 解决方案 Something like this, perhaps. Note the setting of expand for both axes to deal with proper spacing, and positions of the category names.The labels in your figure aren't really off-centre, they are in the centre of their category boundary. It's just that by default the axes are expanded a bit further.If you want to get more fancy, you can draw outside of the plotting area too, but it requires a bit more fiddeling. This question should get you started.ggplot(mpg, aes(class))+ geom_bar()+ geom_text(data = data.frame(br = breaks.minor), aes(y = br, label = br, x = 7.75), size = 4, col = 'grey30') + coord_flip()+ scale_y_continuous(limit = lims, minor_breaks = breaks.minor, breaks = breaks.major, labels = labels.minor, expand = c(0, 0)) + scale_x_discrete(expand = c(0.05, 0)) + theme(panel.grid.major.x = element_blank()) + theme(panel.grid.major.y = element_blank()) + theme(axis.ticks.x=element_blank()) + theme(axis.title= element_blank()) 这篇关于使用一个X轴将辅助X轴标签添加到ggplot中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-09 19:20