繁体   English   中英

基于变量的ggplot颜色轴标签

[英]ggplot color axis labels based on variable

我有一个名为 df_plot 的 dataframe,我正在尝试创建一个按列排序的哑铃 plot(2020 年中龄),轴分配了基于另一个类别的颜色)。 这非常接近,但有人可以向我解释为什么轴中的 colors 不匹配吗? 正如您从 dataframe 中看到的那样,所罗门群岛应该是红色的,但它们被输出为绿色?

非常感谢任何提示

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

图像颜色错误

名为 df_plot 的数据框

(我不确定您如何将 dataframe 添加到 stackoverflow 中,因此在这里复制了 header - 图片说明了结构。唯一的区别是我已将颜色更改为十六进制代码)

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

您确实应该提供一个可重现的示例并命名所需的包。

你把你的 colors 变成因子,从而变成黑色、红色、绿色。 如果您保持原样,它们将被正确解释:

library(forestplot)
library(dplyr)
library(ggalt)

df_plot <- structure(list(
  Country = c("Solomon Islands", "Afghanistan", "Niger", "Mali", "Chad", "Uganda", "Angola", "Democratic Republic of Congo", "Burundi", "Mozambique"), 
  HMTC.Region = c("Asia Pacific", "Middle East", "Africa", "Africa", "Africa", "Africa", "Africa", "Africa", "Africa", "Africa"), 
  Median.Age.2020 = c(19.9, 18.4, 15.2, 16.3, 16.6, 16.7, 16.7, 17, 17.3, 17.6), 
  Median.age.2025 = c(20.4, 19.9, 15.6, 17, 17.2, 17.8, 17.2, 17.5, 17.8, 18.3), 
  Median.Age.2030 = c(21.1, 21.5, 16.1, 17.8, 18, 19.1, 17.9, 18.2, 18.7, 19), 
  age_group = c("box1", "box1", "box1", "box1", "box1", "box1", "box1", "box1", "box1", "box1"), 
  color = c("Red", "Orange", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue", "Blue")), 
  class = "data.frame", row.names = c(28L, 15L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L))

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=df_plot$color),
        axis.ticks.y=element_blank())+
  ylab("")
#> Warning: Vectorized input to `element_text()` is not officially supported.
#> Results may be unexpected or may change in future versions of ggplot2.

代表 package (v0.3.0) 于 2020 年 5 月 23 日创建

触摸木头我已经想通了。 上面的代码运行良好,但是当值具有相同值时无法处理。 所以需要添加第二层排列df<-df %>% arrange(Median.Age.2020,Country) %>% mutate(id=row_number())

library(dplyr)
library(ggalt)
library(ggplot2)
library(tidyverse)
library(ggalt)
library(ggrepel)
library(RColorBrewer)

df<-read.csv("..Median_age.csv",stringsAsFactors = F)

limit<-10E6 #10 mill

#Drop unknown HMTC regions and those with more than 10 mill
df <- df %>% filter(!HMTC.Region=="N/A") %>% filter(Population_2020>=limit & Population_2030>=limit)


df$Country<-str_trim(df$Country)



df<-df  %>% arrange(Median.Age.2020,Country) %>% mutate(id=row_number())




df_plot <- structure(list(
  Country = df$Country, 
  HMTC.Region = df$HMTC.Region, 
  Median.Age.2020 = df$Median.Age.2020, 
  Median.age.2025 = df$Median.age.2025, 
  Median.Age.2030 = df$Median.Age.2030),
  class = "data.frame", row.names = c(NA,-72L)) #72 is length of dataframe?

df_plot$color<-"#4D4D4D"
df_plot<-df_plot %>% mutate(color=ifelse(HMTC.Region=="Africa","#00B0F0",color)) %>%
  mutate(color=ifelse(HMTC.Region=="Europe","#FFBD33",color)) %>%
  mutate(color=ifelse(HMTC.Region=="Asia Pacific ","#7030A0",color)) %>%
  mutate(color=ifelse(HMTC.Region=="Eastern Europe & Central Asia","#5F589E",color)) %>%
  mutate(color=ifelse(HMTC.Region=="South Asia","#F6003B",color)) %>%
  mutate(color=ifelse(HMTC.Region=="Latin America","#82C836",color)) %>%
  mutate(color=ifelse(HMTC.Region=="Middle East","#A19F57",color)) %>%
  mutate(color=ifelse(HMTC.Region=="North America","#002060",color)) 


# 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,70) +
  theme_classic()+
  theme(axis.text.y = element_text(colour=df_plot$color),
        axis.ticks.y=element_blank())+
  ylab("")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM