geom_col 中错误闪避列的标签
Labels on wrong dodged columns in geom_col
我想为我的数据框创建一个简单的条形图:
> dput(corr)
structure(list(`sample length` = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("3s", "10s"), class = "factor"),
feature = structure(c(1L, 1L, 5L, 5L, 2L, 5L, 6L, 5L, 5L,
4L, 1L, 1L, 1L, 1L, 1L, 2L, 5L, 5L, 3L, 4L, 1L, 1L, 1L, 1L
), .Label = c("f0", "f1", "f2", "f3", "f2 prime", "f2-f1"
), class = "factor"), measure = c("meanf0 longterm", "meanf0 longterm st",
"f2' Fant", "f2' Carlson", "F1meanERB", "F2meanERB", "f2-f1 ERB",
"f2' Fant", "f2' Carlson", "F3meanERB", "meanf0 3secs", "meanf0 3secs st",
"meanf0 10secs", "meanf0 longterm", "meanf0 longterm st",
"F1meanERB", "f2' Fant", "f2' Carlson", "F2meanERB", "F3meanERB",
"meanf0 longterm", "meanf0 longterm st", "meanf0 3secs",
"meanf0 3s st"), score = c(0.574361009949897, 0.592472685498182,
0.597453479834514, 0.529641256460457, 0.585994252821649,
0.618734735308094, 0.517715270144259, 0.523916918327387,
0.616237363007349, 0.732926257362305, 0.649505366093518,
0.626628120773466, 0.522527636952945, 0.53968850323167, 0.548664887822775,
0.648294358978928, 0.650806695307235, 0.696797693503567,
0.621298393945597, 0.57140950987443, 0.606634531002859, 0.597064217305556,
0.582534743353082, 0.572808145210493), dimension = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L), .Label = c("1", "2", "3",
"4"), class = "factor")), row.names = c(NA, -24L), class = c("tbl_df",
"tbl", "data.frame"))
我试过以下代码:
ggplot(data=corr, aes(x=factor(dimension), y=score)) +
geom_col(aes(fill=feature),position=position_dodge2(width=1,preserve='single')) +
facet_grid(~`sample length`, scales='free_x',space='free_x') +
labs(x="Dimension", y="Correlation Coefficient (Abs. value)") +
geom_text(aes(label=measure),position=position_dodge2(width=0.9, preserve='single'), angle=90,
size=4,hjust=2.5,color='white')
给出以下条形图:
但是,'measure' 的标签被错误地分配给了列。例如。对于 3s 面图,在 'dimension 2' 下,两个浅蓝色条应标记为 'f2' Carlson' 和 'f2' Fant' 但它们已与其他两个标签交换。
我觉得关卡一定是错了,但我不明白怎么回事!
非常感谢任何帮助,ta
切换标签的问题来自geom_text()
不知道为了躲避的目的应该如何分割信息。解决方案是为 geom_text()
提供一个 group=
美学,该美学与为 geom_col()
指定的 fill=
美学相匹配。
在 geom_col()
的情况下,您指定 aes(fill=feature)
。因此,不同列的高度自动按 corr$feature
分组。您也可以提供 group=
美学,但这是不必要的,闪避会如您所愿地发生。
在geom_text()
的情况下,没有明显的数据分组方法。当您未指定 group=
美学时,ggplot2
选择其中一列(在本例中为第一列编号)进行分组。为了躲避在这里工作,您需要指定标签信息的分组方式。如果您在这里没有特定的 legend-associated 美学可供选择,您可以使用 group=
美学来指定 group=feature
。这让我们ggplot2
知道文本标签应该根据数据中的这一列进行分组排序和躲避:
ggplot(data=corr, aes(x=factor(dimension), y=score)) +
geom_col(aes(fill=feature),position=position_dodge2(width=1,preserve='single')) +
facet_grid(~`sample length`, scales='free_x',space='free_x') +
labs(x="Dimension", y="Correlation Coefficient (Abs. value)") +
geom_text(aes(label=measure, group=feature),position=position_dodge2(width=0.9, preserve='single'), angle=90,
size=4,hjust=2.5,color='white')
附带说明一下,如果您指定 color-based 美学(或会产生图例的美学),则不必指定 group=
美学。如果我们将 color=feature
设置为 geom_text()
,它可以在没有 group=
的情况下工作。要查看标签,您需要将列的 alpha 设置得低一点,但这应该很好地说明了这一点:
ggplot(data=corr, aes(x=factor(dimension), y=score)) +
geom_col(aes(fill=feature),position=position_dodge2(width=1,preserve='single'), alpha=0.2) +
facet_grid(~`sample length`, scales='free_x',space='free_x') +
labs(x="Dimension", y="Correlation Coefficient (Abs. value)") +
geom_text(aes(label=measure, color=feature),position=position_dodge2(width=0.9, preserve='single'), angle=90,
size=4,hjust=2.5)
我想为我的数据框创建一个简单的条形图:
> dput(corr)
structure(list(`sample length` = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("3s", "10s"), class = "factor"),
feature = structure(c(1L, 1L, 5L, 5L, 2L, 5L, 6L, 5L, 5L,
4L, 1L, 1L, 1L, 1L, 1L, 2L, 5L, 5L, 3L, 4L, 1L, 1L, 1L, 1L
), .Label = c("f0", "f1", "f2", "f3", "f2 prime", "f2-f1"
), class = "factor"), measure = c("meanf0 longterm", "meanf0 longterm st",
"f2' Fant", "f2' Carlson", "F1meanERB", "F2meanERB", "f2-f1 ERB",
"f2' Fant", "f2' Carlson", "F3meanERB", "meanf0 3secs", "meanf0 3secs st",
"meanf0 10secs", "meanf0 longterm", "meanf0 longterm st",
"F1meanERB", "f2' Fant", "f2' Carlson", "F2meanERB", "F3meanERB",
"meanf0 longterm", "meanf0 longterm st", "meanf0 3secs",
"meanf0 3s st"), score = c(0.574361009949897, 0.592472685498182,
0.597453479834514, 0.529641256460457, 0.585994252821649,
0.618734735308094, 0.517715270144259, 0.523916918327387,
0.616237363007349, 0.732926257362305, 0.649505366093518,
0.626628120773466, 0.522527636952945, 0.53968850323167, 0.548664887822775,
0.648294358978928, 0.650806695307235, 0.696797693503567,
0.621298393945597, 0.57140950987443, 0.606634531002859, 0.597064217305556,
0.582534743353082, 0.572808145210493), dimension = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L), .Label = c("1", "2", "3",
"4"), class = "factor")), row.names = c(NA, -24L), class = c("tbl_df",
"tbl", "data.frame"))
我试过以下代码:
ggplot(data=corr, aes(x=factor(dimension), y=score)) +
geom_col(aes(fill=feature),position=position_dodge2(width=1,preserve='single')) +
facet_grid(~`sample length`, scales='free_x',space='free_x') +
labs(x="Dimension", y="Correlation Coefficient (Abs. value)") +
geom_text(aes(label=measure),position=position_dodge2(width=0.9, preserve='single'), angle=90,
size=4,hjust=2.5,color='white')
给出以下条形图:
但是,'measure' 的标签被错误地分配给了列。例如。对于 3s 面图,在 'dimension 2' 下,两个浅蓝色条应标记为 'f2' Carlson' 和 'f2' Fant' 但它们已与其他两个标签交换。
我觉得关卡一定是错了,但我不明白怎么回事!
非常感谢任何帮助,ta
切换标签的问题来自geom_text()
不知道为了躲避的目的应该如何分割信息。解决方案是为 geom_text()
提供一个 group=
美学,该美学与为 geom_col()
指定的 fill=
美学相匹配。
在 geom_col()
的情况下,您指定 aes(fill=feature)
。因此,不同列的高度自动按 corr$feature
分组。您也可以提供 group=
美学,但这是不必要的,闪避会如您所愿地发生。
在geom_text()
的情况下,没有明显的数据分组方法。当您未指定 group=
美学时,ggplot2
选择其中一列(在本例中为第一列编号)进行分组。为了躲避在这里工作,您需要指定标签信息的分组方式。如果您在这里没有特定的 legend-associated 美学可供选择,您可以使用 group=
美学来指定 group=feature
。这让我们ggplot2
知道文本标签应该根据数据中的这一列进行分组排序和躲避:
ggplot(data=corr, aes(x=factor(dimension), y=score)) +
geom_col(aes(fill=feature),position=position_dodge2(width=1,preserve='single')) +
facet_grid(~`sample length`, scales='free_x',space='free_x') +
labs(x="Dimension", y="Correlation Coefficient (Abs. value)") +
geom_text(aes(label=measure, group=feature),position=position_dodge2(width=0.9, preserve='single'), angle=90,
size=4,hjust=2.5,color='white')
附带说明一下,如果您指定 color-based 美学(或会产生图例的美学),则不必指定 group=
美学。如果我们将 color=feature
设置为 geom_text()
,它可以在没有 group=
的情况下工作。要查看标签,您需要将列的 alpha 设置得低一点,但这应该很好地说明了这一点:
ggplot(data=corr, aes(x=factor(dimension), y=score)) +
geom_col(aes(fill=feature),position=position_dodge2(width=1,preserve='single'), alpha=0.2) +
facet_grid(~`sample length`, scales='free_x',space='free_x') +
labs(x="Dimension", y="Correlation Coefficient (Abs. value)") +
geom_text(aes(label=measure, color=feature),position=position_dodge2(width=0.9, preserve='single'), angle=90,
size=4,hjust=2.5)