R ggplot构面-共享y轴,多个不同的x轴 [英] R ggplot facet -- shared y axis, multiple distinct x-axes

查看:828
本文介绍了R ggplot构面-共享y轴,多个不同的x轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由3个变量(a,b,c)和一个响应(d)组成的数据集.

I have a dataset consisting of 3 variables (a, b, c) and one response (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轴的y图,并且三个面分别具有a,b和c的x轴-换句话说,d相对于a,d与b以及d与c彼此相邻且具有共同的y轴.我的目标的近似值可以在R的基础上显示为:

My goal is to use facet in ggplot2 to create a plot with a common y-axis of d, and three facets having x-axes of a, b, and c respectively -- in other words, d vs a, d vs. b, and d vs. c adjacent to each other with a common y-axis. An approximation of my goal can be shown in base R with:

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

我想用ggplot做到这一点,而不必重复y轴.我见过类似的图,但是它们通常涉及一个x轴,重复了几次.我尝试melt数据(即来自reshape2的数据),但是我没有提出适合我的方向.

I'd like to do this with ggplot and without repeating the y-axis. I've seen similar plots, but they usually involve one x-axis repeated several times. I've tried to melt the data (i.e. from reshape2) but I haven't come up with an orientation that works for me.

在视觉上还希望使d vs. a,d vs. b和d vs. c中的每一个具有不同的颜色.

It would also be visually desirable to have each of d vs. a, d vs. b, and d vs. c be a different color.

提前感谢您的帮助!

推荐答案

您处在正确的轨道上.我们需要将df转换为长格式(例如tidyr::gather),然后使用ggplot::facet_grid获得所需的图

You're on the right track. We need to convert df to long format (tidyr::gather for example) then use ggplot::facet_grid to get the desired plot

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-06-07创建.

Created on 2018-06-07 by the reprex package (v0.2.0).

这篇关于R ggplot构面-共享y轴,多个不同的x轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆