【问题标题】:ggplot: position="dodge" questionggplot: position="dodge" 问题
【发布时间】:2010-09-22 22:04:52
【问题描述】:

我有一个相当简单的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")

我想做的是创建一个图表,显示每个“变量”的 top2 和 bottom 并排显示。我正在尝试实现 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

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    这是一个快速的答案

    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)
    

    【讨论】:

    • 那个不行。 a)它过度绘制。 b) 我什至不确定它是否绘制了正确的值。
    • 啊,如果你在 geom_bar() 中添加 ', position="dodge"' 就可以了,谢谢!
    • 哦,对了。我忘记将它包含在我发布的代码中。很高兴你知道了!
    • 你能编辑帖子,让路人得到正确答案吗? :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 2012-07-21
    • 2021-01-16
    • 2022-01-19
    相关资源
    最近更新 更多