r - 使用 ggplot 将变化绘制为值之间的条形图

标签 r ggplot2 plot bar-chart

我想像《经济学人》那样将值之间的变化绘制为值之间的单个条形图:

enter image description here

这个图称为dumbbell,所以如果您知道要搜索什么,问题很可能是重复的。

这是为两个类别制作条形图的代码示例。能够按照上面的示例绘制它会很棒。

library(ggplot2)
library(ggthemes)
Animals <- read.table(
  header=TRUE, text='Year        Reason Species
1   2018       Genuine      24
2   2019      Genuine      16
3   2019 Misclassified      85
4   2018 Misclassified      41
5   2018     Taxonomic       2
6   2019     Taxonomic       7
7   2018       Unclear      41
8   2019       Unclear     117')

Animals$Year <- as.character(Animals$Year)

ggplot(Animals, aes(factor(Reason), Species, fill = Year)) + 
  geom_bar(stat="identity", position = "dodge") + 
  scale_fill_brewer(palette = "Set1") + 
  theme_economist() + 
  coord_flip()

能够将颜色减少为粉红色(例如)将是一种奖励。

enter image description here

最佳答案

这是一个可能的解决方案,我使用第二个辅助 data.frame 和 geom_segment() 来绘制两个时间点之间的连接线。我还包括了一些视觉定制,如颜色、点类型、透明度和箭头。这些可以根据需要删除或修改。

# Auxiliary data.frame to draw segments.
adat = reshape2::dcast(data=Animals, 
                       formula=Reason ~ Year, 
                       value.var="Species")
adat
#          Reason 2018 2019
# 1       Genuine   24   16
# 2 Misclassified   41   85
# 3     Taxonomic    2    7
# 4       Unclear   41  117

# Colors selected from 
# https://yutannihilation.github.io/allYourFigureAreBelongToUs/ggthemes/economist_pal/
year_colors = c("2018"="#D7D29E", "2019"="#82C0E9")

p1 = ggplot() +
    theme_economist() +
    geom_point(data=Animals, 
               aes(y=Reason, x=Species, fill=Year),
               size=6, shape=21, color="grey30") +
    geom_segment(data=adat, 
                 aes(y=Reason, yend=Reason, x=`2018`, xend=`2019`),
                 size=1.8, color="grey30",
                 lineend="butt", linejoin="mitre",
                 arrow=arrow(length = unit(0.02, "npc"), type="closed")) +
    scale_fill_manual(values=year_colors)

p2 = ggplot() +
     theme_economist() +
     geom_segment(data=adat, 
                  aes(y=Reason, yend=Reason, x=`2018`, xend=`2019`),
                  size=6, color="#82C0E9", alpha=0.6, lineend="butt") +
     geom_point(data=Animals, 
                aes(y=Reason, x=Species, color=Year),
                size=6) +
     xlab("Species") +
     scale_color_manual(values=c("2018"="#C10534", "2019"="#008BBC"))

ggsave("segment_plot_1.png", plot=p1, width=8, height=2.5, dpi=150)
ggsave("segment_plot_2.png", plot=p2, width=8, height=2.5, dpi=150)

# Basic version.
p3 = ggplot() +
     geom_point(data=Animals, aes(y=Reason, x=Species, color=Year)) +
     geom_segment(data=adat, aes(y=Reason, yend=Reason, x=`2018`, xend=`2019`))

ggsave("segment_plot_3.png", plot=p3, width=8, height=2.5, dpi=150)

enter image description here

enter image description here

enter image description here

关于r - 使用 ggplot 将变化绘制为值之间的条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56817348/

相关文章:

visual-studio-code - vscode在固定位置打开 "Julia Plots"编辑器

r - 是否有比使用三重感叹号选择更强大的重命名替代方案?

javascript - 仅当使用 LayersControl 缩放级别 > 8 时,才在 Shiny 中显示传单 map 中的图层?

r - 在ggplot中创建多列图例

r - 如何在一个图中绘制 'multiple box plots'?

r - R 中的叠加直方图(首选 ggplot2)

python - 向按年线分组的时间序列添加标签 - Matplotlib、Python

r - 将一列从分类转换为二进制,保留其余列

使用已贬值的 funs() 重新编写旧代码,并且无法使 n() 工作

r - 如何在ggplot2中更改自定义图例中的线条角度