facet_gridded 马赛克图上的 Geom_text

发布于 2024-11-17 10:39:09 字数 991 浏览 1 评论 0原文

我正在尝试将 geom_text 标签添加到下面的马赛克图中:

Mosaic Plot

我使用 ggplot2 生成的代码如下

tsc.p1 <- tsc.p + geom_rect(colour = I("grey")) +
          facet_grid(helmet~.) +
          geom_text(aes(x = c(9.0, 22.0, 33.0, 46.0, 72.0, 98.0),
                        y = 125,
                        label = c("C", "DS", "S", "ST", "Std", "T")),
                        size = 3) +
          scale_fill_brewer(palette = "Greys") +
          xlab("Percentage of Sample") +
          ylab("Percentage Responded") +
          opts(title="Mosaic Plot of Helmet Type Use",
               legend.position="none") +
          scale_x_continuous(expand = c(0, 0)) +
          scale_y_continuous(expand = c(0, 125)) +
          ylim(0, 101)

:有两个问题:

  • ylim 在顶部切断了我的 geom_text 。
  • 如果没有 ylim() 函数,前三个类别将显示在奇数编号的面上方,后三个类别将显示在偶数编号的面上方(从最上面的面开始计数)。我不明白为什么会出现这种情况。

我只想将六个类别添加到图的顶部。有办法做到这一点吗?

I am trying to add geom_text labels to my mosaic plot below:

Mosaic Plot

that I generated using ggplot2 with the code below:

tsc.p1 <- tsc.p + geom_rect(colour = I("grey")) +
          facet_grid(helmet~.) +
          geom_text(aes(x = c(9.0, 22.0, 33.0, 46.0, 72.0, 98.0),
                        y = 125,
                        label = c("C", "DS", "S", "ST", "Std", "T")),
                        size = 3) +
          scale_fill_brewer(palette = "Greys") +
          xlab("Percentage of Sample") +
          ylab("Percentage Responded") +
          opts(title="Mosaic Plot of Helmet Type Use",
               legend.position="none") +
          scale_x_continuous(expand = c(0, 0)) +
          scale_y_continuous(expand = c(0, 125)) +
          ylim(0, 101)

I have two problems:

  • The ylim cuts off my geom_text at the top.
  • Without the ylim() function, the first three categories are shown above the odd numbered facets, and the last three categories are shown above the even numbered facets, counting from the top-most facet. I couldn't figure out how or why this is the case.

I only want to add the six categories to the top of the plot. Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经大条 2024-11-24 10:39:09

正如安德里所说,没有数据就很难进行测试。但是您希望对第一个问题使用 coord_cartesian 限制,因为比例限制会丢弃超出其范围的数据。这是我的解决方案,我合并了实验室和秤,因为有时它们可​​能会发生冲突。

tsc.p1 <- tsc.p + geom_rect(colour = I("grey")) +
          facet_grid(helmet~.) +
          geom_text(aes(x = c(9.0, 22.0, 33.0, 46.0, 72.0, 98.0),
                        y = 125,
                        label = c("C", "DS", "S", "ST", "Std", "T")),
                        size = 3) +
          scale_fill_brewer(palette = "Greys") +
          opts(title="Mosaic Plot of Helmet Type Use",
               legend.position="none") +
          scale_x_continuous("Percentage of Sample", expand = c(0, 0)) +
          scale_y_continuous("Percentage Responded", expand = c(0, 125)) +
          cood_cartesian(ylim = c(0, 101))

华泰

As Andrie says it's hard to test with out data. But you want to use coord_cartesian limits for your first questinion, as scale limits throws away data that's out it range. Here's my solution, I merged the labs and the scales because sometimes they can collide.

tsc.p1 <- tsc.p + geom_rect(colour = I("grey")) +
          facet_grid(helmet~.) +
          geom_text(aes(x = c(9.0, 22.0, 33.0, 46.0, 72.0, 98.0),
                        y = 125,
                        label = c("C", "DS", "S", "ST", "Std", "T")),
                        size = 3) +
          scale_fill_brewer(palette = "Greys") +
          opts(title="Mosaic Plot of Helmet Type Use",
               legend.position="none") +
          scale_x_continuous("Percentage of Sample", expand = c(0, 0)) +
          scale_y_continuous("Percentage Responded", expand = c(0, 125)) +
          cood_cartesian(ylim = c(0, 101))

HTH

神妖 2024-11-24 10:39:09

我通过将 geom_text 分成两半并具有两个函数来得到我想要的。我不知道为什么会这样,但确实如此!

tsc.p <- ggplot(tsc, 
                aes(ymin = ymin, ymax = ymax,
                    xmin = xmin, xmax = xmax,
                    fill = variable))
tsc.p1 <- tsc.p + geom_rect(colour = I("grey")) +
          facet_grid(helmet~.) +
          geom_text(aes(x = c(9.0, 22.0, 33.0),
                        y = 25,
                        label = ifelse(helmet == "FF",
                                       c("Cru", "DualSp", "Sport"),
                                       "")),
                        size = 3) +
          geom_text(aes(x = c(45.0, 72.0, 97.0),
                        y = 25,
                        label = ifelse(helmet == "FF",
                                       c("SptTour", "Std", "Tour"),
                                       "")),
                        size = 3) +
          scale_fill_brewer("Frequency of Helmet Use", palette = "Greys") +
          xlab("Percentage of Sample") +
          ylab("Percentage Responded") +
          opts(title="Mosaic Plot of Helmet Use by Helmet Type") +
          scale_x_continuous(expand = c(0, 0)) +
          scale_y_continuous(expand = c(0, 101)) +
          ylim(0, 101)

和图表:

Mosaic Plot

I got what I wanted by just splitting up the geom_text in half and having two functions. I don't know why this works, but it does!

tsc.p <- ggplot(tsc, 
                aes(ymin = ymin, ymax = ymax,
                    xmin = xmin, xmax = xmax,
                    fill = variable))
tsc.p1 <- tsc.p + geom_rect(colour = I("grey")) +
          facet_grid(helmet~.) +
          geom_text(aes(x = c(9.0, 22.0, 33.0),
                        y = 25,
                        label = ifelse(helmet == "FF",
                                       c("Cru", "DualSp", "Sport"),
                                       "")),
                        size = 3) +
          geom_text(aes(x = c(45.0, 72.0, 97.0),
                        y = 25,
                        label = ifelse(helmet == "FF",
                                       c("SptTour", "Std", "Tour"),
                                       "")),
                        size = 3) +
          scale_fill_brewer("Frequency of Helmet Use", palette = "Greys") +
          xlab("Percentage of Sample") +
          ylab("Percentage Responded") +
          opts(title="Mosaic Plot of Helmet Use by Helmet Type") +
          scale_x_continuous(expand = c(0, 0)) +
          scale_y_continuous(expand = c(0, 101)) +
          ylim(0, 101)

And the graph:

Mosaic Plot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文