r - ggplot2 中每个方面的不同轴限制

标签 r ggplot2

我正在尝试在 ggplot2 中制作一个多面图其中 y 轴显示标签,x 轴应显示折线图,其中每个标签的值在两个不同的度量中(在不同的尺度上)。到目前为止,我有这个:

Data <- structure(list(label = structure(
  c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
  4L, 5L, 5L, 6L, 6L), .Label = c("A", "B", "C", "D", "E", "F"), class = "factor"), 
  facet = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
  1L, 2L), .Label = c("A", "B"), class = "factor"), value = c(0.0108889081049711, 
  0.37984336540103, 0.0232500876998529, 0.777756493305787, 
  0.0552913920022547, 0.920194681268185, 0.0370863009011373, 
  0.114463779143989, 0.00536034172400832, 0.469208759721369, 
  0.0412159096915275, 0.587875489378348), group = c(1, 1, 1, 
  1, 1, 1, 1, 1, 1, 1, 1, 1)), .Names = c("label", "facet", 
  "value", "group"), row.names = c(NA, -12L), class = "data.frame")

ggplot(Data, aes(x = label, y = value, group = group)) + geom_line() + 
     facet_grid(~ facet, scales = "free") + coord_flip()

这创建了以下情节:
enter image description here

问题是这些措施的尺度不同,我更喜欢 A绘图以具有从 0 到 0.1 的 x 限制和 B绘制从 0 到 1 的 x 限制。我想 scales = "free"应该解决这个问题,但它不会改变情节。

最佳答案

我想出了类似于 df239 的东西:

ggplot(Data, aes(y = label, x = value, group=group)) + geom_path() + 
  facet_wrap( ~ facet, scales = "free")

请注意,您必须使用 geom_path ,并注意您的积分顺序,因为只需切换 xycoord_flip 不一样(正如另一个答案中所述, facet_wrap 不支持)。

enter image description here

关于r - ggplot2 中每个方面的不同轴限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21937127/

相关文章:

重新排列表中的值

r - data.table 按组获取 N 个最频繁的值

r - ggplot2:从包含geom_histogram的绘图对象读取最大条形高度

r - 逻辑回归的解释和绘图

r - 从二项分布生成相关随机数

r - Plotly R 的 slider

json - 使用jsonlite包解析JSON文件时出错

r - 如何用文本和点注释R图

r - 我如何权衡 stat_smooth 与 ggplot2 中值数的倒数

r - 如何使用 ggplot2 在轴上仅显示整数值