如何在 ggplot2 图表中的某些组之间插入空格

问题描述 投票:0回答:3

我刚刚做了这个图表

enter image description here

使用这组数据

75e1444444
G T
1 7
1 9
1 8
1 7
1 6
1 8
1 7
1 8
1 7
1 7
1 6
1 7
1 7
1 9
1 9
2 9
2 7
2 8
2 8
2 9
2 8
2 9
2 7
2 10
2 9
2 7
2 8
2 8
2 7
2 8
3 16
3 17
3 19
3 17
3 17
3 18
3 17
3 19
3 18
3 17
3 18
3 19
3 19
4 17
4 18
4 16
4 18
4 18
4 20
4
16
20
17
17
19
19
这段代码:

ggplot(Raiz, aes(x = G, y = T, color = c('WT', 'at5g01950')[2 - (as.numeric(G) %% 2)])) + geom_jitter(position = position_jitter(width = 0.2), alpha = 1, size =3) + geom_boxplot(width = 0.5, color = "black", alpha = 0.2, position = position_dodge(0.9)) + theme_minimal() + theme(plot.background = element_rect(fill = "white") , panel.grid.minor = element_blank()) + labs(color = '')

我想知道如何在每次治疗的测量值对之间留出空格。例如:1,2 - 空格 - 3,4 .... 不是一个很大的空格,但足以轻松区分治疗。

r ggplot2
3个回答
1
投票

创建于 2024-01-08,使用

reprex v2.0.2


1
投票
facet_wrap

scale = free_x
一起使用。为此,我们向数据框中添加一个新列,其中包含两个组:
library(dplyr)
library(ggplot2)

Raiz %>% 
  mutate(supergroup = ifelse(G %in% c(1,2), "one", "two")) %>% 
  ggplot(aes(x = factor(G), y = T, 
                   color = c('WT', 'at5g01950')[2 - (as.numeric(G) %% 2)])) +
  geom_jitter(position = position_jitter(width = 0.2), alpha = 0.5, size =4) +
  geom_boxplot(width = 0.5, color = "black", alpha = 0.2,
             position = position_dodge(0.9)) +
  scale_color_manual(values = c("steelblue3", "red3"))+
  facet_wrap(.~supergroup, scales = "free_x", labeller = labeller(supergroup = function(x) ""))+
  labs(x="")+
  theme_minimal() + 
  theme(plot.background = element_rect(fill = "white") ,
        panel.grid.minor = element_blank()) +
  labs(color = '')+
  theme(text = element_text(size = 14))

tar_ggplot_font_size()

数据:

structure(list(G = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), T = c(7L, 9L, 8L, 7L, 6L, 8L, 7L, 8L, 7L, 7L, 6L, 7L, 7L, 9L, 9L, 9L, 7L, 8L, 8L, 9L, 8L, 9L, 7L, 10L, 9L, 7L, 8L, 8L, 7L, 8L, 16L, 17L, 19L, 17L, 17L, 18L, 17L, 19L, 18L, 17L, 18L, 19L, 19L, 17L, 18L, 16L, 18L, 18L, 20L, 15L, 16L, 20L, 17L, 17L, 19L, 19L)), class = "data.frame", row.names = c(NA, -56L))


0
投票

ggplot(within(Raiz, x <- G + (G - 1) %/% 2 * 0.4), aes(x = x, y = T, color = c('WT', 'at5g01950')[2 - (as.numeric(G) %% 2)])) + geom_jitter(position = position_jitter(width = 0.2), alpha = 1, size =3) + geom_boxplot(width = 0.75, color = "black", alpha = 0.2, position = position_dodge(0.9), aes(group = G)) + scale_x_continuous(breaks = 1:6 + 0:5 %/% 2 * 0.4, labels = floor) + theme_minimal() + theme(plot.background = element_rect(fill = "white") , panel.grid.minor = element_blank()) + labs(color = '')

© www.soinside.com 2019 - 2024. All rights reserved.