如何在 R 中使用 twoord.plot () 绘制多个图形(分面)?
How to plot multiple graphs (faceting) using twoord.plot () in R?
我有这样的数据:
height <- c(1,2,3,4,2,4,6,8)
weight <- c(12,13,14,15,22,23,24,25)
person <- c("Jack","Jim","Jill","Tess","Jack","Jim","Jill","Tess")
set <- c(1,1,1,1,2,2,2,2)
dat <- data.frame(set,person,height,weight)
我正在尝试使用 plotrix
包中的 twoord.plot
函数绘制具有相同 x 轴(人)和 2 个不同 y 轴(体重和身高)的图表。
但是,我不知道如何像 ggplot2 那样对图进行分面处理。
例如,如果我的绘图可以叠加在 ggplot2 中,我的代码将如下所示:
ggplot(data = dat, aes(x = person, y = weight)) +
geom_point(color = "red") + facet_wrap(~set, scales="free")
#And similarly have the height on the other axis.
知道如何在 twoord.plot() 中实现这一点吗?
根据评论,似乎真正的问题是如何使用 ggplot2
实现适当的情节,而不是如何强制 plotrix
做它不能做的事情。
不鼓励在 ggplot2
中使用辅助轴,因为它们具有误导性。眼睛倾向于比较不可比较的值,尤其是在线条交叉的地方。因此,sec_axis
仅限于一个变量是另一个变量的直接转换的情况。
我认为最好使用高度和重量作为刻面,并按组着色。这将清楚地显示每个变量、每个人、每个集合中的变化(我假设是时间点之类的东西)。关键是要考虑您要突出显示数据集的哪些方面并相应地创建图表。
几个例子。首先,facet by height/weight,color by set:
library(tidyr)
library(ggplot2)
dat %>%
gather(var, value, -(1:2)) %>%
ggplot(aes(person, value)) +
geom_point(aes(color = factor(set))) +
facet_wrap(~var, scales = "free_y")
或按人物颜色、绘图集与每个变量。在这种情况下,我很想使用线条:
dat %>%
gather(var, value, -(1:2)) %>%
ggplot(aes(factor(set), value)) +
geom_line(aes(color = person, group = person)) +
facet_wrap(~var, scales = "free_y")
我觉得这个情节真的很混乱,但这似乎或多或少是 plotrix::twoord.plot
所做的,所以如果这是你的想法,请告诉我。
在 ggplot2 中,第二个轴必须基于第一个轴的变换。因此,我们首先转换绘制的高度值,将它们置于与体重值相同的范围内(因此它们将出现在基于体重的图表的 y 范围内)。然后我们对第二个y轴的刻度进行逆变换(这样y轴刻度就会对应数据中实际的高度值)
ggplot(dat, aes(person)) +
geom_point(aes(y=weight), colour="blue") +
geom_point(aes(y=height/mean(height/weight)), colour="red", shape=17) +
scale_y_continuous(sec.axis=sec_axis(~ . * mean(dat$height/dat$weight),
breaks=seq(0,max(dat$height),1),
name="height")) +
theme_classic() +
theme(axis.text.y.right=element_text(colour="red"),
axis.text.y=element_text(colour="blue"),
axis.title.y.right=element_text(colour="red"),
axis.title.y=element_text(colour="blue"))
这对我来说似乎更直观,尤其是如果每个人的测量结果(隐含地)按时间排序:
ggplot(dat, aes(weight, height, label=person, group=person)) +
geom_line(colour="grey70") +
geom_text()
我有这样的数据:
height <- c(1,2,3,4,2,4,6,8)
weight <- c(12,13,14,15,22,23,24,25)
person <- c("Jack","Jim","Jill","Tess","Jack","Jim","Jill","Tess")
set <- c(1,1,1,1,2,2,2,2)
dat <- data.frame(set,person,height,weight)
我正在尝试使用 plotrix
包中的 twoord.plot
函数绘制具有相同 x 轴(人)和 2 个不同 y 轴(体重和身高)的图表。
但是,我不知道如何像 ggplot2 那样对图进行分面处理。
例如,如果我的绘图可以叠加在 ggplot2 中,我的代码将如下所示:
ggplot(data = dat, aes(x = person, y = weight)) +
geom_point(color = "red") + facet_wrap(~set, scales="free")
#And similarly have the height on the other axis.
知道如何在 twoord.plot() 中实现这一点吗?
根据评论,似乎真正的问题是如何使用 ggplot2
实现适当的情节,而不是如何强制 plotrix
做它不能做的事情。
不鼓励在 ggplot2
中使用辅助轴,因为它们具有误导性。眼睛倾向于比较不可比较的值,尤其是在线条交叉的地方。因此,sec_axis
仅限于一个变量是另一个变量的直接转换的情况。
我认为最好使用高度和重量作为刻面,并按组着色。这将清楚地显示每个变量、每个人、每个集合中的变化(我假设是时间点之类的东西)。关键是要考虑您要突出显示数据集的哪些方面并相应地创建图表。
几个例子。首先,facet by height/weight,color by set:
library(tidyr)
library(ggplot2)
dat %>%
gather(var, value, -(1:2)) %>%
ggplot(aes(person, value)) +
geom_point(aes(color = factor(set))) +
facet_wrap(~var, scales = "free_y")
或按人物颜色、绘图集与每个变量。在这种情况下,我很想使用线条:
dat %>%
gather(var, value, -(1:2)) %>%
ggplot(aes(factor(set), value)) +
geom_line(aes(color = person, group = person)) +
facet_wrap(~var, scales = "free_y")
我觉得这个情节真的很混乱,但这似乎或多或少是 plotrix::twoord.plot
所做的,所以如果这是你的想法,请告诉我。
在 ggplot2 中,第二个轴必须基于第一个轴的变换。因此,我们首先转换绘制的高度值,将它们置于与体重值相同的范围内(因此它们将出现在基于体重的图表的 y 范围内)。然后我们对第二个y轴的刻度进行逆变换(这样y轴刻度就会对应数据中实际的高度值)
ggplot(dat, aes(person)) +
geom_point(aes(y=weight), colour="blue") +
geom_point(aes(y=height/mean(height/weight)), colour="red", shape=17) +
scale_y_continuous(sec.axis=sec_axis(~ . * mean(dat$height/dat$weight),
breaks=seq(0,max(dat$height),1),
name="height")) +
theme_classic() +
theme(axis.text.y.right=element_text(colour="red"),
axis.text.y=element_text(colour="blue"),
axis.title.y.right=element_text(colour="red"),
axis.title.y=element_text(colour="blue"))
这对我来说似乎更直观,尤其是如果每个人的测量结果(隐含地)按时间排序:
ggplot(dat, aes(weight, height, label=person, group=person)) +
geom_line(colour="grey70") +
geom_text()