如何用ggplot2在轴标签上显示分层结构? [英] How to show a hierarchical structure on the axis labels with ggplot2?

查看:416
本文介绍了如何用ggplot2在轴标签上显示分层结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用ggplot2创建一个这样的情节:

(我在这里做了一个小错误,导致不是所有的标签都显示出来,显然我希望所有的标签都显示出来:))
但是,我的Google搜索没有任何结果。我得到的最接近的是来自plotrix的barNest,但是,我不喜欢所制作的图表的质量。 ggplot2看起来好多了。

在理想的情况下,我希望能够做到这样的事情:
scale_x_discrete(Axis Title ,labels = Level1 + Level2 + Level3)
然后Level1,Level2和Level3应该是在qplot函数中使用的数据框的列。
如果我能得到这个工作,我想创建一个函数来将这种类型的功能添加到尺度函数中。也许scale_x_hierarchical会更好。
使用以下代码可以生成类似的数据:

 数据<  -  data.frame(
Subject = c(rep(1,48),rep(2,48),rep(3,48),rep(4,48)),
month = rep(c(rep(1,12), rep(4,12),rep(7,12),rep(10,12)),4),
day = rep(c(rep(1,4),rep(11,4),rep (21,4)),16),
小时= rep(c(0,6,12,18),48),
data = rnorm(192))

顺便说一下,这里使用的图片是使用Spotfire创建的。

解决方案

这有点不同,但是LearnR博客有一个 post 将表格链接到图表,使其看起来像这:


I would like to create a plot like this with ggplot2:

(I made a small error here that caused not all labels to show up. obviously I do want all labels to show up :) ) However, my Google search didn't turn up anything. The closest I got was with barNest from plotrix, however, I don't like the quality of the graphs that are produced. The ggplot2 ones look a lot better.

In the ideal case, I would have expected to be able to do something like this: scale_x_discrete("Axis Title", labels=Level1+Level2+Level3) And then Level1, Level2 and Level3 would have been columns of the data frame that was used in the qplot function. If I can get this to work, I want to create a function to add this type of functionality to the scales function. Maybe scale_x_hierarchical would be better. Similar data can be generated with the following code:

data <- data.frame(
  Subject = c(rep(1, 48), rep(2, 48), rep(3, 48), rep(4, 48)), 
  month = rep(c(rep(1, 12), rep(4, 12), rep(7, 12), rep(10, 12)), 4), 
  day = rep(c(rep(1, 4), rep(11, 4), rep(21, 4)), 16), 
  hour = rep(c(0, 6, 12, 18), 48), 
  data = rnorm(192))

By the way, the image used here is created with Spotfire.

解决方案

It's a little different, but the LearnR blog has a post on linking a table to a graph so that it looks like this:

这篇关于如何用ggplot2在轴标签上显示分层结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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