Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在同一刻面ggplot2上显示两个数据帧_R_Ggplot2 - Fatal编程技术网

R 在同一刻面ggplot2上显示两个数据帧

R 在同一刻面ggplot2上显示两个数据帧,r,ggplot2,R,Ggplot2,我有两个数据帧,我正在努力合并成一个绘图。下面给出了第一个数据帧的复制,然后是生成数据森林图的代码,针对不同的变量组分面。如果p.value为您没有提供数据帧df2,则会填充geom_点,因此让我创建一个具有随机值的点 df2 <- df1 %>% mutate( coefficient = coefficient + rnorm(1, sd=0.1), conf.low = coefficient - 0.05, co

我有两个数据帧,我正在努力合并成一个绘图。下面给出了第一个数据帧的复制,然后是生成数据森林图的代码,针对不同的变量组分面。如果p.value为您没有提供数据帧
df2
,则会填充
geom_点
,因此让我创建一个具有随机值的点

df2 <-
    df1 %>%
    mutate(
        coefficient = coefficient + rnorm(1, sd=0.1),
        conf.low = coefficient - 0.05,
        conf.high = coefficient + 0.05,
        significant = ifelse(p.value > 0.05, 'P > 0.05', 'P < 0.05'))
可以使用
geom\u pointrange()
geom\u point()
命令中的
color
美学和
position\u dodge()
创建图形

ggplot(data = df, aes(outcome.var, coefficient, color=dataset)) +
    geom_pointrange(aes(ymin = conf.low, ymax = conf.high), shape = 32, position=position_dodge(width=0.5)) +
    geom_point(aes(shape = significant), fill = 'white', position=position_dodge(width=0.5)) +
    geom_hline(mapping = NULL, data = NULL, yintercept = 0, colour = "grey40", size = 0.5, linetype = "solid") +
    geom_hline(mapping = NULL, data = NULL, yintercept = c(-0.15, -0.10, -0.05, 0.05, 0.10), colour = "grey85", size = 0.5, linetype = "longdash") +
    theme(panel.background = element_rect(fill = "grey95")) +
    scale_y_continuous('Coefficient') +
    scale_shape_manual(values = c(19, 21)) +
    # scale_colour_manual(values = c('black', 'black')) + 
    theme(panel.spacing = unit(1, "lines")) +
    theme(legend.title = element_blank()) +
    xlab(NULL) +
    coord_flip() +
    facet_grid(label ~ ., scales = "free", space = "free", switch = "x") +
    theme(strip.text.y = element_blank()) +
    theme(legend.text = element_text(face = 'bold'))

你能提供一个
df2
的例子,就像你对
df1
所做的那样吗?第一个好答案:)这比管道中包含管道的另一种方法可读性稍高。谢谢,也谢谢你包括图表和简化代码:)@David和@Mikey谢谢你的解决方案。在我的代码中,我忘记了将
output.var
格式化为一个因子,以确保y轴上标签的顺序符合我的要求。此顺序既不是字母顺序,也不是非字母顺序。当我将您的解决方案应用于我自己的数据时,为
df1
df2
绘制的点和置信区间是相同的。这可能是由转换为因子的附加步骤引起的吗?我无法理解为什么作为因子的标签会导致
df1
df2
的数据帧相同。在合并数据帧之前,请检查数据帧。这个问题似乎与这个问题明显不同,所以如果你找不到解决问题的方法,你应该把它作为一个单独的问题发布。@Mikey我想我已经解决了。如果我在绘图调用的第一行中设置
group=dataset
,则似乎可以防止数据点脱离置信区间线的中心。
df2 <-
    df1 %>%
    mutate(
        coefficient = coefficient + rnorm(1, sd=0.1),
        conf.low = coefficient - 0.05,
        conf.high = coefficient + 0.05,
        significant = ifelse(p.value > 0.05, 'P > 0.05', 'P < 0.05'))
df1 <- df1 %>%
    mutate(dataset = 'original')

df2 <- df2 %>%
    mutate(dataset = 'alternative')

df <- bind_rows(df1, df2)
ggplot(data = df, aes(outcome.var, coefficient, color=dataset)) +
    geom_pointrange(aes(ymin = conf.low, ymax = conf.high), shape = 32, position=position_dodge(width=0.5)) +
    geom_point(aes(shape = significant), fill = 'white', position=position_dodge(width=0.5)) +
    geom_hline(mapping = NULL, data = NULL, yintercept = 0, colour = "grey40", size = 0.5, linetype = "solid") +
    geom_hline(mapping = NULL, data = NULL, yintercept = c(-0.15, -0.10, -0.05, 0.05, 0.10), colour = "grey85", size = 0.5, linetype = "longdash") +
    theme(panel.background = element_rect(fill = "grey95")) +
    scale_y_continuous('Coefficient') +
    scale_shape_manual(values = c(19, 21)) +
    # scale_colour_manual(values = c('black', 'black')) + 
    theme(panel.spacing = unit(1, "lines")) +
    theme(legend.title = element_blank()) +
    xlab(NULL) +
    coord_flip() +
    facet_grid(label ~ ., scales = "free", space = "free", switch = "x") +
    theme(strip.text.y = element_blank()) +
    theme(legend.text = element_text(face = 'bold'))