【问题标题】:How to add segments in a lollipop chart ggplot2 in R如何在R中的棒棒糖图表ggplot2中添加段
【发布时间】:2020-07-22 15:58:43
【问题描述】:

这是我的初始条形图代码:

Full %>%                  ggplot(aes(x = reorder(POS, -Iconicity), y = Iconicity, fill = Group)) +
  geom_bar(stat = "summary", position=position_dodge(width = 0.9)) +
    scale_fill_viridis_d() + # color-blind compatible colors
  theme_minimal() + xlab("POS")

创建了这个可爱的图表:

所以我想把它变成一个棒棒糖图表,让它看起来更整洁、更现代,这就是我使用的代码:

Full %>%                  ggplot(aes(x = reorder(POS, -Iconicity), y = Iconicity, color = Group)) +
  geom_point(size=3, stat = "summary", position=position_dodge(width = 0.9)) +
  geom_segment(aes(x=POS, 
                    xend=POS, 
                    y=0,
                    yend=Iconicity)) +
  scale_fill_viridis_d() + # color-blind compatible colors
  theme_minimal() + xlab("POS") 

但是,这当然不会在正确的位置添加足够的段,而且我似乎无法弄清楚如何更改为代码。我剩下的就是:

显然我在 R 方面还是个新手,所以请原谅我

【问题讨论】:

  • 您能否详细解释一下您希望最终的图形是什么样子 - “棒棒糖图表”有点模糊。您能否也请使用 dput() 函数向我们展示您的数据是什么样的。谢谢。

标签: r ggplot2 segment


【解决方案1】:

我认为问题在于您总结了您的 geom_point 而不是 geom_segment 并且您在 geom_point 中使用了 position_dodge

如果没有关于什么是 Full 的可重复示例,很难确定您的问题的答案,但也许您可以尝试在 ggplot 之外总结您的价值观,并将相同的 position_dodge 应用于每个geom:

Full %>%  
  group_by(Group) %>%
  summarise(Iconicity = mean(Iconicity, na.rm = TRUE)) %>%
  ggplot(aes(x = reorder(POS, -Iconicity), y = Iconicity, color = Group)) +
  geom_point(size=3,position=position_dodge(width = 0.9)) +
  geom_segment(aes(x=POS, 
                   xend=POS, 
                   y=0,
                   yend=Iconicity), position = position_dodge(0.9) ) +
  scale_fill_viridis_d() + # color-blind compatible colors
  theme_minimal() + xlab("POS") 

它回答了你的问题吗?

如果不是,请提供一个可重现的示例,说明您的 Full 数据集是什么(请参阅此链接:How to make a great R reproducible example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 2017-07-28
    相关资源
    最近更新 更多