简体   繁体   English

ggplot2:如何在geom_bar中的条旁边而不是条中间重新放置断线?

[英]ggplot2: How to reposition break lines next to bars in geom_bar instead of at the middle of the bar?

I have the following figure. 我有下图。

情节

As you can see the breaklines for the x scale (which is scaled discrete in this case) go right through the different bars. 如您所见,x比例尺的折线(在这种情况下为离散比例)正好穿过不同的条。 I want to these breaklines to form "lanes" in which the bars are nicely fitted. 我希望这些断裂线形成“车道”,其中的杆非常适合。 So instead of having my breaks go through the bar, I would like them to lie at the edge of each bar. 因此,我不想让我的休息时间穿过酒吧,而是希望他们躺在每个酒吧的边缘。

I experimented a lot with the scale_x_discrete function but just can't seem to figure out how to achieve this... 我对scale_x_discrete函数进行了很多实验,但似乎无法弄清楚该如何实现...

You can create some fake gridlines using geom_vline while hiding the true gridlines. 您可以使用geom_vline创建一些假的网格线,同时隐藏真实的网格线。

library(tidyverse)

data <- data.frame(x = letters[1:10], y = runif(10), stringsAsFactors = FALSE)

data %>%
  ggplot(aes(x, y)) + 
  geom_bar(stat = "identity") + 
  coord_flip() + 
  geom_vline(xintercept = seq(1.5, 9.5, 1), color = "white") + 
  theme(panel.grid = element_blank())

情节

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

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