在一个图中并排放置来自 lessR 的两个饼图
Placing two Pie Charts from lessR side by side in one figure
lessR 包可以做一些非常好的事情,它使圆环图比任何其他包都更简单。如何将两个饼图并排放置在一个图中?我知道它可以为多面板做一个格子,但在这里的例子中这似乎不是一个选项。
d <- data.frame(
gender = c("M","M","M","M","M","F","M","M","M","M","M","M","F","M","M","M","F","M","M","M"),
ethnic_grp = c("WHITE","ASIAN","ASIAN","MULTIETH","MULTIETH","BLACK","NSPEC","ASIAN","ASIAN", "WHITE", "HISPA", "NSPEC","MULTIETH","ASIAN","ASIAN","ASIAN","HISPA","ASIAN","BLACK","MULTIETH")
)
#chart 1
PieChart(ethnic_grp, fill = "viridis",
main = NULL, quiet=TRUE)
#chart 2
PieChart(gender, fill = "heat",
main = NULL, quiet=TRUE)
与其按顺序生成它们,我更喜欢这样的东西:
我已经使用 ggplot2 重新创建了这个,但是以这种方式创建这些图需要更多的工作。
您可以使用 PieChart 中的 add
参数轻松做到这一点。非常简单。
首先,您需要使用 par
函数来定义网格,即根据您拥有的地块数量定义所需的行数和列数。
在你的情况下,你需要 par(mfrow = c(1, 2))
这将绘制两个相邻的地块。
整个代码如下所示:
library(lessR)
d <- data.frame(
gender = c("M","M","M","M","M","F","M","M","M","M","M","M","F","M","M","M","F","M","M","M"),
ethnic_grp = c("WHITE","ASIAN","ASIAN","MULTIETH","MULTIETH","BLACK","NSPEC","ASIAN","ASIAN", "WHITE", "HISPA", "NSPEC","MULTIETH","ASIAN","ASIAN","ASIAN","HISPA","ASIAN","BLACK","MULTIETH")
)
par(mfrow = c(1, 2))
#chart 1
PieChart(ethnic_grp, fill = "viridis",
main = NULL, quiet=TRUE, add = PieChart(gender, fill = "heat",
main = NULL, quiet=TRUE))
请注意,您需要在第一个图表的 add
参数内添加其他图表,而不是分别绘制两个图表。
输出如下所示:
lessR 包可以做一些非常好的事情,它使圆环图比任何其他包都更简单。如何将两个饼图并排放置在一个图中?我知道它可以为多面板做一个格子,但在这里的例子中这似乎不是一个选项。
d <- data.frame(
gender = c("M","M","M","M","M","F","M","M","M","M","M","M","F","M","M","M","F","M","M","M"),
ethnic_grp = c("WHITE","ASIAN","ASIAN","MULTIETH","MULTIETH","BLACK","NSPEC","ASIAN","ASIAN", "WHITE", "HISPA", "NSPEC","MULTIETH","ASIAN","ASIAN","ASIAN","HISPA","ASIAN","BLACK","MULTIETH")
)
#chart 1
PieChart(ethnic_grp, fill = "viridis",
main = NULL, quiet=TRUE)
#chart 2
PieChart(gender, fill = "heat",
main = NULL, quiet=TRUE)
与其按顺序生成它们,我更喜欢这样的东西:
我已经使用 ggplot2 重新创建了这个,但是以这种方式创建这些图需要更多的工作。
您可以使用 PieChart 中的 add
参数轻松做到这一点。非常简单。
首先,您需要使用 par
函数来定义网格,即根据您拥有的地块数量定义所需的行数和列数。
在你的情况下,你需要 par(mfrow = c(1, 2))
这将绘制两个相邻的地块。
整个代码如下所示:
library(lessR)
d <- data.frame(
gender = c("M","M","M","M","M","F","M","M","M","M","M","M","F","M","M","M","F","M","M","M"),
ethnic_grp = c("WHITE","ASIAN","ASIAN","MULTIETH","MULTIETH","BLACK","NSPEC","ASIAN","ASIAN", "WHITE", "HISPA", "NSPEC","MULTIETH","ASIAN","ASIAN","ASIAN","HISPA","ASIAN","BLACK","MULTIETH")
)
par(mfrow = c(1, 2))
#chart 1
PieChart(ethnic_grp, fill = "viridis",
main = NULL, quiet=TRUE, add = PieChart(gender, fill = "heat",
main = NULL, quiet=TRUE))
请注意,您需要在第一个图表的 add
参数内添加其他图表,而不是分别绘制两个图表。
输出如下所示: