facet_gridded 马赛克图上的 Geom_text
我正在尝试将 geom_text 标签添加到下面的马赛克图中:
我使用 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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如安德里所说,没有数据就很难进行测试。但是您希望对第一个问题使用 coord_cartesian 限制,因为比例限制会丢弃超出其范围的数据。这是我的解决方案,我合并了实验室和秤,因为有时它们可能会发生冲突。
华泰
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.
HTH
我通过将 geom_text 分成两半并具有两个函数来得到我想要的。我不知道为什么会这样,但确实如此!
和图表:
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!
And the graph: