首页 > 解决方案 > 在单个图中为一个连续 Y 变量绘制多个 X 变量

问题描述

我正在尝试绘制一个线图,其中包含两个x变量,x-axis其中一个连续y变量y-axisx1和的计数x2不同。df看起来如下-

df <- structure(list(val = c(3817,2428,6160,6729,7151,7451,6272,7146,7063,6344,5465,6169,7315,6888,7167,6759,4903,6461,7010,7018,6920,3644,6541,31862,31186,28090,28488,29349,28284,25815,23529,20097,19945,22118), type = c("1wt", "1wt", "3wt", "3wt", "3wt", "5wt", "5wt", "7wt", "7wt", "7wt","10wt","10wt","10wt","15wt","15wt","20wt","20wt","25wt","25wt","25wt","30wt","30wt","30wt","20m","20m","15m","15m","15m","10m","10m","5m", "5m", "5m", "5m"), group = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B")), row.names = c(NA, 34L), class = "data.frame")

其中x变量是-

x1 <- factor(df$type, levels = c("1wt", "3wt", "5wt", "7wt", "10wt", "15wt", "20wt", "25wt", "30wt"))

x2 <- factor(df$type, levels = c("20m", "15m","10m","5m"))

我想根据 x 轴和y 轴的不同颜色和图例为x1和设置单独的线。你能帮我做这件事吗?提前致谢。x2df$groupdf$val

标签: rggplot2plotrbindmelt

解决方案


编辑:在下面添加

这是一种假设意图是将组 A 的可能类型值的范围与来自组 B 的可能值的范围进行映射的方法。

可以手动添加标签,但我认为没有任何简单的方法可以在一个图中同时使用两个分类 x 轴。

df2 <- df %>%
  mutate(x = case_when(type == "1wt" ~ 0,
                       type == "3wt" ~ 1,
                       type == "5wt" ~ 2,
                       type == "7wt" ~ 3,
                       type == "10wt" ~ 4,
                       type == "15wt" ~ 5,
                       type == "20wt" ~ 6,
                       type == "25wt" ~ 7,
                       type == "30wt" ~ 8,

                       type == "20m"  ~ 0/3 * 8,
                       type == "15m"  ~ 1/3 * 8,
                       type == "10m"  ~ 2/3 * 8,
                       type == "5m"   ~ 3/3 * 8))


ggplot(df2, aes(x, val, color = group, group = group)) + 
  geom_point() +
  geom_smooth(method = lm)

在此处输入图像描述

第二种方法

听起来 OP 想以type某种方式以数字方式使用这些值。如果它们没有以所描述的方式内在地相互联系,我怀疑将它们描绘成好像它们是一样的会产生误导。(有关为什么这很麻烦的讨论,请参见此处。)

也就是说,这就是你如何做到的。首先,这是一种仅使用原样数字部分的type方法。请注意,与组 B 关联的“m”位于底部,而“wt”位于顶部,与组 A 关联,如下面的 OP 注释中添加的示例所示。我在坐标轴上添加了颜色以澄清这一点。这在视觉上有点违反直觉,因为与顶轴相关的点在底部,反之亦然。

df2 <- df %>%
  # First, let's take the number used in "type" without adjustment
  mutate(x_unadj = parse_number(type))

ggplot(df2, aes(x_unadj, val, color = group, group = group)) + 
  geom_point() +
  geom_smooth(method = lm) + # Feel free to use other smoothing method, but
                             # not obvious to me what would be improvement.
  scale_x_continuous("m", sec.axis = sec_axis(~., name = "wt")) +
  theme(axis.text.x.bottom  = element_text(color = "#00BFC4"),
        axis.title.x.bottom = element_text(color = "#00BFC4"),
        axis.text.x.top     = element_text(color = "#F8766D"),
        axis.title.x.top    = element_text(color = "#F8766D"))

在此处输入图像描述

如果这不令人满意,我们可以使用颠倒两个轴的顺序

scale_x_reverse("m", sec.axis = sec_axis(~., name = "wt")) +

在此处输入图像描述

使用 ggplot 3.1.0(从 2018 年 10 月开始),我无法让辅助 x 轴在与主轴相反的方向上移动。这个2017 年的例子似乎不再适用于这个版本。截至 2018 年 12 月,正在审查一项旨在解决此问题的建议修复程序。


推荐阅读