將一些資料繪制到帶有數字資料的圖形中,人們會期望列邊框與網格對齊。但是,在繪制此資料時,您可以看到有些排列正確 (10, 5),但其他排列不正確 (2, 1)。
這是錯誤還是功能?

可重現的例子
library(tidyverse, scales)
資料
x1 <- c("a", "b", "c", "d", "e")
y1 <- c(1, 10, 2, 1, 5)
xy <- data.frame(x1, y1)
劇情
xy %>%
ggplot(aes(x = fct_reorder(x1, desc(y1)),
y = y1))
geom_col()
geom_text(aes(label = y1), vjust = 1.5, colour = "white") # to show the numbers
一些實驗
正確的
xy %>%
ggplot(aes(x = fct_reorder(x1, desc(y1)),
y = y1))
geom_col()
scale_y_continuous(minor_breaks = seq(0, 10, .5))
# scale_y_continuous(labels = scales::number_format(accuracy = .5))
geom_text(aes(label = y), vjust = 1.5, colour = "white")

但是之后
不正確
xy %>%
ggplot(aes(x = fct_reorder(x1, desc(y1)),
y = y1))
geom_col()
scale_y_continuous(minor_breaks = seq(0, 10, .5))
scale_y_continuous(labels = scales::number_format(accuracy = .5))
geom_text(aes(label = y), vjust = 1.5, colour = "white")

也不正確
xy %>%
ggplot(aes(x = fct_reorder(x1, desc(y1)),
y = y1))
geom_col()
scale_y_continuous(minor_breaks = seq(0, 10, 1),
labels = scales::number_format(accuracy = 1))
geom_text(aes(label = y1), vjust = 1.5, colour = "white")

uj5u.com熱心網友回復:
這2在Y軸的2.5附近1是1.25不是1。
xy %>%
ggplot(aes(x = fct_reorder(x1, desc(y1)),
y = y1))
geom_col()
scale_y_continuous(breaks = seq(0,10,2))
geom_text(aes(label = y1), vjust = 1.5, colour = "white")

xy %>%
ggplot(aes(x = fct_reorder(x1, desc(y1)),
y = y1))
geom_col()

我不知道你為什么添加,accuracy = 1但看看下面的情節。
xy %>%
ggplot(aes(x = fct_reorder(x1, desc(y1)),
y = y1))
geom_col()
scale_y_continuous(breaks = seq(0,10,2))

uj5u.com熱心網友回復:
這是由您的scales_y_continuous呼叫引起的舍入錯誤。用
ggplot(xy,aes(x = fct_reorder(x1, desc(y1)),
y = y1))
geom_col()
scale_y_continuous(breaks=c(0,2,5,8,10))
geom_text(aes(label = y1), vjust = 1.5, colour = "white")
去得到你想要的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315819.html
上一篇:將Geom_pointsggrepel標簽與輔助軸對齊
下一篇:無法在輔助軸上繪制折線圖
