R 中不从 0 开始的条形图
Bar graphs that don't start at 0 in R
我正在尝试显示机器的可用行程范围,该机器具有 360 度旋转并且具有另一个运动轴,范围为 -5 到 152,与偏航无关。我能找到的所有条形图绘制函数都假设数据从 0 开始,这在 -5 和 0 之间的图形中间留下了一个洞。是否可以告诉 geom_bar() 或 geom_col() 从 -5 而不是 0 开始绘图?
这是我正在使用的代码和示例图。
df <- data.frame(0:360)
colnames(df) = "Yaw"
df$Max.Static <- ((runif(361) * 157)-5)
library(ggplot2)
ggplot(df, aes(x =Yaw , y = Max.Static)) +
geom_col(width = 1, alpha = .5 , fill = "#69B600") +
scale_y_continuous(
limits = c(-5,152),
breaks = seq(0,140,20)
) +
scale_x_continuous(
limits = c(-1,361),
breaks = seq(0,360,45),
minor_breaks = seq(0,360,15)
) +
coord_polar(theta = "x") +
labs(x = NULL, y = NULL) +
theme(axis.text.y = element_blank(),
axis.ticks = element_blank())
有点奇怪的 hack,但如果您可以容忍警告,则以下内容有效:
df <- data.frame(0:360)
colnames(df) = "Yaw"
df$Max.Static <- ((runif(361) * 157)-5)
library(ggplot2)
ggplot(df, aes(x =Yaw , y = Max.Static)) +
geom_col(width = 1, alpha = .5 , fill = "#69B600",
aes(ymin = after_scale(-Inf))) + ######## <- Change this line
scale_y_continuous(
limits = c(-5,152),
breaks = seq(0,140,20)
) +
scale_x_continuous(
limits = c(-1,361),
breaks = seq(0,360,45),
minor_breaks = seq(0,360,15)
) +
coord_polar(theta = "x") +
labs(x = NULL, y = NULL) +
theme(axis.text.y = element_blank(),
axis.ticks = element_blank())
#> Warning: Ignoring unknown aesthetics: ymin
由 reprex package (v0.3.0)
于 2021 年 1 月 20 日创建
之所以有效,是因为在使用 after_scale()
.
将条形参数化转换为矩形后,我们可以访问矩形参数(xmin、xmax、ymin、ymax)
-Inf
指示 ggplot 变量应处于比例的最小值(即使在扩展后),从而缩小差距。
从技术上讲,您应该查看 geom_segment 或 geom_rect,而不是 geom_bar,因为从概念上讲,条形图表示计数,因此始终从 0
开始
这里有geom_segment。无需破解。
df <- data.frame(0:360)
colnames(df) = "Yaw"
df$Max.Static <- ((runif(361) * 157)-5)
df$y <- -5
library(ggplot2)
ggplot(df, aes(Yaw , y, xend = Yaw, yend = Max.Static)) +
geom_segment( color = "#69B600", size = .2) +
scale_y_continuous(
limits = c(-5,152),
breaks = seq(0,140,20)
) +
scale_x_continuous(
limits = c(-1,361),
breaks = seq(0,360,45),
minor_breaks = seq(0,360,15)
) +
coord_polar(theta = "x") +
labs(x = NULL, y = NULL) +
theme(axis.text.y = element_blank(),
axis.ticks = element_blank())
或者,使用 geom_rect,获得风车样式
ggplot(df, aes(xmin = Yaw-0.4 , ymin = y, xmax = Yaw +0.4, ymax = Max.Static)) +
geom_rect( fill = "#69B600") +
scale_y_continuous(
limits = c(-5,152),
breaks = seq(0,140,20)
) +
scale_x_continuous(
limits = c(-1,361),
breaks = seq(0,360,45),
minor_breaks = seq(0,360,15)
) +
coord_polar(theta = "x") +
labs(x = NULL, y = NULL) +
theme(axis.text.y = element_blank(),
axis.ticks = element_blank())
我正在尝试显示机器的可用行程范围,该机器具有 360 度旋转并且具有另一个运动轴,范围为 -5 到 152,与偏航无关。我能找到的所有条形图绘制函数都假设数据从 0 开始,这在 -5 和 0 之间的图形中间留下了一个洞。是否可以告诉 geom_bar() 或 geom_col() 从 -5 而不是 0 开始绘图?
这是我正在使用的代码和示例图。
df <- data.frame(0:360)
colnames(df) = "Yaw"
df$Max.Static <- ((runif(361) * 157)-5)
library(ggplot2)
ggplot(df, aes(x =Yaw , y = Max.Static)) +
geom_col(width = 1, alpha = .5 , fill = "#69B600") +
scale_y_continuous(
limits = c(-5,152),
breaks = seq(0,140,20)
) +
scale_x_continuous(
limits = c(-1,361),
breaks = seq(0,360,45),
minor_breaks = seq(0,360,15)
) +
coord_polar(theta = "x") +
labs(x = NULL, y = NULL) +
theme(axis.text.y = element_blank(),
axis.ticks = element_blank())
有点奇怪的 hack,但如果您可以容忍警告,则以下内容有效:
df <- data.frame(0:360)
colnames(df) = "Yaw"
df$Max.Static <- ((runif(361) * 157)-5)
library(ggplot2)
ggplot(df, aes(x =Yaw , y = Max.Static)) +
geom_col(width = 1, alpha = .5 , fill = "#69B600",
aes(ymin = after_scale(-Inf))) + ######## <- Change this line
scale_y_continuous(
limits = c(-5,152),
breaks = seq(0,140,20)
) +
scale_x_continuous(
limits = c(-1,361),
breaks = seq(0,360,45),
minor_breaks = seq(0,360,15)
) +
coord_polar(theta = "x") +
labs(x = NULL, y = NULL) +
theme(axis.text.y = element_blank(),
axis.ticks = element_blank())
#> Warning: Ignoring unknown aesthetics: ymin
由 reprex package (v0.3.0)
于 2021 年 1 月 20 日创建之所以有效,是因为在使用 after_scale()
.
-Inf
指示 ggplot 变量应处于比例的最小值(即使在扩展后),从而缩小差距。
从技术上讲,您应该查看 geom_segment 或 geom_rect,而不是 geom_bar,因为从概念上讲,条形图表示计数,因此始终从 0
开始这里有geom_segment。无需破解。
df <- data.frame(0:360)
colnames(df) = "Yaw"
df$Max.Static <- ((runif(361) * 157)-5)
df$y <- -5
library(ggplot2)
ggplot(df, aes(Yaw , y, xend = Yaw, yend = Max.Static)) +
geom_segment( color = "#69B600", size = .2) +
scale_y_continuous(
limits = c(-5,152),
breaks = seq(0,140,20)
) +
scale_x_continuous(
limits = c(-1,361),
breaks = seq(0,360,45),
minor_breaks = seq(0,360,15)
) +
coord_polar(theta = "x") +
labs(x = NULL, y = NULL) +
theme(axis.text.y = element_blank(),
axis.ticks = element_blank())
或者,使用 geom_rect,获得风车样式
ggplot(df, aes(xmin = Yaw-0.4 , ymin = y, xmax = Yaw +0.4, ymax = Max.Static)) +
geom_rect( fill = "#69B600") +
scale_y_continuous(
limits = c(-5,152),
breaks = seq(0,140,20)
) +
scale_x_continuous(
limits = c(-1,361),
breaks = seq(0,360,45),
minor_breaks = seq(0,360,15)
) +
coord_polar(theta = "x") +
labs(x = NULL, y = NULL) +
theme(axis.text.y = element_blank(),
axis.ticks = element_blank())