【问题标题】:Multiple plot with ggplot2: controlling the scale of each plot [duplicate]ggplot2的多图:控制每个图的比例[重复]
【发布时间】:2013-07-30 17:21:55
【问题描述】:

我有以下类型的数据(在 data.frame df 内):

x     A      B      C
1    100    1.2    1.3
2    90     1.1    0.9

我想要一个列ABC 的列df 的多条形图,所以我运行以下命令:

tmp <- cbind(df$x, melt(df[,-1]))
names(tmp) <- c("x", "variable", "value")
ggplot(tmp, aes(x,value)) + geom_bar(stat = "identity" ) + facet_grid(variable~.)

它工作正常,除了 A 列中的值比其他列中的值高得多,并且比例不适应。 有人可以给我一个提示来调整每个情节的比例吗?

非常感谢!

【问题讨论】:

  • 未来,请包含运行代码所需的 library 语句,并使用 dput 或更容易重现的数据表示。

标签: r ggplot2


【解决方案1】:

您需要将scales='free_y' 添加到facet_grid,如?facet_grid 中所述...

library(ggplot2)
library(reshape2)

df <- read.data('clipboard', header=TRUE)

tmp <- cbind(df$x, melt(df[, -1]))
names(tmp) <- c("x", "variable", "value")

ggplot(tmp, aes(x=x)) + 
  geom_bar(stat = "identity" ) + 
  facet_grid(variable ~ ., scales='free_y')

【讨论】:

  • 我试过你的答案,但我明白了:Error in exists(name, envir = env, mode = mode) : argument "env" is missing, with no default
  • 这是正确的答案,正如贾斯汀在开头所说的那样:ggplot(tmp, aes(x,value)) + geom_bar(stat = "identity" ) + facet_grid(variable~., scales= 'free_y')
猜你喜欢
  • 2013-11-23
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 2018-04-14
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
相关资源
最近更新 更多