在单个图中为一个连续的 Y 变量绘制多个 X 变量
Plotting multiple X variables for one continuous Y variable in a single plot
我正在尝试绘制一个线图,在 x-axis
中有两个 x
变量,在 y-axis
中有一个连续的 y
变量。 x1
和 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 轴上的 df$group
和 df$val
上的不同颜色和图例为 x1
和 x2
设置单独的行y 轴。你能帮我做这个吗?提前致谢。
编辑:在下方添加
这是一种假设意图是将 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
值。如果它们没有按照所描述的方式相互联系,我怀疑将它们绘制成好像它们是一样的会产生误导。 (请参阅 here 了解为什么会出现这种情况的讨论。)
就是说,这就是您可以做到的。首先,这是一种仅按原样使用 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 轴向与主轴相反的方向移动。 from 2017 doesn't seem to work with this version any more. As of Dec 2018, there is a proposed fix 正在审查旨在解决此问题。
我正在尝试绘制一个线图,在 x-axis
中有两个 x
变量,在 y-axis
中有一个连续的 y
变量。 x1
和 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 轴上的 df$group
和 df$val
上的不同颜色和图例为 x1
和 x2
设置单独的行y 轴。你能帮我做这个吗?提前致谢。
编辑:在下方添加
这是一种假设意图是将 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
值。如果它们没有按照所描述的方式相互联系,我怀疑将它们绘制成好像它们是一样的会产生误导。 (请参阅 here 了解为什么会出现这种情况的讨论。)
就是说,这就是您可以做到的。首先,这是一种仅按原样使用 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 轴向与主轴相反的方向移动。