r - 在单个图中为一个连续 Y 变量绘制多个 X 变量
问题描述
我正在尝试绘制一个线图,其中包含两个x
变量,x-axis
其中一个连续y
变量y-axis
。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 轴和y 轴的不同颜色和图例为x1
和设置单独的线。你能帮我做这件事吗?提前致谢。x2
df$group
df$val
解决方案
编辑:在下面添加
这是一种假设意图是将组 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 月,正在审查一项旨在解决此问题的建议修复程序。
推荐阅读
- c# - 我想使用 jQuery DataTable 但我收到错误
- ios - VNDocumentCameraViewController 文档检测框的自定义颜色(自动快门)
- java - 如何在函数中返回通用对象?
- c++ - 如何使用 cmake 变量提供自定义库位置(使用 cmake 编译 SYSTRAN/fuzzy-match 项目)?
- node.js - 如何在连接到对话流的 Facebook Bot 上添加持久菜单
- javascript - 无法将 JavaScript 元素附加到 Div
- routes - 本地域都路由到同一个应用程序 - Laravel Homestead
- etl - 实现触发器如果文件共享中删除了任何文件(Windows),则触发我的 ADF 管道。管道 3 和 2 在管道 1 完成后运行
- flutter - Flutter & Firebase:当帐户来自社交认证时,电子邮件和密码重置如何工作?
- jquery - 如何通过方括号括起来的文本输入将新值插入数据属性数组?