龙卷风图/ggplot2 图表
我很难让 this 正确输出...
这就是我的内容到目前为止尝试过:
示例数据:
dat <- data.frame(
variable=c("A","B","A","B"),
Level=c("Top-2","Top-2","Bottom-2","Bottom-2"),
value=c(.2,.3,-.2,-.3)
)
这是迄今为止我得到的最接近的数据:
ggplot(dat, aes(variable, value, fill=Level)) + geom_bar(position="dodge")
## plots offset, as expected
ggplot(dat, aes(variable, value, fill=Level)) + geom_bar(position="stack")
# or geom_bar(), default is stack but it overplots
I'm having a spot of difficulty getting this to output right...
Here's what I've tried so far:
sample data:
dat <- data.frame(
variable=c("A","B","A","B"),
Level=c("Top-2","Top-2","Bottom-2","Bottom-2"),
value=c(.2,.3,-.2,-.3)
)
This is the closest I've got so far:
ggplot(dat, aes(variable, value, fill=Level)) + geom_bar(position="dodge")
## plots offset, as expected
ggplot(dat, aes(variable, value, fill=Level)) + geom_bar(position="stack")
# or geom_bar(), default is stack but it overplots
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
自 2012 年起,ggplot 禁止
错误:将变量映射到y 并且还使用 stat="bin"
。解决方案是:如果您使用非对称示例,它也会非常有帮助,否则您怎么知道您是否没有查看镜像两次的顶级系列?!
给出您想要的龙卷风图:
Since 2012, ggplot forbids
Error: Mapping a variable to y and also using stat="bin"
. The solution is:It also seriously helps if you use a non-symmetric example, otherwise how do you know if you're not looking at the top series mirrored twice?!
gives your desired tornado plot:
您还可以使用
+ coord_flip()
而不是+ geom_bar(position="identity")
You could also use
+ coord_flip()
instead of+ geom_bar(position="identity")
如果负值只是比较两组的技巧,您可以使用:
If the minus values are just a trick to compare two groups, you can use: