如何在R中的棒棒糖图表ggplot2中添加片段

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

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

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")

哪个创建了这个可爱的图表:

the barchart

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

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") 

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

catastrophic lollipop chart attempt

我显然仍然是R的新手,请原谅我

r ggplot2 segment
1个回答
0
投票

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

没有什么是Full的可复制示例,很难确定,但也许您可以尝试:

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

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