gpt4 book ai didi

r - 设置带有端点的 ggplot 网格线/ggplot 的中断计算

转载 作者:行者123 更新时间:2023-12-04 18:56:53 32 4
gpt4 key购买 nike

我有一个 ggplot我试图以非常简约的外观制作线图的问题。我已经摆脱了图例,转而使用每行右侧的文本标签。如果标签不是那么长,它可能不会那么明显,但如果网格线停在最大 x 值(在这种情况下,在 2015 年),我希望它更好。

library(tidyverse)

df <- structure(list(industry = c("Accommodation & Food Services", "Construction", "Health Care & Social Asst.", "Retail Trade",
"Accommodation & Food Services", "Construction", "Health Care & Social Asst.", "Retail Trade"),
year = c(2002L, 2002L, 2002L, 2002L, 2015L, 2015L, 2015L, 2015L),
value = c(6.977, 5.264, 17.065, 14.528, 8.032, 4.648, 20.547, 12.568)),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -8L),
.Names = c("industry", "year", "value"))

ggplot(df, aes(x = year, y = value, color = industry)) +
geom_line() +
geom_text(aes(label = industry), data = . %>% filter(year == max(year)), size = 3, hjust = 0, nudge_x = 0.1) +
scale_x_continuous(expand = expand_scale(mult = c(0.05, 0.85)), breaks = c(min(df$year), max(df$year)), name = NULL) +
scale_y_continuous(limits = c(0, NA), name = "Value (thousands)") +
theme_minimal() +
theme(legend.position = "none",
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank())



我想避免 grob-hacking in this answer因为我有几个情节,想快速把它们放在一起。

到目前为止,我想出的解决方案是通过制作 geom_segment 来创建伪网格线。使用从最早年份开始到最晚年份结束的常规 y 值的虚拟数据。这行得通,因为我知道我想要什么 y-breaks 并且可以设置 scale_y_continuous匹配 geom_segment .

break_df <- tibble(x1 = 2002, x2 = 2015, y = seq(0, 20, by = 5))

ggplot(df, aes(x = year, y = value, color = industry)) +
geom_segment(aes(x = x1, xend = x2, y = y, yend = y), data = break_df, inherit.aes = F, size = 0.4, color = "gray85") +
geom_line() +
geom_text(aes(label = industry), data = . %>% filter(year == max(year)), size = 3, hjust = 0, nudge_x = 0.1) +
scale_x_continuous(expand = expand_scale(mult = c(0.05, 0.85)), breaks = c(min(df$year), max(df$year)), name = NULL) +
scale_y_continuous(limits = c(0, NA), name = "Value (thousands)", breaks = break_df$y) +
theme_minimal() +
theme(legend.position = "none",
panel.grid = element_blank())



创建于 2018-05-24 由 reprex package (v0.2.0)。

我想知道的是 ggplot计算休息时间,如果它使用了一个我可以利用的函数?就像我可以将休息时间输入到 geom_segment 中的一些内部计算一样。和 scale_y_continuous ,因此无需我设置单独的中断信息数据框,它们就可以匹配。

或者,也许这是一种愚蠢的方法,并且有更好的方法来获得我想要的网格线。我在想也可能有一种方法可以使用 trans function ,比如建立一个 trans_new() ,但我不知道该怎么做。

提前致谢!

最佳答案

我想提出一种使用 annotation_custom() 的替代方法,它实现了类似的外观,至少在这种情况下:

ggplot(df, aes(x = year, y = value, color = industry)) +
geom_line() +
annotation_custom(
grob = grid::rectGrob(gp = grid::gpar(col = NA, fill = "white")),
xmin = max(df$year)
) +
geom_text(aes(label = industry), data = . %>% filter(year == max(year)),
size = 3, hjust = 0, nudge_x = 0.1) +

# note: my version of ggplot2 doesn't have the expand_scale function, but I suppose
# it isn't critical to the question here
scale_x_continuous(expand = c(0.2, 0), breaks = c(min(df$year), max(df$year)),
name = NULL) +
scale_y_continuous(limits = c(0, NA), name = "Value (thousands)") +
theme_minimal() +
theme(legend.position = "none",
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank())

plot
annotation_custom的默认位置参数为 Inf/ -Inf , 将显示的 grob 拉伸(stretch)到绘图面板的所有四个边缘,因此指定 xmin = max(df$year)足以让它覆盖 max(df$year) 右侧所有内容的网格线.

这个技巧最适用于没有可见 panel.border 的 ggplot 主题。组件,例如 theme_minimal , theme_grey , 或 theme_dark .如果您使用 theme_bw/ theme_linedraw/ theme_light ,错觉变得不那么有效。 (至于 theme_classic/ theme_void ,它们没有网格线,所以无论哪种方式都没有关系。)

关于r - 设置带有端点的 ggplot 网格线/ggplot 的中断计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50520920/

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