绘制矩形和lineranges与GGPLOT2离散轴

问题描述 投票:2回答:1

我是相当新的计算器。我要绘制矩形,而不是lineranges因为我希望有一个黑色边框。其实我的教授希望有黑边,但不是用于计算器的问题。

加载库和创建虚拟数据集

library(tidyverse)

mydat <- tibble(
       mymsmt = rep(c("bio", "bio", "den", "den"), 2),
       mylvl = c("NT", "till", "NT", "till", "no", "yes", "no", "yes"),
       mytrt = c(rep("tillage", 4), rep("herbicides", 4)),
       est = c(-60, -13, -65, -40, -16, -24, -49, -50),
       cilow = c(-85, -48, -78, -56, -61, -60, -68, -64),
       ciup = c(8, 45, -44, -18, 79, 42, -20, -31)) %>%
       # Dummy code mylvls as numeric
       mutate(mylvln = rep(c(1, 2), 4))  

如果我只用linerange情节,它的工作原理(我不能嵌入图像还)

ggplot(mydat, aes(est, mylvl)) + 
  geom_linerangeh(aes(xmin = cilow, xmax = ciup), color = "blue", size = 5) +
  # geom_rect(aes(xmin = cilow, xmax = ciup, 
  #               ymin = mylvln - 0.2, ymax = mylvln + 0.2), 
  #           fill = "red", color = "black") +
  geom_point() + 
  facet_grid(mytrt ~ mymsmt, scales = "free")

情节只有矩形,失败,错误如下:提供给连续尺度离散值

ggplot(mydat, aes(est, mylvl)) + 
  #geom_linerangeh(aes(xmin = cilow, xmax = ciup), color = "blue", size = 5) +
  geom_rect(aes(xmin = cilow, xmax = ciup, 
                 ymin = mylvln - 0.2, ymax = mylvln + 0.2), 
             fill = "red", color = "black") +
  geom_point() + 
  facet_grid(mytrt ~ mymsmt, scales = "free")

情节与linerange,由矩形覆盖,工作,你可以看到lineranges背景

ggplot(mydat, aes(est, mylvl)) + 
  geom_linerangeh(aes(xmin = cilow, xmax = ciup), color = "blue", size = 5) +
  geom_rect(aes(xmin = cilow, xmax = ciup, 
                ymin = mylvln - 0.2, ymax = mylvln + 0.2), 
            fill = "red", color = "black", alpha = 0.5) +
  geom_point() + 
  facet_grid(mytrt ~ mymsmt, scales = "free")

Why? It works, I get the figure I want, but I don't know why. Thanks for your help!

r ggplot2 rectangles facet axis-labels
1个回答
1
投票

您还可以在地方geom_tile的使用geom_rect

ggplot(mydat, aes(est, mylvl)) + 
  geom_tile(aes(width = ciup-cilow, height=0.1),  fill="red", color="black") +
  geom_point() + 
  facet_grid(mytrt ~ mymsmt, scales = "free")

enter image description here

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