r - 如何在 ggplot2 中同时使用 coord_carteisan 和 coord_flip

标签 r ggplot2

我有一些来自 ggplot 的奇怪行为。这是一个MWE:

the_data <- data.frame(
   myx <- 1:10,
  lower <- rnorm(10,-5,1),
  mean <- rnorm(10,0,.5),
  upper <- rnorm(10,5,1))
the_data2 <- data.frame(
  myx <- 1:10,
  lower <- rnorm(10,-5,1),
  mean <- rnorm(10,0,.5),
  upper <- rnorm(10,5,1))

现在,我想构建一个图,其中最终产品将具有一个均值点,以及一条从下到上绘制的线。但我希望这些线是水平的。我还想在图表上“放大”,以便只显示从 -1 到 1 的值。我需要使用 coord_cartesian因为如果我使用 ylim它会丢弃图表之外的数据点,从而弄乱线条。但是当我运行时:
ggplot() +
  geom_pointrange(aes(x=myx, y=mean, ymin=lower, ymax=upper), data=the_data) +
  geom_pointrange(aes(x=myx, y=mean, ymin=lower, ymax=upper), data=the_data2) +
  coord_cartesian(ylim = c(-1, 1)) +
  coord_flip() 

它不应用“缩放”和切换两个参数:
ggplot() +
  geom_pointrange(aes(x=myx, y=mean, ymin=lower, ymax=upper), data=the_data) +
  geom_pointrange(aes(x=myx, y=mean, ymin=lower, ymax=upper), data=the_data2) +
  coord_flip() +
  coord_cartesian(ylim = c(-1, 1)) 

应用缩放但不应用翻转。这里发生了什么?

最佳答案

coord_flipcoord_cartesian 的包装器.你给 coord_cartesian 打了两次电话第二个覆盖第一个。你可以这样做:

ggplot() +
  geom_pointrange(aes(x=myx, y=mean, ymin=lower, ymax=upper), data=the_data) +
  geom_pointrange(aes(x=myx, y=mean, ymin=lower, ymax=upper), data=the_data2) +
  coord_flip(ylim = c(-1, 1))

关于r - 如何在 ggplot2 中同时使用 coord_carteisan 和 coord_flip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33393803/

相关文章:

r - Knitr PDF 可以,但没有内容和错误消息

r - 在 purrr :map call 中对函数使用准引号参数

c++ - 在包 BAR 中使用 R 包 FOO 中的 C++ 代码的最佳方法

r - 绘制一个图表,表示每条弧线的权重

r - 抑制 R 中的 rgdal 警告

r - 饼图,其中标签显示在饼图内部,百分比显示在饼图外部

r - 时间数据的ggplot2和chron条形图scale_x_chron

r - 将社区网络绘制成 ggplot map

在 ObserveEvent 内部渲染(动态绘图生成)

r - Weibull 函数作为直线(R 中的 ggplot2 的双对数)