我找不到在点图中按组绘制数据的方法(或 ggplot2 中的任何等效绘图类型)。示例数据如下所示,分组变量为 Turn_no
:
df
# A tibble: 8 × 4
# Groups: Turn_no [4]
c5 Turn_no Word RelPosition
<chr> <int> <chr> <dbl>
1 NN1 9 daddy 0.111
2 NN2 9 apartments 0.778
3 NN1 13 job 1
4 NN1 15 bit 0.167
5 NP0 15 mark 0.667
6 NP0 16 michael 0.222
7 NN1 16 gel 0.778
8 NN1 16 morning 1
我想做的是绘制按 Turn_no
分组的 RelPosition
值,以便每行有与 RelPosition
值一样多的点每个 Turn_no
组。
我尝试将 dplyr
语法与 (base R) dotchart
结合起来的尝试失败了:
libtary(tidyverse)
df %>%
group_by(Turn_no) %>%
dotchart(RelPosition)
Error in dotchart(., RelPosition) :
'x' must be a numeric vector or matrix
我所能做的就是这样,但是不管它属于哪个组,它都会将每个 RelPosition
值放在自己的行上:
dotchart(df$RelPosition)
grouped dot plot 如何在 base R 中或在 ggplot2 中实现(geom_dotplot
似乎不等同于 点图
)?
数据:
df <- structure(list(c5 = c("NN1", "NN2", "NN1", "NN1", "NP0", "NP0",
"NN1", "NN1"), Turn_no = c(9L, 9L, 13L, 15L, 15L, 16L, 16L, 16L
), Word = c("daddy", "apartments", "job", "bit", "mark", "michael",
"gel", "morning"), RelPosition = c(0.111111111111111, 0.777777777777778,
1, 0.166666666666667, 0.666666666666667, 0.222222222222222, 0.777777777777778,
1)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA,
-8L), groups = structure(list(Turn_no = c(9L, 13L, 15L, 16L),
.rows = structure(list(1:2, 3L, 4:5, 6:8), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -4L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE))
最佳答案
是这样的吗?
library(ggplot2)
df <- structure(list(c5 = c(
"NN1", "NN2", "NN1", "NN1", "NP0", "NP0",
"NN1", "NN1"
), Turn_no = c(9L, 9L, 13L, 15L, 15L, 16L, 16L, 16L), Word = c(
"daddy", "apartments", "job", "bit", "mark", "michael",
"gel", "morning"
), RelPosition = c(
0.111111111111111, 0.777777777777778,
1, 0.166666666666667, 0.666666666666667, 0.222222222222222, 0.777777777777778,
1
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(
NA,
-8L
), groups = structure(list(
Turn_no = c(9L, 13L, 15L, 16L),
.rows = structure(list(1:2, 3L, 4:5, 6:8), ptype = integer(0), class = c(
"vctrs_list_of",
"vctrs_vctr", "list"
))
), row.names = c(NA, -4L), class = c(
"tbl_df",
"tbl", "data.frame"
), .drop = TRUE))
df |>
ggplot(aes(y = Turn_no, x = RelPosition, group = Turn_no)) +
geom_line() +
geom_point(size = 4)
由 reprex package 创建于 2023-01-07 (v2.0.1)
关于r - Dotchart(在 base R 中)或 ggplot2 中具有分组数据的等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75040759/