ggplot2中的不同geom_point形状以factor为条件

时间:2018-03-01 12:18:54

标签: r ggplot2

我为我正在研究的项目创建了一个森林图。我正在努力实现的目标是为“重要”结果填充形状,为“非重要”结果填充空心形状。我试图在下面格式化一个可重现的例子,它复制了我所拥有的问题。

首先是数据框:

library(tidyverse)

##data frame
df <- tibble('outcome.var' = c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 
'J'),
   'coefficient' = c(-0.08, -0.01, -0.06, 0.02, 0.01, 0.02, -0.08, -0.1, 0.1, 0.2),
   'conf.low' = c(-0.12, -0.03, -0.09, 0.01, 0.02,  0.01, -0.10, -0.2, 0.05, 0.1),
   'conf.high' = c(-0.05, 0.02, -0.03, 0.03, -0.01, 0.04, -0.06, 0, 0.2, 0.3),
   'p.value' =c(0.01, 0.02, 0.05, 0.10, 0.02, 0.13, 0.11, 0.01, 0.01, 0.04)) %>%
  mutate(significant = as.factor(ifelse(p.value > 0.05, 'nonsig', 'sig')),
     label = case_when(
      .$outcome.var %in% c('A', 'B', 'C') ~ 'First',
      .$outcome.var %in% c('D', 'E', 'F') ~ 'Second',
      .$outcome.var %in% c('G', 'H') ~ 'Third',
      .$outcome.var %in% c('I', 'J') ~ 'Fourth'))
然后是森林情节。如果df$significant变量是&lt; = 0.05,我希望填充这些点,如果> 0.05,我希望填充这些点。虽然这个图例产生了填充和空心的形状,反映了我所追求的格式,但实际的情节本身只有圆圈。

##forest plot
p1 <- ggplot(df, aes(outcome.var, coefficient, shape = factor(significant))) +
  geom_point() +
  scale_shape_manual(values = c(1, 16)) +
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high), colour = "grey1", shape = 20) +
  geom_hline(mapping = NULL, data = NULL, yintercept = 0, colour = "grey42", size = 0.5, linetype = "longdash") +
  theme(panel.background = element_rect(fill = "grey98")) +
  coord_flip()

enter image description here

使用@PoGibas的有用答案,我可以更好地理解不同层和规范的解释。随着一些游戏,我终于找到了解决方案。

p1 <- ggplot(df, aes(outcome.var, coefficient)) +
  geom_pointrange(aes(ymin = conf.low, ymax = conf.high), shape = 32) +
  geom_point(aes(shape = significant), fill = 'white') +
  geom_hline(mapping = NULL, data = NULL, yintercept = 0, colour = "grey42", 
size = 0.5, linetype = "longdash") +
  scale_shape_manual(values = c(21, 19)) +
  scale_colour_manual(values = c('black', 'black')) + 
  theme(panel.background = element_rect(fill = "grey98")) +
  coord_flip()

1 个答案:

答案 0 :(得分:0)

您的代码过于复杂,但主要问题是您在主significant中指定shape = aes,并在geom_pointrange中指定一个形状。

我会写这样的代码:

代码:

# Do you really need geom_point layer?
library(ggplot2)
ggplot(df, aes(outcome.var, coefficient, shape = significant)) +
    geom_pointrange(aes(ymin = conf.low, ymax = conf.high)) +
    geom_hline(yintercept = 0, colour = "grey42", size = 0.5, linetype = "longdash") +
    scale_shape_manual(values = c(1, 16)) +
    theme(panel.background = element_rect(fill = "grey98")) +
    coord_flip()

简介:

enter image description here

数据(df):

structure(list(outcome.var = c("A", "B", "C", "D", "E", "F", 
"G", "H", "I", "J"), coefficient = c(-0.08, -0.01, -0.06, 0.02, 
0.01, 0.02, -0.08, -0.1, 0.1, 0.2), conf.low = c(-0.12, -0.03, 
-0.09, 0.01, 0.02, 0.01, -0.1, -0.2, 0.05, 0.1), conf.high = c(-0.05, 
0.02, -0.03, 0.03, -0.01, 0.04, -0.06, 0, 0.2, 0.3), p.value = c(0.01, 
0.02, 0.05, 0.1, 0.02, 0.13, 0.11, 0.01, 0.01, 0.04), significant = structure(c(2L, 
2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L), .Label = c("nonsig", "sig"
), class = "factor"), label = c("First", "First", "First", "Second", 
"Second", "Second", "Third", "Third", "Fourth", "Fourth")), .Names = c("outcome.var", 
"coefficient", "conf.low", "conf.high", "p.value", "significant", 
"label"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-10L))

编辑(OP要求使用颜色而不是形状):

ggplot(df, aes(outcome.var, coefficient)) +
    geom_pointrange(aes(ymin = conf.low, ymax = conf.high)) +
    geom_point(aes(color = significant)) +
    geom_hline(yintercept = 0, colour = "grey42", size = 0.5, linetype = "longdash") +
    theme(panel.background = element_rect(fill = "grey98")) +
    coord_flip()