【问题标题】:Separate geom_linerange by group within a facet in ggplot在ggplot的一个方面内按组分隔geom_linerange
【发布时间】:2021-12-26 20:55:57
【问题描述】:

假设我有以下数据:

test = read.table(text = 'condition1 condition2 estimate std_error name
a x .466 .09 name_1
a y .343 .131 name_1
b x .466 .09 name_1
b y .343 .131 name_1
a x .466 .09 name_2
a y .343 .131 name_2
b x .466 .09 name_2
b y .343 .131 name_2', header = T, stringsAsFactors = T)


ggplot(data = test, aes(x = estimate, y = condition1, fill = condition2, group = condition2)) +
    geom_point(color = 'black') +
    geom_linerange(aes(xmin = estimate - std_error,
                       xmax = estimate + std_error), color = 'black') +
    ylab(NULL) +
    facet_grid(name ~ .,
               scales = "free_y",
               space = "free_y",
               switch = 'y')

我试图将xy 行分离为ba 条件within 给定方面(name_1name_2)中的单独行。但是我的代码将这两行作为相同的 y 值,所以它们是重叠的。分隔线的最佳方法是什么?

【问题讨论】:

    标签: r ggplot2 dplyr


    【解决方案1】:

    根据口味调整width = X

    ...
     geom_point(color = 'black', position = position_dodge(width = 1)) +
      geom_linerange(aes(xmin = estimate - std_error,
                         xmax = estimate + std_error), color = 'black',
                         position = position_dodge(width = 1)) +
    ...
    

    或者在这里进行一些更美观的调整:

    ggplot(data = test, aes(x = estimate, y = condition1, fill = condition2, group = condition2)) +
      geom_linerange(aes(xmin = estimate - std_error,
                         xmax = estimate + std_error), color = 'black',
                     position = position_dodge(width = 0.5)) +
      geom_point(color = 'black', size = 2, shape = 21, position = position_dodge(width = 0.5)) +
      ylab(NULL) +
      facet_grid(name ~ .,
                 scales = "free_y",
                 space = "free_y",
                 switch = 'y') +
      theme(panel.grid.major.y = element_blank())
    

    【讨论】:

    • 谢谢!我曾在aes 中尝试过position = dodge,但它没有做任何事情。没想到你必须把它包装在position_dodge
    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    相关资源
    最近更新 更多