简体   繁体   English

ggplot,在条形组之间添加网格线

[英]ggplot, add grid lines between bars groups

When using dodged bars in a ggplot with discrete x axis, they are centered around the x ticks.在具有离散 x 轴的 ggplot 中使用躲避条时,它们以 x 刻度为中心。 As a consequence, also the vertical grid lines pass along the tick in the middle of the group of bars.因此,垂直网格线也沿着柱组中间的刻度线穿过。

在此处输入图片说明

I would like instead to have grid lines between the groups of bars.我希望条形组之间有网格线。 This is especially useful in cases like the one shown before, in which bars are sparse and is not immediate which group each bar belongs to.这在前面显示的情况下特别有用,在这种情况下,条形稀疏并且每个条形所属的组不是直接的。

Here's the code (without ordering of the x, which is not related and long to write):这是代码(没有 x 的排序,它不相关并且很长):

library(ggplot)
library(dplyr)

structure(list(Reparto = c("Oncologia medica", "Centro trapianti", 
"Chirurgia epatobiliare", "Dh oncologico", "Radioterapia", "Chirurgia", 
"Chirurgia oncologica", "Gastroenterologia", "Radiologia", "Oncologia medica", 
"Centro trapianti", "Chirurgia epatobiliare", "Dh oncologico", 
"Radioterapia", "Chirurgia", "Chirurgia oncologica", "Gastroenterologia", 
"Radiologia", "Oncologia medica", "Centro trapianti", "Chirurgia epatobiliare", 
"Dh oncologico", "Radioterapia", "Chirurgia", "Chirurgia oncologica", 
"Gastroenterologia", "Radiologia", "Oncologia medica", "Centro trapianti", 
"Chirurgia epatobiliare", "Dh oncologico", "Radioterapia", "Chirurgia", 
"Chirurgia oncologica", "Gastroenterologia", "Radiologia"), Fascia.oraria = c("00:00 - 3:00", 
"00:00 - 3:00", "00:00 - 3:00", "00:00 - 3:00", "00:00 - 3:00", 
"00:00 - 3:00", "00:00 - 3:00", "00:00 - 3:00", "00:00 - 3:00", 
"3:00 - 13:00", "3:00 - 13:00", "3:00 - 13:00", "3:00 - 13:00", 
"3:00 - 13:00", "3:00 - 13:00", "3:00 - 13:00", "3:00 - 13:00", 
"3:00 - 13:00", "13:00 - 24:00", "13:00 - 24:00", "13:00 - 24:00", 
"13:00 - 24:00", "13:00 - 24:00", "13:00 - 24:00", "13:00 - 24:00", 
"13:00 - 24:00", "13:00 - 24:00", "Orario sconosciuto", "Orario sconosciuto", 
"Orario sconosciuto", "Orario sconosciuto", "Orario sconosciuto", 
"Orario sconosciuto", "Orario sconosciuto", "Orario sconosciuto", 
"Orario sconosciuto"), Eventi = c(19.7, 2.19, 0, 0, 0, 0, 0, 
0, 0, 4.6, 1.32, 0.66, 0, 0.66, 0, 0, 0.66, 0, 7.77, 0, 1.2, 
1.2, 0, 0.6, 0.6, 0, 0.6, NA, NA, NA, NA, NA, NA, NA, NA, NA)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -36L)) %>%
ggplot(aes(str_first_up(Reparto), Eventi)) +
        geom_col(aes(fill = Fascia.oraria), position = position_dodge2(preserve = 'total')) +
        theme(
            axis.text.x  = element_text(angle = 45, hjust = 1),
            panel.grid.minor.x = element_line(color = 'gray')
            ) +
        labs(x = NULL, y = 'N. eventi x 100 gg', fill = 'Fascia oraria')

You can manually set the lines like this:您可以像这样手动设置行:

p +
  theme(panel.grid = element_blank()) + # remove grid lines
  geom_vline(xintercept = seq(0.5, length(df$Reparto), by = 1), color="gray", size=.5, alpha=.5) # set vertical lines between x groups

where p is your original plot, and df is your data.frame.其中p是您的原始图,而df是您的 data.frame。

在此处输入图片说明

Obs: To produce this picture, I removed the str_first_up() from your code since it's not from dplyr or ggplot packages (current versions). Obs:为了制作这张图片,我从你的代码中删除了str_first_up() ,因为它不是来自dplyrggplot包(当前版本)。 Also, I added theme_bw() before your theme settings.另外,我在您的主题设置之前添加了theme_bw()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM