使用ggplot2绘制飓风图/图表

4

我遇到了一点困难,无法正确输出这个...

到目前为止,我尝试过以下方法:

样本数据:

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

自2012年以来,此代码已经失效,ggplot禁止这样做:Error: Mapping a variable to y and also using stat="bin" ... If you want y to represent counts of cases, use stat="bin" and don't map a variable to y. 请参见ggplot2 mapping variable to y and using stat=“bin” - smci
3个回答

7

自2012年起,ggplot禁止Error: Mapping a variable to y and also using stat="bin"。解决方法如下:

ggplot(dat, aes(variable, value, fill=Level)) +
    geom_bar(position="identity", stat="identity") 

如果您使用非对称的示例,那么这也会严重有助于您,否则您怎么知道您是否正在查看两次镜像的顶部系列?!

dat <- data.frame(
  variable=c("A","B","A","B"),
  Level=c("Top-2","Top-2","Bottom-2","Bottom-2"),
  value=c(.8,.7,-.2,-.3)
  )

给您所需的龙卷风图:

您需要的龙卷风图


1
感谢您的帖子,2k12的答案。 - Brandon Bertelsen
@BrandonBertelsen:没问题。只是想让这个答案网站保持活力和实用性... - smci
1
这是真心的感谢,不是虚假的 - 如果不清楚的话。干得好! - Brandon Bertelsen

2
你也可以使用+ coord_flip()代替+ geom_bar(position="identity")

-1
如果负值只是用来比较两组数据的技巧,你可以使用以下代码:
scale_y_continuous(labels=abs)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接