我有一个名为 df_plot 的数据框,我正在尝试创建一个按列排序的哑铃图(2020 年中龄),轴根据另一个类别分配了颜色)。这非常接近,但有人可以向我解释为什么轴上的颜色不匹配吗?正如您从数据框中看到的那样,所罗门群岛应该是红色的,但它们被输出为绿色?
非常感谢任何提示
df_plot<-df_plot %>% arrange(Median.Age.2020)
ggplot(df_plot,aes(x=Median.Age.2020,xend=Median.Age.2030,y=reorder(Country,Median.Age.2020))) +
geom_dumbbell( size=1.4,color="#5E5E5E",
colour_x = "#6E0019", colour_xend = "#FF5179",
dot_guide = T,
dot_guide_size = 0.4,
size_x=3.5,
size_xend=3.5)+
geom_point(aes(x=Median.age.2025,y=Country),size=3.5,color="#A50026")+
xlab("Median Age")+
xlim(14,26) +
theme_classic()+
theme(axis.text.y = element_text(colour=fct_reorder(df_plot$color, df_plot$Median.Age.2020)),
axis.ticks.y=element_blank())+
ylab("")
(我不确定您如何将数据帧添加到 stackoverflow 中,因此在此处复制标题 - 图片解释了结构。唯一的区别是我已将颜色更改为十六进制代码)
"Country" "HMTC.Region" "Median.Age.2020" "Median.age.2025" "Median.Age.2030" "Population_2020" "Population_2030" "id" "color"
"Niger" "Africa" 15.2 15.6 16.1 24075000 34994000 1 "#00B0F0"
"Mali" "Africa" 16.3 17 17.8 20284000 27057000 2 "#00B0F0"
"Chad" "Africa" 16.6 17.2 18 16285000 21460000 3 "#00B0F0"
"Uganda" "Africa" 16.7 17.8 19.1 47188000 63842000 4 "#00B0F0"
"Angola" "Africa" 16.7 17.2 17.9 32827000 44712000 5 "#00B0F0"
"Burundi" "Africa" 17.3 17.8 18.7 11939000 15799000 6 "#00B0F0"