r - 在ggplot中,如何在类别之间沿x和y轴制作网格?

标签 r ggplot2

以下是没有任何情节的可重现 R 脚本 panel.grid

require(ggplot2)
library(ggrepel)
# Create the data frame.
sales_data <- data.frame(
  emp_name = rep(c("Sam", "Dave", "John", "Harry", "Clark", "Kent", "Kenneth", "Richard", "Clement", "Toby", "Jonathan"), times = 5), 
  month = as.factor(rep(c("Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan", "Feb", "Mar", "Jan", "Jan"), times = 5)),
  dept_name = as.factor(rep(c("Production", "Services", "Support", "Support", "Services", "Production", "Production", "Support", "Support", "Support", "Production"), times = 5)), 
  revenue = rep(c(100, 200, 300, 400, 500, 600, 500, 400, 300, 200, 500), times = 5)
)

sales_data$month <- factor(sales_data$month, levels = c("Jan", "Feb", "Mar"))
month_vector <- levels(sales_data$month)
number_of_enteries <- nrow(sales_data)

sales_data$month <- as.integer(sales_data$month)

ggplot(sales_data, aes(x = month, y = dept_name)) +
  geom_raster(data = expand.grid(sales_data$month, sales_data$dept_name), 
            aes(x = Var1, y = Var2, width=1, height=1), fill = NA, col = 'gray50', lty = 1) + #default width and height is 1
  geom_point(aes(size = revenue, col = revenue), 
             shape = 16, position = position_jitter(seed = 0), show.legend = F) +
  geom_text_repel(aes(label = revenue), size=4, vjust = 1.6, position = position_jitter(seed = 0)) + #try with geom_text
  
  theme_bw() +
  theme(
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    plot.background = element_blank(), 
    axis.line = element_blank(), 
    panel.border = element_blank(), 
    panel.grid = element_blank(),
    #panel.grid.major.y = element_line(colour = "red"),
    #panel.grid.major.x = element_line(colour = "red"),
    axis.text = element_text(colour = "blue", face = "plain", size =11)
  ) +
 
  scale_x_continuous(limits=c(0.5,3.5), expand = c(0,0), breaks = 1:length(month_vector), labels = month_vector)
输出图是 :
enter image description here
预期剧情 :
enter image description here
我想沿着 x 和 y 轴有刻度线,如红线所示。我尝试使用 panel.gridtheme但由于我的自定义添加 scale_x_continuous它给了我不需要的主要次要轴。请取消注释行 panel.grid.major.y = element_line(colour = "red"),查看 major网格。

最佳答案

令人惊讶的是,据我所知,似乎没有什么好方法可以获取 panel.grid离散类别之间的线。解决此问题的一种方法是使用 hlinevline添加这些行:

致您的 ggplot代码,添加以下3行:

# Remove extra whitespace from y-axis so lines are against the axis
scale_y_discrete(expand = c(0,0)) +
# Add straight lines at each factor level, shifted left/down so they're between values
geom_hline(yintercept = as.numeric(sales_data$dept_name) - 0.5) +
geom_vline(xintercept = as.numeric(sales_data$month) - 0.5)

Plot with grid lines added

关于r - 在ggplot中,如何在类别之间沿x和y轴制作网格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55306948/

相关文章:

r - 一个新列中跨列子集的字数统计

r - 使用guide_area时,图例与拼凑而成的图重叠

r - ggplot2:设置每个离散x点的绝对距离

r - 带有 ggplot2 的多个直方图 - 位置

debugging - 如何获取R脚本出错时的行号?

r - 达到阈值时 dplyr 重置计数器

r - ggplot2 map 上的多个站点

r - 带注释的 geom_bar 条形内与 x 轴对齐

从 R 中的向量列表中删除特定元素

r - 使用 ggplot 在刻面上创建部分固定、无部分的轴限制?