简体   繁体   English

如何在ggplot2条形图中的刻度线之间绘制x轴标签和条形图?

[英]How to plot x-axis labels and bars between tick marks in ggplot2 bar plot?

I prepared a MWE and hope for help on how to set ticks and labels at different position on the x-axis for a grouped bar plot. 我准备了MWE,希望对如何在成组的条形图的x轴上的不同位置设置刻度和标签有所帮助。

library(ggplot2)
library(reshape2)

data <- data.frame(name = c("X","Y","Z"), A = c(2,4,6), B = c(1,3,4), C = c(3,4,5))
data <- melt(data, id = 1)

ggplot(data, aes(name,value)) +
  geom_bar(aes(fill = variable), position = "dodge", stat = "identity")

MWEplot]([![在此处输入图片描述

The ticks should appear BETWEEN the groups, but the labels centered below the grouped bars (as they are in the figure). 刻度应出现在组之间,但标签位于组的条形下方的中心(如图中所示)。 I tried to set user-defined breaks (as factors) for scale_x_discrete but it only made my ticks and labels disappear completely. 我试图为scale_x_discrete设置用户定义的scale_x_discrete (作为因子),但这只会使我的刻度和标签完全消失。

Any help is much appreciated! 任何帮助深表感谢!

One options would be to convert the discrete x scale to continuous, to facilitate calculation of break positions: 一种选择是将离散的x比例转换为连续的,以方便计算break位置:

# numeric version of x values
data$x <- as.integer(as.factor(data$name))

1. x ticks between groups of bars 1. x组之间的刻度线

x_tick <- head(unique(data$x), -1) + 0.5
len <- length(x_tick)

ggplot(data, aes(x = x, y = value, fill = variable)) + 
  geom_col(position = "dodge") +
  scale_x_continuous(breaks = c(sort(unique(data$x)), x_tick),
                     labels = c(sort(unique(data$name)), rep(c(""), len))) +
  theme(axis.ticks.x = element_line(color = c(rep(NA, len + 1), rep("black", len))))

在此处输入图片说明


2: x ticks before, between, and after groups of bars 2:x组小节之前,之间和之后的刻度

x_tick <- c(0, unique(data$x)) + 0.5
len <- length(x_tick)

ggplot(data, aes(x = x, y = value, fill = variable)) + 
  geom_col(position = "dodge") +
  scale_x_continuous(breaks = c(sort(unique(data$x)), x_tick),
                     labels = c(sort(unique(data$name)), rep(c(""), len))) +
  theme(axis.ticks.x = element_line(color = c(rep(NA, len - 1), rep("black", len))))

在此处输入图片说明

Don't ask me about the additional grid lines which appeared at 2.25 and 1.75 respectively... 不要问我分别出现在2.25和1.75处的额外网格线...

Here is another solution which uses grid package. 这是另一个使用grid包的解决方案。

library(grid)

nTicks <- 2
tickersPosition <- unit(rep(1:nTicks /(nTicks+1), each=2), "native")

Part 1:nTicks /(nTicks+1) identifies positions where ticks will be placed. 1:nTicks /(nTicks+1)部分1:nTicks /(nTicks+1)标识将放置刻度的位置。

p1 <- ggplot(data, aes(name,value)) +
  geom_bar(aes(fill = variable), position = "dodge", stat = "identity") 

To change position of ticks we need to create gtable 要更改刻度线的位置,我们需要创建gtable

p2 <- ggplot_gtable(ggplot_build(p1))

and find the right grob (using str ): 并找到正确的grob(使用str ):

p2$grobs[[7]]$children$axis$grobs[[1]]$x <- tickersPosition

After the position is rewritten, we can run 改写职位后,我们可以运行

grid::grid.draw(p2)

which will show warnings. 这将显示警告。 This is because of a different number of splits. 这是因为分割数不同。

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

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