ggplot2 标记多层条形图

发布于 2024-09-25 00:37:49 字数 658 浏览 0 评论 0原文

我想做的是用各自的数据标签来标记以下示例中的两个 geom_bar() 。到目前为止,我只能显示其中一个:

dput():

x <- structure(list(variable = c("a", "b", "c"), f = c(0.98, 0.66, 
0.34), m = c(0.75760989010989, 0.24890977443609, 0.175125)), .Names = c("variable", 
"f", "m"), row.names = c(NA, 3L), class = "data.frame")

ggplot(x, aes(variable, f, label=paste(f*100,"%", sep=""))) + 
geom_bar() + 
geom_text(size=3, hjust=1.3, colour="white") +
geom_bar(aes(variable, m, label=paste(m*100,"%",sep="")), fill="purple") + 
coord_flip()

Multilayered Bar Graph

看看如何黑色里面有一个数据标签。在同一个图中,我想在紫色的内部做同样的事情。

What I'd like to do is label both of the geom_bar() 's in the following example with their respective data labels. So far, I can only get one or the other to show up:

dput():

x <- structure(list(variable = c("a", "b", "c"), f = c(0.98, 0.66, 
0.34), m = c(0.75760989010989, 0.24890977443609, 0.175125)), .Names = c("variable", 
"f", "m"), row.names = c(NA, 3L), class = "data.frame")

ggplot(x, aes(variable, f, label=paste(f*100,"%", sep=""))) + 
geom_bar() + 
geom_text(size=3, hjust=1.3, colour="white") +
geom_bar(aes(variable, m, label=paste(m*100,"%",sep="")), fill="purple") + 
coord_flip()

Multilayered Bar Graph

See how on the inside of the black there is a data label. In the same plot, I'd like to do the same thing on the inside of the purple.

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

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

发布评论

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

评论(2

瞎闹 2024-10-02 00:37:49

这对你有什么作用:

ggplot(x, aes(variable, f, label=paste(f*100,"%", sep=""))) + 
geom_bar() + 
geom_text(size=3, hjust=1.3, colour="white") +
geom_bar(aes(variable, m, label=paste(m*100,"%",sep="")), fill="purple") + 
geom_text(aes(y=m,label=paste(round(m*100),"%",sep="")),size=3, hjust=1.3, colour="white") +
coord_flip()

how does this work for you:

ggplot(x, aes(variable, f, label=paste(f*100,"%", sep=""))) + 
geom_bar() + 
geom_text(size=3, hjust=1.3, colour="white") +
geom_bar(aes(variable, m, label=paste(m*100,"%",sep="")), fill="purple") + 
geom_text(aes(y=m,label=paste(round(m*100),"%",sep="")),size=3, hjust=1.3, colour="white") +
coord_flip()
孤蝉 2024-10-02 00:37:49

您可能需要考虑不同的方法。

如果图表的主要目标是比较条形的长度/位置,则条形中包含数字会产生“模糊”条形,使眼睛/大脑更难正确判断条形的长度/位置。

如果主要目标是比较数字,那么您所拥有的就是一个布局不佳的表格(具有彩色背景),如果数字排列正确,比较数字会更容易。

一些替代方案包括使用单独的图表和表格(均正确布局),或者将数字放在边距中(正确对齐)而不是放在条形图的顶部,或者创建一个在边缘单元格中包含条形图的表格。您可能还想考虑点图而不是条形图(ggplot2 应该仍然可以轻松做到这一点),堆叠条形图允许仅对第一个(最左边)类别和总数进行合理比较,在您的示例中,比较顶部和底部条形黑色部分的相对大小。

You may want to consider a different approach.

If the main goal of the graph is to compare the lengths/positions of the bars, then including numbers in the bars produces "fuzzy" bars making it harder for the eye/brain to properly judge the length/position of the bar.

If the main goal is to compare the numbers, then what you have is a poorly laid out table (with colorful background), it is easier to compare the numbers if they line up properly.

Some alternatives would include having a seperate graph and table (both properly laid out), or put the numbers in the margin (properly aligned) rather than on top of the bars, or create a table with barplots in marginal cells. You may also want to consider a dot plot rather than a bar plot (ggplot2 should still do this easily), stacked bars allow reasonable comparison of only the 1st (leftmost) category and the total, in your example it is not easy to compare the relative size of the black portion of the top and bottom bars.

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