列与 ggplot 中的数据不对齐 geom_col
columns are not aligned with the data in ggplot geom_col
将一些数据绘制成带有数值数据的图形,人们会期望列边框与网格对齐。但是,在绘制此数据时,您可以看到有些排列正确 (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")
yaxis 中的 2
是 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))
这是由您的 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")
得到你想要的。
将一些数据绘制成带有数值数据的图形,人们会期望列边框与网格对齐。但是,在绘制此数据时,您可以看到有些排列正确 (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")
yaxis 中的 2
是 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))
这是由您的 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")
得到你想要的。