gpt4 book ai didi

r - 根据轴刻度位置显示的数据为 ggplot2 轴刻度标签着色

转载 作者:行者123 更新时间:2023-12-02 05:23:57 25 4
gpt4 key购买 nike

我有这个数据集:

top_bot_5_both <- structure(list(
name = structure(c(20L, 19L, 18L, 17L, 16L, 15L, 14L, 13L, 12L,
11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L),
.Label = c("Michele Bachmann", "Donald Trump", "Ted Cruz",
"Newt Gingrich", "Rick Santorum", "Terry McAuliffe",
"Nancy Pelosi", "Debbie Wasserman Schultz",
"Tammy Baldwin", "Joe Biden", "Rand Paul", "Jeb Bush",
"John Kasich", "Barack Obama", "Bill Clinton",
"Hillary Clinton", "Nathan Deal", "Tim Kaine",
"Rob Portman", "Sherrod Brown"),
class = "factor"),
Party = c("Democratic", "Republican", "Democratic", "Republican", "Democratic",
"Democratic", "Democratic", "Republican", "Republican", "Republican",
"Republican", "Republican", "Republican", "Republican", "Republican",
"Democratic", "Democratic", "Democratic", "Democratic", "Democratic"),
total_ratings = c(35L, 48L, 51L, 49L, 296L, 41L, 599L, 64L, 80L, 55L,
61L, 472L, 123L, 82L, 61L, 31L, 35L, 48L, 33L, 75L),
sum = c(22, 29, 21, 18, 96, 12, 172, 16, 18, 2, -86, -525, -94, -57,
-42, -19, -14, -7, -4, -1),
score = c(0.628571428571429, 0.604166666666667, 0.411764705882353,
0.36734693877551, 0.324324324324324, 0.292682926829268,
0.287145242070117, 0.25, 0.225, 0.0363636363636364, -1.40983606557377,
-1.11228813559322, -0.764227642276423, -0.695121951219512,
-0.688524590163934, -0.612903225806452, -0.4, -0.145833333333333,
-0.121212121212121, -0.0133333333333333)),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -20L),
.Names = c("name", "Party", "total_ratings", "sum", "score"))

我想制作一个柱形图,其 y 轴文本镜像图表本身的填充颜色。遵循this thread的建议,我将 color 列添加到我的 tibble

top_bot_5_both <- top_bot_5_both %>%
mutate(color = ifelse(Party == "Democratic", "#1A80C4", "#CC3D3D"))

然后将其绘制成这样:

ggplot(top_bot_5_both, aes(x = name, y = score, fill = Party)) +
geom_col(size = 1, color = "black") +
coord_flip() +
geom_vline(xintercept = 10.5, size = 1.5, linetype = "twodash") +
scale_fill_manual(values = c("Democratic" = "#1A80C4", "Republican" = "#CC3D3D")) +
theme(axis.text = element_text(size = 12),
axis.text.y = element_text(color = top_bot_5_both$color),
axis.title = element_text(size = 14),
legend.position = "bottom",
legend.title = element_blank())

enter image description here

在图中,fill 正确识别了每个政治家的政党,但 axis.text.y 却没有。例如,米歇尔·巴赫曼 (Michelle Bachmann) 是一名共和党人,因此她的专栏应该采用红色填充(确实如此)并且应该具有红色轴文本(实际上没有)。

然后我决定使用创建此图表的 axis.text.y = element_text(color = rev(top_bot_5_both$color) 将其包装在 rev() 中,这是更接近正确的。

ggplot(top_bot_5_both, aes(x = name, y = score, fill = Party)) +
geom_col(size = 1, color = "black") +
coord_flip() +
geom_vline(xintercept = 10.5, size = 1.5, linetype = "twodash") +
scale_fill_manual(values = c("Democratic" = "#1A80C4", "Republican" = "#CC3D3D")) +
theme(axis.text = element_text(size = 12),
axis.text.y = element_text(color = rev(top_bot_5_both$color)),
axis.title = element_text(size = 14),
legend.position = "bottom",
legend.title = element_blank())

enter image description here

top_bot_5_both$color 向量在小标题中的顺序是正确的,但在实际绘图过程中不知何故会变得困惑。也许这与 top_bot_5_both$name 是一个因素有关,但我认为为了按降序绘制分数,这是必要的。

我尝试过但不起作用的其他事情:

  • 摆脱 coord_flip() 并将 y 更改为 x 等。
  • 不生成列 top_bot_5_both$color ,而是将 ifelse(...) 命令放入 element_text(color = ...) >
  • 而且我无法手动执行此操作,因为我计划将来重复此分析,并且政治家在 y 轴上的顺序可能会发生变化

我做错了什么还是这里有错误?

最佳答案

重要的是要意识到,当您编写axis.text.y = element_text(color = ...)时,您不是使用常规 ggplot2 映射机制将数据映射到颜色,而是手动将颜色分配给特定的轴标签。因此,您分配的颜色必须完全按照正确的顺序。

当您检查数据框时,您可以看到行并不按照绘制的顺序排列;顺序由 name 的级别设置因素。因此,您也必须对颜色使用该顺序。您可以使用order()来做到这一点功能:

colors <- top_bot_5_both$color[order(top_bot_5_both$name)]

现在,使用这个颜色向量,我们可以按预期绘制绘图:

ggplot(top_bot_5_both, aes(x = name, y = score, fill = Party)) +
geom_col(size = 1, color = "black") +
coord_flip() +
geom_vline(xintercept = 10.5, size = 1.5, linetype = "twodash") +
scale_fill_manual(values = c("Democratic" = "#1A80C4", "Republican" = "#CC3D3D")) +
theme(axis.text = element_text(size = 12),
axis.text.y = element_text(color = colors),
axis.title = element_text(size = 14),
legend.position = "bottom",
legend.title = element_blank())

enter image description here

关于r - 根据轴刻度位置显示的数据为 ggplot2 轴刻度标签着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47934253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com