R ggplot分面--共享y轴,多个不同的x轴。

4

我有一个数据集,包括三个变量(a,b,c)和一个响应变量(d)。

    df <- structure(list(a = c(0.9973, 0.9965, 1.002, 1.0019, 1.001, 1.0031, 
1.0005, 1.0009, 1.0007, 0.9977, 1.0004, 1.001, 0.9936, 0.9945, 
1.0056, 1.0004, 0.9969, 0.9977, 1.001, 0.9876), b = c(-4.0841, 
-4.216, -4.1469, -4.1418, -4.1919, -4.1953, -4.3314, -4.1205, 
-4.2449, -4.0841, -4.276, -4.3396, -4.2885, -4.1386, -4.0689, 
-4.2229, -4.3886, -4.2267, -4.0344, -4.6152), c = c(32.04, 18.52, 
26.01, 25.65, 21.44, 22.26, 21.8, 21.6, 17.38, 13.84, 20.19, 
27.66, 20.85, 18.71, 22.18, 17.25, 26.78, 28.08, 25.9, 16.68), 
    d = c(2241, 2231, 2231, 2220, 2218, 2216, 2210, 2204, 
    2202, 2194, 2157, 2028, 1853, 1850, 1770, 1755, 1679, 
    1673, 1673, 1645)), .Names = c("a", "b", "c", "d"), class = "data.frame", row.names = c(NA, 
-20))

我的目标是在ggplot2中使用facet,创建一个具有共同y轴d的绘图,并且三个分面分别具有x轴a、b和c - 换句话说,d vs a、d vs b和d vs c相邻,具有共同的y轴。 在R基础上,可以通过以下方式近似实现我的目标:

par(mfrow=c(1,3))
plot(df$a, df$d)
plot(df$b, df$d)
plot(df$c, df$d)

我希望能够用ggplot实现此功能,而不重复y轴。我见过类似的图表,但它们通常涉及一个x轴多次重复。我尝试过将数据融合(即使用reshape2),但没有找到适合我的方向。
另外,每个d与a、d与b和d与c之间都有不同的颜色,这也是视觉上的需求。
感谢您提前的帮助!

1
类似这样的吗?https://dev59.com/22Uq5IYBdhLWcg3wJNLg - Amanda
2个回答

3
你走在正确的道路上。我们需要将 df 转换为长格式(例如使用 tidyr::gather),然后使用 ggplot::facet_grid 来得到所需的图形。
df <- structure(list(a = c(0.9973, 0.9965, 1.002, 1.0019, 1.001, 1.0031, 
  1.0005, 1.0009, 1.0007, 0.9977, 1.0004, 1.001, 0.9936, 0.9945, 
  1.0056, 1.0004, 0.9969, 0.9977, 1.001, 0.9876), b = c(-4.0841, 
  -4.216, -4.1469, -4.1418, -4.1919, -4.1953, -4.3314, -4.1205, 
  -4.2449, -4.0841, -4.276, -4.3396, -4.2885, -4.1386, -4.0689, 
  -4.2229, -4.3886, -4.2267, -4.0344, -4.6152), c = c(32.04, 18.52, 
  26.01, 25.65, 21.44, 22.26, 21.8, 21.6, 17.38, 13.84, 20.19, 
  27.66, 20.85, 18.71, 22.18, 17.25, 26.78, 28.08, 25.9, 16.68), 
      d = c(2241, 2231, 2231, 2220, 2218, 2216, 2210, 2204, 
      2202, 2194, 2157, 2028, 1853, 1850, 1770, 1755, 1679, 
      1673, 1673, 1645)), 
  .Names = c("a", "b", "c", "d"), 
  class = "data.frame", row.names = c(NA,-20))

library(tidyverse)

df_long <- df %>% 
  gather(key, value, a:c)

ggplot(df_long, aes(x = d, y = value, color = key)) +
  geom_point() +
  facet_grid(~ key) +
  theme_bw()

ggplot(df_long, aes(x = d, y = value, color = key)) +
  geom_line() +
  facet_grid(~ key)+
  theme_bw()

ggplot(df_long, aes(x = d, y = value, color = key, fill = key)) +
  geom_col() +
  facet_grid(~ key)+
  theme_bw()

这篇文章是由 reprex包(v0.2.0)在2018年6月7日创建的。


谢谢,这很接近,但还不太对。我需要“d”成为y轴(对所有三个图都是共同的),然后从右到左,我希望图表分别具有a、b和c作为x轴。不幸的是,仅仅在代码中翻转x和y会产生不理想的结果:ggplot(df_long, aes(x = key, y = d, color = key)) + geom_point() + facet_grid(~ key) - jeffgoblue

1
感谢Tung和Amanda让我走上了正确的道路。关键在于使用facet_wrap而不是facet_grid。一旦数据按照上述正确的长格式排列:
library(tidyverse) 
df_long <- df %>% 
   gather(key, value, a:c)

那么这个版本就可以解决问题:
ggplot(df_long, aes(x = value, y = d, color = key)) +
    geom_point() +
    facet_wrap(~ key, scales="free_x")

1
你可以使用 facet_grid(~ key, scales = "free_x") 来获得相同的结果。 - Tung

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