尝试用 abline 画一条线?
Try to draw a line with abline?
ggplot(G, aes(x = State, y = Score, fill = State)) +
geom_bar(stat = "identity", position = "identity", width = 0.5) +
scale_y_continuous(labels = scales::comma) +
coord_flip()
这是我正在使用的代码,我正在尝试在得分为 236 处添加一行,所以如何操作以及如何改进图表以及任何编辑或建议总是受欢迎的。
您可以使用 geom_vline()
来执行此操作。因为你有这么多条,你会想在你的 geom_bar()
之后应用 vline,这样它就会出现在你的条的顶部(而不是在你几乎看不到它的下方)。
ggplot(G, aes(x = State, y = Score, fill = State)) +
geom_bar(stat = "identity", position = "identity", width = 0.5) +
geom_hline(yintercept=236, color="#000000", linetype="solid") +
scale_y_continuous(labels = scales::comma) +
coord_flip()
我所做的只是将第三行添加到上面的示例中。 ggplot2
总是让我混淆水平和垂直,尤其是当你做像 coord_flip()
这样的事情时。我想我已经弄对了(尽管它看起来不对是因为翻转),但如果我错了并且线是水平的,请将上面的第三行替换为:
geom_vline(xintercept=236, color="#000000", linetype="solid") +
并注意仅有的两个变化,即 vline 变为 hline,xintercept 变为 yintercept。
只需使用:
geom_hline(yintercept = 236)
可能值得重新排序您的 y 轴并使用 fill = Score
。它会使情节看起来像这样:
df %>%
ggplot(aes(reorder(State, -Assault), Assault, fill = Assault)) +
geom_col(width = 0.75, aes(fill = Assault)) +
labs(x = "State") +
geom_hline(yintercept = 200, size = 1) +
coord_flip() +
theme_classic()
ggplot(G, aes(x = State, y = Score, fill = State)) +
geom_bar(stat = "identity", position = "identity", width = 0.5) +
scale_y_continuous(labels = scales::comma) +
coord_flip()
这是我正在使用的代码,我正在尝试在得分为 236 处添加一行,所以如何操作以及如何改进图表以及任何编辑或建议总是受欢迎的。
您可以使用 geom_vline()
来执行此操作。因为你有这么多条,你会想在你的 geom_bar()
之后应用 vline,这样它就会出现在你的条的顶部(而不是在你几乎看不到它的下方)。
ggplot(G, aes(x = State, y = Score, fill = State)) +
geom_bar(stat = "identity", position = "identity", width = 0.5) +
geom_hline(yintercept=236, color="#000000", linetype="solid") +
scale_y_continuous(labels = scales::comma) +
coord_flip()
我所做的只是将第三行添加到上面的示例中。 ggplot2
总是让我混淆水平和垂直,尤其是当你做像 coord_flip()
这样的事情时。我想我已经弄对了(尽管它看起来不对是因为翻转),但如果我错了并且线是水平的,请将上面的第三行替换为:
geom_vline(xintercept=236, color="#000000", linetype="solid") +
并注意仅有的两个变化,即 vline 变为 hline,xintercept 变为 yintercept。
只需使用:
geom_hline(yintercept = 236)
可能值得重新排序您的 y 轴并使用 fill = Score
。它会使情节看起来像这样:
df %>%
ggplot(aes(reorder(State, -Assault), Assault, fill = Assault)) +
geom_col(width = 0.75, aes(fill = Assault)) +
labs(x = "State") +
geom_hline(yintercept = 200, size = 1) +
coord_flip() +
theme_classic()