ggplot:位置=“闪避”问题

发布于 2024-09-24 12:56:04 字数 780 浏览 2 评论 0原文

我有一个相当简单的 data.frame dput(x),如下所示:

x <- structure(list(variable = c("a", "b", "c", "d", "e", "f", "g", 
"h", "i", "j", "k", "l", "m", "n", "o", "p"), top2 = c(0.51, 
0.24, 0.55, 0.36, 0.56, 0.67, 0.36, 0.22, 0.48, 0.6, 0.59, 0.06, 
0.04, 0.2, 0.26, 0.25), bottom = c(0.03, 0.05, 0.03, 0.01, 0.02, 
0.03, 0.01, 0.05, 0.03, 0, 0.03, 0.2, 0.11, 0.06, 0.16, 0.07)), .Names = c("variable", 
"top2", "bottom"), row.names = c(NA, -16L), class = "data.frame")

我想做的是创建一个图表,显示每个“变量”的顶部2 和底部。我正在尝试实现position =“dodge”,但到目前为止它似乎在很大程度上被ggplot2忽略了

ggplot() + geom_bar(aes(variable, top2), data=x, position="dodge") +
geom_bar(aes(x$variable, x$bottom), data=x, position="dodge", fill="pink") + 
coord_flip()

粉红色没有留下:P

I have a fairly simple data.frame dput(x), below:

x <- structure(list(variable = c("a", "b", "c", "d", "e", "f", "g", 
"h", "i", "j", "k", "l", "m", "n", "o", "p"), top2 = c(0.51, 
0.24, 0.55, 0.36, 0.56, 0.67, 0.36, 0.22, 0.48, 0.6, 0.59, 0.06, 
0.04, 0.2, 0.26, 0.25), bottom = c(0.03, 0.05, 0.03, 0.01, 0.02, 
0.03, 0.01, 0.05, 0.03, 0, 0.03, 0.2, 0.11, 0.06, 0.16, 0.07)), .Names = c("variable", 
"top2", "bottom"), row.names = c(NA, -16L), class = "data.frame")

What I'd like to do is create a graph that shows top2 and bottom beside each other for each "variable". I'm trying to implement position="dodge" but so far it seems to be largely ignored by ggplot2

ggplot() + geom_bar(aes(variable, top2), data=x, position="dodge") +
geom_bar(aes(x$variable, x$bottom), data=x, position="dodge", fill="pink") + 
coord_flip()

The pink isn't staying :P

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

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

发布评论

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

评论(1

删除会话 2024-10-01 12:56:04

这是一个快速答案

df = melt(x, id = 'variable');
names(df) = c('variable', 'topbot', 'value');
pl1 = ggplot(df, aes(x = variable, y = value)) +
      geom_bar(aes(fill = topbot, position = 'dodge')) +
      coord_flip() +
      scale_fill_manual(values = c('red', 'blue'));
print(pl1)

here is a quick answer

df = melt(x, id = 'variable');
names(df) = c('variable', 'topbot', 'value');
pl1 = ggplot(df, aes(x = variable, y = value)) +
      geom_bar(aes(fill = topbot, position = 'dodge')) +
      coord_flip() +
      scale_fill_manual(values = c('red', 'blue'));
print(pl1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文