繁体   English   中英

如何在ggplot上插入空格? geom_rect() 的问题

[英]How to insert blank space on a ggplot? An issue with geom_rect()

请在下面找到一个reprex

我已经生产了这个 plot

在此处输入图像描述

如您所见,网格从 0 连续到 -5。 我希望将该区域完全留空,如 plot (在 Photoshop 中制作):

在此处输入图像描述

我试过 geom_rect() 但我不能让它工作:

在此处输入图像描述

1) 为什么指定填充=“白色”时矩形是粉红色的? 我需要它是白色的。

2) 如何缩小从 geom_rect() 底部到 x 轴的间隙? 我也需要那个空间是白色的(即,没有网格的迹象)。

解决方案

1) geom_rect(data=df, mapping=aes(xmin=0, xmax=3, ymin=-6, ymax=0), fill="white", alpha=1)

2) 我改为scale_y_continuous(name="X", breaks=seq(0,35,5)) + coord_cartesian(ylim=c(-5,35))而不是scale_y_continuous(name="X", breaks=seq(0,35,5), limits=c(-5,35))

#My script
df <- data.frame(x = as.factor(w3$uicc),
                  y = w3$lnd,
                  f = rep(c(""), c(nrow(w3))))

df <- df[!is.na(df$x),]

  ggplot(df) +
    geom_boxplot(aes(x, y, fill = f, colour = f), outlier.alpha = 0, position = position_dodge(width = 0.78)) +
    scale_x_discrete(name = "", label=c("Stage III\nn=115","Stage IV\nn=302")) + theme +
    scale_y_continuous(name="X", breaks=seq(0,35,5), limits=c(-5,35)) +
    stat_boxplot(aes(x, y, colour = f), geom = "errorbar", width = 0.3,position = position_dodge(0.7753), size=1) +
    geom_point(aes(x, y, fill = f, colour = f), size = 3, shape = 21, position = position_jitterdodge(), alpha=0.7) +
    scale_fill_manual(values = c("#edf1f9", "#fcebeb"), name = "X",
                      labels = c("X", "X")) +
    scale_colour_manual(values = c("#1C73C2", "red"), name = "X",
                        labels = c("X", "X")) +
    geom_rect(data=df, mapping=aes(xmin=0, xmax=3, ymin=-5, ymax=0, fill="white"), alpha=1)



#My data

w3 <- structure(list(uicc = c(4L, 3L, 4L, 4L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 3L, 3L, 3L, 4L, 3L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 
4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 3L, 3L, 3L, 3L, 4L, 
4L, 3L, 3L, 3L, 3L, 4L, 4L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
3L, 3L, 4L, 3L, 3L, 4L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
3L, 4L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 4L), lnd = c(22.2222222, 
9.0909091, 25, 12.1212121, 6.6666667, 4.3478261, 33.3333333, 
10, 33.3333333, 60, 71.4285714, 25, 16.6666667, 11.1111111, 42.8571429, 
16.6666667, 10, 62.5, 25, 11.1111111, 50, 33.3333333, 7.6923077, 
7.6923077, 7.1428571, 16.6666667, 20, 14.2857143, 11.7647059, 
12.5, 71.4285714, 17.6470588, 33.3333333, 2.8571429, 7.1428571, 
5.8823529, 6.25, 4.7619048, 5, 14.2857143, 3.3333333, 20, 28.5714286, 
100, 7.1428571, 9.0909091, 2.173913, 18.1818182, 7.6923077, 9.0909091, 
14.2857143, 4.5454545, 8.6956522, 3.8461538, 11.5384615, 11.1111111, 
66.6666667, 4, 4.5454545, 14.2857143, 25, 14.2857143, 14.2857143, 
25, 7.6923077, 9.0909091, 5.8823529, 4.5454545, 16.6666667, 12.5, 
4.7619048, 30, 8.3333333, 10.5263158, 8.1081081, 8.3333333, 6.6666667, 
18.1818182, 5, 9.0909091, 6.6666667, 63.6363636, 26.6666667, 
12.5, 14.2857143, 25, 6.0606061, 25.9259259, 25, 10.5263158, 
40, 9.0909091, 6.6666667, 16.6666667, 20, 22.2222222, 14.2857143, 
15.3846154, 12.1212121, 2.0408163)), row.names = c(3L, 4L, 10L, 
15L, 19L, 20L, 35L, 37L, 40L, 47L, 48L, 50L, 54L, 57L, 59L, 62L, 
63L, 66L, 67L, 68L, 74L, 80L, 81L, 83L, 84L, 94L, 99L, 100L, 
117L, 120L, 122L, 123L, 129L, 130L, 131L, 137L, 140L, 141L, 142L, 
144L, 165L, 171L, 175L, 182L, 192L, 195L, 198L, 199L, 204L, 208L, 
209L, 210L, 211L, 212L, 215L, 218L, 221L, 222L, 234L, 236L, 239L, 
241L, 242L, 243L, 246L, 251L, 257L, 260L, 261L, 268L, 275L, 276L, 
277L, 280L, 284L, 294L, 297L, 298L, 303L, 306L, 309L, 316L, 317L, 
319L, 320L, 322L, 324L, 326L, 328L, 329L, 330L, 333L, 334L, 336L, 
337L, 339L, 340L, 341L, 346L, 349L), class = "data.frame") 

要使geom_rect为白色,必须在映射之外设置fill参数,如下所示:

geom_rect(data=df, mapping=aes(xmin=0, xmax=3, ymin=-5, ymax=0), fill = "white", alpha=1)

当您指定

geom_rect(mapping=aes([...], fill="white"), alpha=1)

你问 ggplot 到 map 变量“白色”到一种颜色,这是使用其默认调色板。 如果要为单个geom指定颜色,最简单的方法是在aes之外指定

geom_rect(mapping=aes([...]), fill="white", alpha=1)

同样,如果您要指定

geom_rect(mapping=aes(alpha=1))

然后 ggplot2 将尝试将 map 这个连续值转换为连续的 alpha 比例( scale_alpha_continuous

我已经用这两个问题的解决方案更新了这个问题

暂无
暂无

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

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